Arduino Project
Speed Test
Course Introduction
In this lesson, you’ll use an OLED display, an LED, and a button with the Arduino R4 UNO to build a reaction time game.
When the LED lights up, players must press the button as quickly as possible. The OLED screen displays the reaction time, and pressing the button again starts a new round.
Note
If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.
1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE
Wiring

Common Connections:
OLED Display Module
SDA: Connect to A4 on the Arduino.
SCK: Connect to A5 on the Arduino.
GND: Connect to breadboard’s negative power bus.
VCC: Connect to breadboard’s red power bus.
Button
Connect to the breadboard’s negative power bus, and the other end to 2 on the Arduino board.
LED
Connect the LED cathode to the to a 220Ω resistor, then to negative power bus on the breadboard, anode to 3 on the Arduino.
Writing the Code
Note
You can copy this code into the Arduino IDE.
To install the library, use the Arduino Library Manager and search for Adafruit SSD1306 and Adafruit GFX and install it.
Don’t forget to select the board(Arduino UNO R4 Minima) and the correct port before clicking the Upload button.
#include
#include
#include
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int LED_PIN = 3; // LED positive to D3, negative -> resistor -> GND
const int BUTTON_PIN = 2; // Button one end to D2, the other to GND
bool waitingReaction = false;
bool roundFinished = false;
unsigned long ledOnTime = 0;
unsigned long reactionTime = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Internal pull-up, button pressed = LOW
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;) ; // Stop if OLED initialization fails
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(10, 20);
display.println("Reaction Game");
display.setCursor(10, 40);
display.println("Press to start");
display.display();
// Wait until button is pressed to start
while (digitalRead(BUTTON_PIN) == HIGH);
delay(200);
}
void loop() {
if (!roundFinished) {
// --- Show waiting message ---
digitalWrite(LED_PIN, LOW);
display.clearDisplay();
display.setCursor(0, 20);
display.println("Wait for LED...");
display.display();
delay(random(1000, 5000)); // Random delay 1~5 seconds
// --- Turn on LED and record time ---
digitalWrite(LED_PIN, HIGH);
ledOnTime = millis();
waitingReaction = true;
roundFinished = true;
}
// --- Wait for player to press the button after LED on ---
if (waitingReaction && digitalRead(BUTTON_PIN) == LOW) {
reactionTime = millis() - ledOnTime;
waitingReaction = false;
showResult();
delay(200); // debounce
}
// --- After result, wait for button press to start next round ---
if (!waitingReaction && roundFinished && digitalRead(BUTTON_PIN) == LOW) {
delay(200); // debounce
roundFinished = false; // Start new round
}
}
void showResult() {
digitalWrite(LED_PIN, LOW); // Turn off LED
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 20);
display.print("Time:");
display.print(reactionTime);
display.println("ms");
display.setTextSize(1);
display.setCursor(0, 50);
display.println("Press button again");
display.display();
}
