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
Serial.begin(9600);
// set the pin modes
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
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