reaction game

could some one please help us, we have to make a game with the arduino and now we don't know how to do the nest step. we now have that al the leds start tot blink when you push the button it belongs to and if you push it again the led turns off. we are using 4 leds and now we have to make them blink after each other with a pause between each led. so first A goes on then pause then B the pause then C then pause then D then pause then A again and on and on without using delay! so please can someone help us?
thank you!!!!

Fred&Gijs

this is our code now

const int buttonPin1 = 13;    // the number of the pushbutton pin
const int ledPin1 =5;       // the number of the LED pin
const int buttonPin2 = 10;    // the number of the pushbutton pin
const int ledPin2 =4;       // the number of the LED pin
const int buttonPin3 = 9;    // the number of the pushbutton pin
const int ledPin3 =3;       // the number of the LED pin
const int buttonPin4 = 8;    // the number of the pushbutton pin
const int ledPin4 =2;        // the number of the LED pin

// Variables will change:
int buttonState1;             // the current reading from the input pin
int lastButtonState1 = LOW;   // the previous reading from the input pin
int buttonState2;             // the current reading from the input pin
int lastButtonState2 = LOW;   // the previous reading from the input pin
int buttonState3;             // the current reading from the input pin
int lastButtonState3 = LOW;   // the previous reading from the input pin
int buttonState4;             // the current reading from the input pin
int lastButtonState4 = LOW;   // the previous reading from the input pin

long lastDebounceTime1 = 0;   // the last time the output pin was toggled
long debounceDelay1 = 50;     // the debounce time; increase if the output flickers
long lastDebounceTime2 = 0;   // the last time the output pin was toggled
long debounceDelay2 = 50;     // the debounce time; increase if the output flickers
long lastDebounceTime3 = 0;   // the last time the output pin was toggled
long debounceDelay3 = 50;     // the debounce time; increase if the output flickers
long lastDebounceTime4 = 0;   // the last time the output pin was toggled
long debounceDelay4 = 50;     // the debounce time; increase if the output flickers

int ledState1 = LOW;             // ledState used to set the LED
long previousMillis1 = 0;        // will store last time LED was updated
int ledState2 = LOW;             // ledState used to set the LED
long previousMillis2 = 0;        // will store last time LED was updated
int ledState3 = LOW;             // ledState used to set the LED
long previousMillis3 = 0;        // will store last time LED was updated
int ledState4 = LOW;             // ledState used to set the LED
long previousMillis4 = 0;        // will store last time LED was updated

int y = 0;
int a = 0;
int b = 0;
int c = 0;
int g = 0;

void setup()
{
  pinMode(buttonPin1, INPUT);
  pinMode(ledPin1, OUTPUT);

  digitalWrite(ledPin1, ledState1);
  digitalWrite(buttonPin1, buttonState1);

  pinMode(buttonPin2, INPUT);
  pinMode(ledPin2, OUTPUT);

  digitalWrite(ledPin2, ledState2);
  digitalWrite(buttonPin2, buttonState2);

  pinMode(buttonPin3, INPUT);
  pinMode(ledPin3, OUTPUT);

  digitalWrite(ledPin3, ledState3);
  digitalWrite(buttonPin3, buttonState3);

  pinMode(buttonPin4, INPUT);
  pinMode(ledPin4, OUTPUT);

  digitalWrite(ledPin4, ledState4);
  digitalWrite(buttonPin4, buttonState4);

  
}


void loop() 
{
  // lees de knop uit
  int reading1 = digitalRead(buttonPin1);

  if (reading1 != lastButtonState1)
  {
    lastDebounceTime1 = millis();
  } 
  
  
  // ga na of de knop is ingedrukt als hij langere tijd dezelfde waarde heeft
  if ((millis() - lastDebounceTime1) > debounceDelay1)
  {
    if (reading1 != buttonState1) 
    {
      buttonState1 = reading1;
      
      if (buttonState1 == HIGH) 
      {
       y++;
      }
    }
  }    


//led 1 knipperen
  if(y==0)
  {
    unsigned long currentMillis1 = millis();
 
    int f = 10; //Hz

    int interval = 1000/f; //sec
 
     if(currentMillis1 - previousMillis1 > interval) 
     {
        previousMillis1 = currentMillis1;   

        if (ledState1 == LOW)
        {
          ledState1 = HIGH;
        }
        else
        {
          ledState1 = LOW;
        }
     }
  }
  
  if(y==1)
  {
      ledState1 = LOW;   
  }
  
  if(y==2)
  {
    y=0;
  }

  digitalWrite(ledPin1, ledState1);

  lastButtonState1 = reading1;

  // lees de knop uit
  int reading2 = digitalRead(buttonPin2);

  if (reading2 != lastButtonState2)
  {
    lastDebounceTime2 = millis();
  } 
  
  
  // ga na of de knop is ingedrukt als hij langere tijd dezelfde waarde heeft
  if ((millis() - lastDebounceTime2) > debounceDelay2)
  {
    if (reading2 != buttonState2) 
    {
      buttonState2 = reading2;
      
      if (buttonState2 == HIGH) 
      {
       a++;
      }
    }
  }    


//led 2 knipperen
  if(a==0)
  {
    unsigned long currentMillis = millis();
 
    int f = 10; //Hz

    int interval = 1000/f; //sec
 
     if(currentMillis - previousMillis2 > interval) 
     {
        previousMillis2 = currentMillis;   

        if (ledState2 == LOW)
        {
          ledState2 = HIGH;
        }
        else
        {
          ledState2 = LOW;
        }
     }
  }
  
  if(a==1)
  {
      ledState2 = LOW;   
  }
  
  if(a==2)
  {
    a=0;
  }

  digitalWrite(ledPin2, ledState2);

  lastButtonState2 = reading2;

  // lees de knop uit
  int reading3 = digitalRead(buttonPin3);

  if (reading2 != lastButtonState2)
  {
    lastDebounceTime3 = millis();
  } 
  
  
  // ga na of de knop is ingedrukt als hij langere tijd dezelfde waarde heeft
  if ((millis() - lastDebounceTime3) > debounceDelay3)
  {
    if (reading3 != buttonState3) 
    {
      buttonState3 = reading3;
      
      if (buttonState3 == HIGH) 
      {
       b++;
      }
    }
  }    


//led 3 knipperen
  if(b==0)
  {
    unsigned long currentMillis = millis();
 
    int f = 10; //Hz

    int interval = 1000/f; //sec
 
     if(currentMillis - previousMillis3 > interval) 
     {
        previousMillis3 = currentMillis;   

        if (ledState3 == LOW)
        {
          ledState3 = HIGH;
        }
        else
        {
          ledState3 = LOW;
        }
     }
  }
  
  if(b==1)
  {
      ledState3 = LOW;   
  }
  
  if(b==2)
  {
    b=0;
  }

  digitalWrite(ledPin3, ledState3);

  lastButtonState3 = reading3;

  // lees de knop uit
  int reading4 = digitalRead(buttonPin4);

  if (reading4 != lastButtonState4)
  {
    lastDebounceTime4 = millis();
  } 
  
  
  // ga na of de knop is ingedrukt als hij langere tijd dezelfde waarde heeft
  if ((millis() - lastDebounceTime4) > debounceDelay4)
  {
    if (reading4 != buttonState4) 
    {
      buttonState4 = reading4;
      
      if (buttonState4 == HIGH) 
      {
       c++;
      }
    }
  }    


//led 4 knipperen
  if(c==0)
  {
    unsigned long currentMillis = millis();
 
    int f = 10; //Hz

    int interval = 1000/f; //sec
 
     if(currentMillis - previousMillis4 > interval) 
     {
        previousMillis4 = currentMillis;   

        if (ledState4 == LOW)
        {
          ledState4 = HIGH;
        }
        else
        {
          ledState4 = LOW;
        }
     }
  }
  
  if(c==1)
  {
      ledState4 = LOW;   
  }
  
  if(c==2)
  {
    c=0;
  }

  digitalWrite(ledPin4, ledState4);

  lastButtonState4 = reading4;
}

Please show the circuit diagram you are using. Your code doesn't make much sense to me.

Have a look at "blink without delay", and "several things at the same time".

From your description it's not clear whether you want to have multiple LEDs blink at the same time, and whether blinking also should stop after some time, even if the button was not pressed. But such details are up to you, require no different coding approach.

In general I'd use an array of struct's, which hold the state variables for every LED and button. Then you can implement a subroutine handleLED(ref), receiving the index of the current LED/button, or a reference to that struct. The subroutine determines from the referenced variables whether the LED should be off or blinking. When off, it will check a stored interval or random value for starting the next blinks. When blinking, it checks the blink interval and turns the LED on and off, and when the associated button is pressed it will report the reaction time, stop blinking, and set a random interval for the next start (if desired).

Then you can use a for loop inside loop(), that calls handleLED(i) for all LEDs.

You can use random() for the random activation of a LED. Call randomSeed(seed) once, in setup(), to prevent identical sequences after every reset. Eventually increment and store the last used seed in the EEPROM, or use the time-of-day of a RTC for the seed.

You seem to have wriiten your code in a linear fashion - all the stuff for button A, then all the stuff for button B etc.

If you look at how the code is organized in Planning and Implementing a Program you will notice that is organized by function (as well as in functions). That is, all the buttons are read, then other stuff is done. That should allow you to remove a lot of duplication. For example a single debounce interval will do for all the buttons. And by reading all the buttons at nearly the same moment you get a better sense of who pressed a button first.

If you need fast reactions it might be better to design the code so it does not need a debounce interval.

I'm not really sure how you want your program to work - especially how the buttons are intended to interact with each other - or not.

...R