Create reaction game with random leds blink

Hello,

I want to write a game, where the 3 LEDs blink randomly, and if all of the 3 LEDs blink at the same time, the player need to hit a button. If the player hit the button at the correct time each level, the game gets 0.5seconds faster. If the player fail to hit the button, the game starts from the beginning. Hope you can provide me help

Thank you in advance

The Dark Knight

Hi err... Dark... Mr/Mrs/Ms Night.... Bruce?

What help do you need? Hardware or software? Can you ask some more specific questions? What components do you have and what have you achieved so far?

Paul

I need the software. I have all the hardware fixed together. I attached a picture with this post.

Nobody, who can help with the programming?

Yes, I/we can help. What do you have so far?

Can you post a schematic of your circuit? Your picture has spaghetti for wires!

If I understood your original message, the Arduino should choose a random on/off state for each of your 3 leds. The leds should then show this pattern for a length of time, perhaps 5 seconds? If all 3 leds are on and the button is pressed within that time, the player "scores a point". Another sequence is then displayed for 4.5 seconds. If the button is not pressed within the time, the time allowed is increased back to 5 seconds. Am I correct?

What happens if the player presses the button when less than 3 leds are on? Does the time go back to 5 seconds?

What happens when the allowed time gets to 0.5 seconds and the button is pressed? Or is that almost impossible?

How does the game record the highest score (i.e. shortest time) the player has achieved? Does the number of times that the player correctly presses the button give a higher score?

PaulRB:
Yes, I/we can help. What do you have so far?

Can you post a schematic of your circuit? Your picture has spaghetti for wires!

If I understood your original message, the Arduino should choose a random on/off state for each of your 3 leds. The leds should then show this pattern for a length of time, perhaps 5 seconds? If all 3 leds are on and the button is pressed within that time, the player "scores a point". Another sequence is then displayed for 4.5 seconds. If the button is not pressed within the time, the time allowed is increased back to 5 seconds. Am I correct?

What happens if the player presses the button when less than 3 leds are on? Does the time go back to 5 seconds?

What happens when the allowed time gets to 0.5 seconds and the button is pressed? Or is that almost impossible?

How does the game record the highest score (i.e. shortest time) the player has achieved? Does the number of times that the player correctly presses the button give a higher score?

The initial state is, that the three LEDs blink slow in a random order. But when the three LEDs blink at the same time, the player has to press the button at that moment. If the player pressed the button, when the three LEDs blinked, then "the game" goes to the next Level, so LEDs need to blink faster than the first time. So lets say we will make 4 Levels, in each Level the LED blink has to be faster. If the player fails to press the button, when the three LEDs blink at the same, "the game" goes back to the first Level. After the player reached LEvel 4 and wins, all three LEDs should blink together three times. Hope you can help me. All I have so far is this:

int ranNum;
int ranDel;
#define STATE_led0 0

void setup() {
randomSeed(analogRead(0));
current_state = STATE_led0;

// 3 LEDs als Output konfiguieren
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);

// Taste als Input konfiguieren
pinMode(5, INPUT);
}

void loop() {
switch(current_state)
{
//Generiert eine zufaellige Zahl zwischen 2 und 5
ranNum=random(2,5);
// Generiert zufällige Delays
ranDel=random(500,300);
//LEDs einschalten
digitalWrite(ranNum, HIGH);
delay(ranDel);
//LEDs ausschalten
digitalWrite(ranNum, LOW);
}

}

But it doesn't work!

Well, I guess it does nothing because you don't have the "switch case" correct. There are no "case" labels. Have a look here. I'm not sure why you need a switch case anyway.

Your current sketch will only light one led at once. You need to use random() three times to decide whether each led is on or not.

You seem to be choosing a random delay. But the delay should not be random, it needs to be long at first, but then reducing when the player presses the button correctly. You need a variable for this.

You will need to remove the delay(), because that will prevent your sketch from detecting the button press. Instead, your sketch should check the millis() function often to see if it is time to change the leds.

Does that help?

Please put any sketches you put in your replies with CODE TAGS. They don't just make the code look nice. Without the code tags, your sketch becomes difficult to read and may even be changed if it contains certain character sequences.

See if this works a little better:

#define LED1 2
#define LED2 3
#define LED3 4
#define SWTCH 4

unsigned long flashSpeed = 5000;
unsigned long lastUpdate;

int led1State, led2State, led3State;

void setup() {
  randomSeed(analogRead(0));
  
  // 3 LEDs als Output konfiguieren
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  
  // Taste als Input konfiguieren
  pinMode(SWTCH, INPUT);
  
  lastUpdate = millis();
}

void loop() {

  if (millis() - lastUpdate > flashSpeed) {

    if (random(1,2) == 2) {
      led1State = HIGH;
    }
    else {
      led1State = LOW;
    }
    digitalWrite(LED1, led1State);

    if (random(1,2) == 2) {
      led2State = HIGH;
    }
    else {
      led2State = LOW;
    }
    digitalWrite(LED2, led2State);

    if (random(1,2) == 2) {
      led3State = HIGH;
    }
    else {
      led3State = LOW;
    }
    digitalWrite(LED3, led3State);
    
    lastUpdate = millis();
  }   
}

PaulRB:
See if this works a little better:

#define LED1 2

#define LED2 3
#define LED3 4
#define SWTCH 4

unsigned long flashSpeed = 5000;
unsigned long lastUpdate;

int led1State, led2State, led3State;

void setup() {
  randomSeed(analogRead(0));
 
  // 3 LEDs als Output konfiguieren
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
 
  // Taste als Input konfiguieren
  pinMode(SWTCH, INPUT);
 
  lastUpdate = millis();
}

void loop() {

if (millis() - lastUpdate > flashSpeed) {

if (random(1,2) == 2) {
      led1State = HIGH;
    }
    else {
      led1State = LOW;
    }
    digitalWrite(LED1, led1State);

if (random(1,2) == 2) {
      led2State = HIGH;
    }
    else {
      led2State = LOW;
    }
    digitalWrite(LED2, led2State);

if (random(1,2) == 2) {
      led3State = HIGH;
    }
    else {
      led3State = LOW;
    }
    digitalWrite(LED3, led3State);
   
    lastUpdate = millis();
  } 
}

Hello, thanks. How do I now combine this with the state, that the three leds blink at same time?

Did you try running the sketch? The 3 leds should blink at the same time, randomly.

Yes I tried, but it doesn't work. PLS help :frowning:

If you took your car into a garage and said "it doesn't work" what do you think the mechanic would say?