void setup() {const int ledPin = 8;
const int buttonPin = 2;
bool ledOn = false;
unsigned long ledOnTime;
unsigned long reactionTime;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
randomSeed(analogRead(0)); // Better random
Serial.println("Get ready...");
delay(1000);
}
void loop() {
// Wait for a random delay (1-5 seconds)
unsigned long waitTime = random(1000, 5000);
delay(waitTime);
// Light up LED
digitalWrite(ledPin, HIGH);
ledOnTime = millis();
ledOn = true;
// Wait for button press
while (ledOn) {
if (digitalRead(buttonPin) == HIGH) {
reactionTime = millis() - ledOnTime;
digitalWrite(ledPin, LOW);
ledOn = false;
Serial.print("Your reaction time: ");
Serial.print(reactionTime);
Serial.println(" ms");
delay(2000); // Pause before next round
Serial.println("Get ready...");
}
}
}
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}