Reaction-Time Game (Arduino)
Wait for the LED, then hit the button as fast as you can — the Arduino times you in milliseconds.
Beginner20 min
What you need
Wiring
Hover a pin or a wire to trace the connection.
Code
reaction_game.ino
// Measure your reaction time: press the button when the LED lights.
const int ledPin = 13;
const int btnPin = 2;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(btnPin, INPUT_PULLUP);
Serial.begin(9600);
randomSeed(analogRead(A0));
}
void loop() {
Serial.println("Get ready...");
digitalWrite(ledPin, LOW);
delay(random(2000, 5000)); // random wait so you can't cheat
digitalWrite(ledPin, HIGH); // GO!
unsigned long start = millis();
while (digitalRead(btnPin) == HIGH) { /* wait for press */ }
unsigned long reaction = millis() - start;
digitalWrite(ledPin, LOW);
Serial.print("Reaction: ");
Serial.print(reaction);
Serial.println(" ms");
delay(3000);
}
Steps
- Wire the LED on D13 (through 220Ω to GND) and the button on D2 to GND.
- Upload and open the Serial Monitor.
- When the LED lights, press the button as fast as you can — your time prints in ms.
- Don't jump early — the wait is randomized.