help with this lab

So i’m working on this lab for class and i will paste it here

Reaction time game. Connect an LED and a Pushbutton to two different pins. When the program starts, turn on the LED for a random time between 3 and 5 seconds. When the LED goes off, start timing in milliseconds. When the player presses the pushbutton stop timing and display his reaction time on the screen in exactly the format below where “X” is the number of milliseconds:

Now modify your program to print “You cheated” if the player pressed the button before the light went out and print the previous “Your reaction time is X milliseconds” if they didn’t cheat. Hint, no one is faster than 150ms, so if it’s less than that, then they cheated.

Now i have the code and i will paste it here as well.

int LED = 12;

int BUTTON = 7;

void setup() {

// set the baud rate for Serial monitor


// set the pin modes

pinMode(LED, OUTPUT);


digitalWrite(LED, HIGH); // turn on the LED

delay(1000 * random(3, 5)); // sleep a random number of seconds between 3-5

unsigned long startTime = millis(); // start timing

while (digitalRead(BUTTON) == HIGH); // the button is in HIGH state when not pressed

unsigned long endTime = millis(); // stop timing

Serial.print("Your reaction time is: ");

Serial.println(endTime - startTime);

if (endTime - startTime <= 150)

Serial.println(“You cheated!”);


void loop() {


Now the issue i’m having is that the LED will turn on for 3 to 5 seconds and it will turn off, but when i press the push button it won’t display the time or anything like that.

Please any advice will help a ton

Thank you in advance

What is keeping the button input HIGH when it is not pressed ?
Your code never seems to turn the LED off

Additionally, you need to attach the Serial Monitor. Depending of the board everything could be over by the time you connected the Serial Monitor.

You can use the following line for the program to wait until the Serial Monitor is running.


Also I would place the code after the pin initialization in the loop function, so you can do the test over and over again.

I noted you don't have INPUT_PULLUP specified when you configure your button input. How is your button wired? It looks like you expect a LOW to be button "pressed". This implies your button is connected to GND in which case you need to specify INPUT_PULLUP or need an external pullup resistor.