3LED Timer Reaction Game Help?

Hey guys, brand new to Arduino and the language, just checking it out so far…

I wanted to set up a game where, with a 3-color LED, where if you pressed one button (the start button) on, it turned each color on randomly for a random amount of time. And if you pressed the other pushbutton, a stop button, when the 3-color LED was blue, you essentially won the game (a “win” is qualified as pressing the button in 300 milliseconds or less after the LED turned blue). Otherwise, you lost the game. I’ve tested it out and the game works as is, the only problem is I’m wondering how I can create a script that doesn’t just “start” the timer for the first time the LED turns blue and none after that, i.e. I want the timer to start every time the blue LED is on and that’s it. Sorry if that’s confusing, basically I just don’t want it to stop working if, say, theoretically the blue LED was on and someone just didn’t press the button at all. Anyway, here’s the code:

unsigned long start, finished, elapsed;
int randNumber = 0;
int led[] = {5, 6, 7};
int startbutton = 9;
int stopbutton = 2;
int pinCount = 3;
int thisPin;

void setup()
{
  Serial.begin(9600);
  pinMode (8, OUTPUT);
  pinMode (2, INPUT);
  pinMode (9, INPUT);
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode (led[thisPin], OUTPUT);
  }
}

void loop()
{
  if (digitalRead(9) == HIGH){
    for (int i = 0; i<= random(10, 20); i++) {
      thisPin = random (pinCount);
      digitalWrite (led[thisPin], HIGH); 
      void blue();
      delay (random (200, 500));
      digitalWrite (led[thisPin], LOW);
      
    }
  }
  if (digitalRead (2) == HIGH){
    finished = millis();
    wincheck();
  }
    
}

void blue (){
  if (led [2] == HIGH){
    start = millis();
  }
}
    
  
void wincheck() {
  elapsed = finished-start;
  Serial.println("Raw elapsed time (ms): ");
  Serial.print(elapsed);
  Serial.println();
  if (elapsed < 300 && led [2] == HIGH){
    Serial.println ("you won!");
  }
  else {
    Serial.println ("you lost...");
  }
}

I think this is your problem:

  if (led [2] == HIGH){

Try this:

  if (digitalRead(led [2]) == HIGH){

Sorry for the late reply, but I needed to update on my new problem. The script is doing what I want it to do, but it’s not calling the wincheck script when I want it to (when I press the stop button). The serial print monitor doesn’t print anything at all, whether I press the stop button or not. In fact, the stop button doesn’t seem to work at all, as the game keeps going even after I press it. Here is the current script below:

unsigned long start, finished, elapsed;
unsigned long time;
int randNumber = 0;
int led[] = {5, 6, 7};
int startbutton = 9;
int stopbutton = 2;
int pinCount = 3;
int thisPin;

void setup()
{
  Serial.begin(9600);
  pinMode (8, OUTPUT);
  pinMode (2, INPUT);
  pinMode (9, INPUT);
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode (led[thisPin], OUTPUT);
  }
}

void loop()
{
  if (digitalRead(9) == HIGH){
    for (int i = 0; i<= 100; i++) {
      time = random(1000, 10000);
      thisPin = random (pinCount);
      digitalWrite (led[thisPin], HIGH); 
      blue();
      delay (350);
      digitalWrite (led[thisPin], LOW);
      delay (time);
      
    }
  }
  else{
    digitalWrite(led[0, 1, 2], LOW);
  }
  if (digitalRead (2) == HIGH){
    finished = millis();
    wincheck(); 
  }
    
}

void blue (){
  if (digitalRead(led [2]) == HIGH){
    start = millis();
  }
}
    
  
void wincheck() {
  elapsed = finished-start;
  Serial.println("Raw elapsed time (ms): ");
  Serial.print(elapsed);
  Serial.println();
  if (digitalRead (2) == HIGH && (led [1] == HIGH || led [0] == HIGH)){
    Serial.println ("Wrong light");
  }
  if (elapsed < 300 && led [2] == HIGH){
    Serial.println ("You won!");
  }
  else {
    Serial.println ("Too slow!");
  }
}

hi,

I see a few issues in your sketch: 1. insert s delay after serial.begin, as it takes a moment for the chip to adapt

  1. make you pulldown the input with an external resistor or at least with an chip internal one

  2. led[0,1,2] results in a multi dimension array access with unknown result

  3. in wincheck all the tests on led[?] will fail as you access the predefined number which is never high

  4. there is a logical glitch in the program, he loop will toggle the leds and then it waits for a random time. At this point in time you lost already (delay is blocking). Finally it runs through the last if which will last a few micro seconds.

so first I would fix these points and check what happens. Another problem you might encounter is debouncing....

Well, I expect the reason you’re not getting anything from the serial monitor is that it is outside the for loop.

    for (int i = 0; i<= 100; i++) {

It is going to do the stuff between the curly braces 101 times.

      time = random(1000, 10000);
      thisPin = random (pinCount);
      digitalWrite (led[thisPin], HIGH); 
      blue();
      delay (350);
      digitalWrite (led[thisPin], LOW);
      delay (time);
      
    }

There isn’t anything in there that does any Serial.print() stuff.
This is what’s going to happen when you push the start button:
1 ) choose a random time between 1 and 10 seconds
2 ) choose a random LED
3 ) turn the LED on
4 ) check to see if it was the blue LED. If so, set the start time
5 ) wait for a third of a second
6 ) turn off the LED
7 ) delay the random amount you chose in step 1

It’s going to cycle through those 7 steps 101 times. THEN it’s going to check to see if you happen to be pushing the stop button.