Introduction: Arduino Reaction Timer

This is a fun and addictive game that tests your reaction speed.

Step 1: Materials

The materials list for this project is very simple:

  • Arduino (Uno or other)
  • breadboard
  • 3 x LED (1 red, 1 yellow, 1 green)
  • 3 x 220 resistor
  • 10k resistor
  • push button
  • wires
  • USB cable (A - B)
  • Arduino IDE

Step 2: Building the Circuit

This project only has a few very simple circuits:

  • 3 LED circuits
  • a push button circuit

The LED circuits

Each LED is connected to a digital port on the Arduino, the LEDs can be controlled by setting the digital ports to HIGH or LOW. Each LED is connected to a 220 resistor.

The push button circuit

The push button is connected to a digital port. When the button is not being pushed the digital port is connected to ground (LOW), when the button is being pushed the digital port is connected to 5V (HIGH). This circuit uses a 10k resistor.

Building the circuit

When building the circuit it is important that you leave enough space around the push button so it is easy to access.

Step 3: The Code

Here is the complete code without comments, it's a bit hard to read without indentation. The code with comments and indentation can be downloaded, it's included at the end of this section.

int ledRed = 12;

int ledYellow = 11;

int ledGreen = 10;

int button = 7;

int startTime = 0;

int endTime = 0;

int reactionTime = 0;

void setup() {

pinMode (ledRed, OUTPUT);

pinMode (ledYellow, OUTPUT);

pinMode (ledGreen, OUTPUT);

pinMode (button, INPUT);

}

void loop() {

digitalWrite (ledRed, LOW);

digitalWrite (ledYellow, LOW);

digitalWrite (ledGreen, LOW);

while (digitalRead (button) == 0) {

digitalWrite (ledRed, HIGH);

delay (50);

digitalWrite (ledRed, LOW);

delay (50);

}

while (digitalRead (button) == 1) { }

delay (random (1500, 6000));

digitalWrite (ledRed, HIGH);

digitalWrite (ledYellow, HIGH);

digitalWrite (ledGreen, HIGH);

startTime = millis ();

while (digitalRead (button) == 0) { }

endTime = millis ();

digitalWrite (ledRed, LOW);

digitalWrite (ledYellow, LOW);

digitalWrite (ledGreen, LOW);

delay (500);

digitalWrite (ledRed, HIGH);

delay (500);

digitalWrite (ledYellow, HIGH);

delay (500);

digitalWrite (ledGreen, HIGH);

delay (1000);

digitalWrite (ledRed, LOW);

digitalWrite (ledYellow, LOW);

digitalWrite (ledGreen, LOW);

reactionTime = endTime - startTime;

if (reactionTime < 180) {

digitalWrite (ledGreen, HIGH);

}

else{

if (reactionTime < 260) {

digitalWrite (ledYellow, HIGH);

}

else {

digitalWrite (ledRed, HIGH);

}

}

while (digitalRead (button) == 0) { }

while (digitalRead (button) == 1) { }

}

Step 4: Test It

Here's what should happen when the program starts:

  • The red LED flashes to indicate the game is ready to start
  • Push the button to start the game
  • All LEDs turn off and the program waits a random time (1.5 - 6 seconds)
  • All LEDs light up
  • Push the button as fast as possible
  • The LEDs turn off and light up again one by one
  • After 1 second 1 LED lights up
  • Green: fast reaction, yellow: average reaction, red: slow reaction
  • Push the button again to start over

Step 5: Enhancements

Make it cheat-proof

Right now you can easily cheat the game by just holding down the button. Can you change the code to prevent cheating? HINT: if the button is pressed when the random timer ends someone is cheating

More accurate results

You can provide a more accurate read-out of the reaction time:

  • use a display to show the actual reaction time
  • use a servo motor to point to an approximate reaction time