Need help with implementing a delay for a minigame.


hello everyone!

me , friend trying create project @ school, , have run issue can't find solution on our own. tried googling few hours trying figure out how it, hasn't helped unfortunately.

basicly trying make game 2 players compete against eachother, trying react fastest.

we have 2 buttons installed, 1 each player.

both players have 3 led's represent health.

when led in middle flashes, player pushes button first, turns off 1 of opponent players leds.

the issue we're having makes possible players keep holding button down. trying implement delay. (if push button , led in middle not flashing, delay.).

we don't want use delay() function, causes issues other parts of our program.

we use millis(); randomly turning led in middle on.

this our code far:

code: [select]


const int pl1liv1 = 13; // player1 life
const int pl1liv2 = 12; // player1 life
const int pl1liv3 = 11; // player1 life
const int pl2liv1 = 10; // player2 life
const int pl2liv2 = 9; // player2 life
const int pl2liv3 = 8; // player2 life
const int vinder = 6; // led represent player winning.
const int delaypl1 = 5; // led represent delay player1
const int delaypl2 = 4; // led represent delay player2
const int lampe = 3; // led in middle.
const int pl1skyd = 2; // player 1 button.
const int pl2skyd = 7; // player 2 button.
const int reset = 0;

int pl1liv = 3;
int pl2liv = 3;







unsigned long previousmillis = 0;
long interval = random(3000,6000);



int buttonstate1 = 0;
int buttonstate2 = 0;
int lampestate = low;


void setup() {
serial.begin(9600);
pinmode(pl1liv1, output);
pinmode(pl1liv2, output);
pinmode(pl1liv3, output);
pinmode(pl2liv1, output);
pinmode(pl2liv2, output);
pinmode(pl2liv3, output);
pinmode(vinder, output);
pinmode(delaypl1, output);
pinmode(delaypl2, output);
pinmode(lampe, output);
pinmode(pl1skyd, input);
pinmode(pl2skyd, input);
pinmode(reset, input);
randomseed(analogread(0));

}

void loop() {
 
 unsigned long currentmillis = millis();
 
 buttonstate1 = digitalread(pl1skyd);
 buttonstate2 = digitalread(pl2skyd);
 
 

 
 if (currentmillis - previousmillis >= interval){
  previousmillis = currentmillis;

    if (lampestate == low){
      lampestate = high;
    } else {
      lampestate = low;
    }
    digitalwrite(lampe, lampestate);
 }

   
 
 

     
  if (pl1liv == 3)
    {
      digitalwrite (pl1liv1, high);
      digitalwrite (pl1liv2, high);
      digitalwrite (pl1liv3, high);
    }
    else if (pl1liv == 2)
      {
        digitalwrite (pl1liv1, high);
        digitalwrite (pl1liv2, high);
        digitalwrite (pl1liv3, low);
      }
      else if (pl1liv == 1)
      {
        digitalwrite (pl1liv1, high);
        digitalwrite (pl1liv2, low);
        digitalwrite (pl1liv3, low);
      }
        else if (pl1liv == 0)
        {
          digitalwrite (pl1liv1, low);
          digitalwrite (pl1liv2, low);
          digitalwrite (pl1liv3, low);
          digitalwrite (vinder, high);
          digitalwrite (delaypl1, high);
      }

       
   if (pl2liv == 3)
    {
      digitalwrite (pl2liv1, high);
      digitalwrite (pl2liv2, high);
      digitalwrite (pl2liv3, high);
    }
    else if (pl2liv == 2)
      {
        digitalwrite (pl2liv1, high);
        digitalwrite (pl2liv2, high);
        digitalwrite (pl2liv3, low);
      }
      else if (pl2liv == 1)
      {
        digitalwrite (pl2liv1, high);
        digitalwrite (pl2liv2, low);
        digitalwrite (pl2liv3, low);
      }
        else if (pl2liv == 0)
        {
          digitalwrite (pl2liv1, low);
          digitalwrite (pl2liv2, low);
          digitalwrite (pl2liv3, low);
          digitalwrite (vinder, high);
          digitalwrite (delaypl2, high);
      }


 if (buttonstate1 == high && lampestate == high)
  {
   serial.println(pl2liv);
    pl2liv--;
   buttonstate1 = low;
   lampestate = low;
   digitalwrite(lampe, low);
   serial.println(buttonstate1);
   serial.println(lampestate);
  }

 
 if (buttonstate2 == high && lampestate == high)
  {
   serial.println(pl1liv);
    pl1liv--;
   buttonstate2 = low;
   lampestate = low;
   digitalwrite(lampe, low);
   serial.println(buttonstate2);
   serial.println(lampestate);
  }


 

     
     
 
 
 
}
 
 



basicly need implementing way count. if player1 pushes button when led (lampe) not high. needs delay 1 sec (perhaps variable changing 1 0 after second)



here picture of our board.
thank taking time read through post, hope can :)

your thinking wrong. stop thinking delay. think about, instead, ignoring input switch period of time, if event occurs. user presses switch @ wrong time (the led not lit) event. event sets flag.

on every pass through loop(), read state of each switch if flag ignore switch not set.

on every pass through loop(), determine if time clear flag, if set. clearing happens if time since flag set exceeds threshold (one second you, 3 opponent).


Arduino Forum > Using Arduino > Programming Questions > Need help with implementing a delay for a minigame.


arduino

Comments