Reaction timer
Arduino C++Build a reaction-timer game on the board you just built. The LED on pin 13 lights after a random 1 to 3 s; the player presses the button on D2 (wired to GND) as fast as they can, and the time from the LED to the press prints as, for example, Reaction: 312 ms. A press before the LED prints Too soon!. After every press the LED goes out and a new round starts. Catch the press with an interrupt on D2, debounce it, and time everything with millis(), never delay().
Checks
- Not run yetUses attachInterrupt, INPUT_PULLUP and millis(), and never delay()
- Not run yetThe LED stays dark at first
- Not run yetThe LED lights within 3 s
- Not run yetA press puts the LED out at once and starts a new round
- Not run yetPrints the reaction time, for example Reaction: 312 ms
- Not run yetA press before the LED prints Too soon!
Hints
- attachInterrupt(digitalPinToInterrupt(2), onFall, FALLING) runs onFall the moment D2 falls. Keep the interrupt routine short: note millis() in a volatile variable and set a flag.
- Contacts bounce for a few milliseconds when pressed and again when released. Count an edge only if the pin still reads LOW 20 ms later.
- No delay(): store when the LED should light, millis() + random(1000, 3001), and compare in loop() with (long)(millis() - goAt) >= 0, which survives the millis() roll-over.
- Copy what the interrupt writes with interrupts off (noInterrupts() … interrupts()): an unsigned long is 4 bytes, and the AVR reads it one byte at a time.