reaction game

hi everyone,

we are trying to make a reaction/drinking game with the arduino for 4 players.
the game is simple: 4 leds blink after each other and everyone has a button linked to your own led.
the rules of the game are that when your led blinks 2 times in a row you have to press the button. if you are too late the other 3 leds turn on and you have to drink but is you press on time the game goes faster. so we need a randomizer and a way to speed up the leds. we also need a reset button so the game can start over.

Cheers and thanx in advance!!!

Fred&Gijs

post the code you have so far.

have you been able to get the LED's to blink in sequence ?

although most randomizers are not truly random, you could take the last 2 digits from millis() from the press of the last button.

this is a picture of how the game looks!! sorry if it looks a bit messy

Please modify your reply to put your code in code tags (use the code button </>) so it looks like this and can easily be selected and copied to a text editor.

…R

// this is what we have so far but we have no clue on how to continu//


const int buttonPin1 = 2;    // the number of the pushbutton pin
const int ledPin1 =13;       // the number of the LED pin
const int buttonPin2 = 3;    // the number of the pushbutton pin
const int ledPin2 =11;       // the number of the LED pin
const int buttonPin3 = 4;    // the number of the pushbutton pin
const int ledPin3 =10;       // the number of the LED pin
const int buttonPin4 = 5;    // the number of the pushbutton pin
const int ledPin4 =9;        // 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;

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;
}
/code]

Have a look at the way the code is organized in planning and implementing a program. It is a collection of small functions that can each be developed and tested separately.

If you have a single function to read all the buttons you only need one debounce routine (but perhaps none is needed). And if you want to know who is first it is essential to read all the buttons with a minimum of time between the readings. You could actually read them all at the same time with port manipulation

You should do all your timing based on a single value of currentMillis taken at the start of loop().

...R