⭐⭐ Advanced beginner
⏱️ 25 minutes
Reaction Game
How fast are you? Wait until the LED turns on. Then press the button as fast as you can. The screen shows your time in milliseconds. Challenge your friends!
🧰 What do you need?
💡
1× LED (little light)
🔌
USB cable + Jumper wires🗺️ Wiring diagram
Follow the wires from the Arduino to each part. You can choose the wire colors yourself.
| From | To |
|---|---|
Arduino UNO · D2 |
Push button · 1 |
Push button · 2 |
Arduino UNO · GND |
Arduino UNO · D13 |
Resistor 220 Ω · 1 |
Resistor 220 Ω · 2 |
LED (little light) · ANODE |
LED (little light) · CATHODE |
Arduino UNO · GND |
Arduino UNO · A4 |
OLED screen 0.91" · SDA |
Arduino UNO · A5 |
OLED screen 0.91" · SCL |
Arduino UNO · 5V |
OLED screen 0.91" · VCC |
Arduino UNO · GND |
OLED screen 0.91" · GND |
🛠️ This is how you build it
- Place the pushbutton across the gap in the breadboard: one leg to pin 2, and a leg on the other side to GND.
- Put the LED with a 220 Ω resistor on pin 13. The short leg goes to GND.
- Connect the OLED screen: SDA to A4, SCL to A5, VCC to 5V and GND to GND.
💻 The code
// Reaction game
// Wait until the LED turns on and then press the button as fast as you can.
// The screen shows how fast you were!
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int BUTTON_PIN = 2; // Pushbutton on pin 2 (other side to GND)
const int LED_PIN = 13; // LED on pin 13 (through the resistor)
Adafruit_SSD1306 display(128, 32, &Wire, -1); // OLED: SDA = A4, SCL = A5
unsigned long bestTime = 0; // Your fastest time so far
// Put text big on the screen
void show(const __FlashStringHelper* top, unsigned long number) {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print(top);
if (number > 0) {
display.setTextSize(2);
display.setCursor(0, 14);
display.print(number);
display.print(F(" ms"));
}
display.display();
}
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
randomSeed(analogRead(A0)); // Makes really random wait times
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextColor(SSD1306_WHITE);
}
void loop() {
show(F("Press the button..."), 0);
while (digitalRead(BUTTON_PIN) == HIGH); // Wait until you press the button
while (digitalRead(BUTTON_PIN) == LOW); // ...and let go again
show(F("Ready? Watch LED!"), 0);
delay(random(2000, 5000)); // A secret wait time between 2 and 5 seconds
if (digitalRead(BUTTON_PIN) == LOW) { // Pressed too soon? Cheater!
show(F("Too soon! Again."), 0);
delay(2000);
return;
}
digitalWrite(LED_PIN, HIGH); // NOW!
unsigned long start = millis();
while (digitalRead(BUTTON_PIN) == HIGH); // Wait for your press
unsigned long time = millis() - start;
digitalWrite(LED_PIN, LOW);
if (bestTime == 0 || time < bestTime) {
bestTime = time;
show(F("NEW RECORD!"), time);
} else {
show(F("Your time:"), time);
}
delay(3000);
}
🧠 What does the code do?
- After you press the button, we wait a secret time between 2 and 5 seconds (
random()). - Then the LED turns on and we start a stopwatch with
millis(). - When you press, we work out how many milliseconds it took. We remember your best time.
🎯 Try this…
- Make it a game for two players, each with their own button
- Make a buzzer beep when there is a new record
- Play 5 rounds and then show your average time
🚀 Build this project with Codey: open codey.online/starter-kits/uno/projects/reaction-game and click MAKE THIS. Then the code and the wiring diagram are ready right away.
Codey Online is a product of OTRONIC