Maker.io main logo

Build a Reaction Time Mini-game Using an Arduino

2021-11-04 | By Maker.io Staff

License: See Original Project

This article outlines building an Arduino-based reaction tester mini-game. The finished product contains a button, an LED, and an LC display. Once the user starts the game, the Arduino chooses a random delay. After that delay, the Arduino turns on the LED. The user then has to press the button as fast as possible. The Arduino measures the time that passes between turning on the LED and the user reacting. It then shows the result on the display and stores the high score. This mini-game is a fantastic beginner project and a fun way of training your reaction skills.

BOM

You’ll need the following components if you want to build this project:

Product/Pcs./Where to buy

In addition to these components, you’ll also need a soldering iron and some solder. Furthermore, I recommend you take a look at this introductory article if you’re new to soldering.

Assembling the Electronics

This simple project only requires a few simple components, and it’s quite suitable for beginners. The most complicated part of this project is the LCD. This component requires you to connect a few wires to the Arduino, which might look overwhelming at first:

 

Connect the LCD, the push button, the LED, the potentiometer, and the resistors as shown in this schematic diagram.

First, note how the LCD has two pairs of 5V and GND supply pins. One supplies the display’s logic circuit, and the other pair provides power to the backlight. In addition, you must add the trimmer or tie the B-pin of the LCD to GND. Otherwise, the display might remain blank. The LCD linked above comes with a potentiometer, and I recommend you use it so that you can set the display’s contrast value to suit your needs. When assembled on a breadboard, the finished product looks like this:

finished_3

The finished product when assembled on a solderless breadboard.

The Reflex-Testing Software

The software part of this project is also beginner-friendly and should be easy to understand. At its core, the software works by detecting when the user presses the push-button. It does this by utilizing a custom interrupt service routine, also called an ISR. Without going into too much detail, an ISR is a function that the Arduino calls whenever a certain event happens. In this project, that is whenever the user presses the push button. Doing so pulls the Arduino’s digital I/O pin two from LOW to HIGH. The Arduino detects this change and automatically calls the ISR whenever that event occurs. The ISR function of this program looks like this:

Copy Code
void buttonPressed(void)
{
if(millis() - lastInterrupt > DEBOUNCE_TIME)
{
// React according to the current program state
if(mode == 0)
startGame();
else if(mode == 2)
endGame();
else if(mode == 3)
mode = 0;

lastInterrupt = millis();
}
}

Besides switching states, the ISR also checks whether enough time has elapsed since the Arduino last detected a button press. The program ignores rapid button presses as a de-bounce measure. Doing so is necessary to prevent the program from erroneously switching states due to mechanical imperfections in the button.

In addition to that ISR, the Arduino stores an internal program state. Depending on the state, a button press has a different effect:

state_4

This simplified flow diagram shows the different program states and how the program switches between them.

As you can see, the program starts in mode zero, where it only displays a short status message. When the user presses the push button, the program begins a new game and switches to state one. During this phase, the application determines a random wait time. While in state one, the program constantly checks whether the previously selected wait time has elapsed. If that’s the case, the Arduino enables the external LED to let the user know that it’s time to react. Once the user reacts, the program records the reaction time, disables the LED, and switches to mode three. In mode three, the program displays the most recent result, and the previously stored high score. If the new reaction time is less than the high score, the firmware updates the high score. Once the user presses the push button another time, the device goes back to mode zero.

Besides a few short helper methods and the ISR function, the loop()-function contains most of the core logic:

Copy Code
void loop()
{
long current = millis();

if(current - lastDisplayRefresh > DISP_REFRESH_INTERVAL)
{
printStatus();
lastDisplayRefresh = current;
}

if(mode == 1 && (millis() >= gameStartTime randomWaitTime))
{
digitalWrite(LED_PIN, HIGH);
mode = 2;
}
}

In this program, the loop()-function is quite short as it only checks whether the Arduino needs to refresh the contents of the display and whether the randomly picked wait time has elapsed when the program is in mode one. Once the wait time elapses, the program turns on the LED and switches to mode two.

Download the Source Code

You can download the source code for this project here.

Summary

tester_5

The reaction-tester displays this message when it’s waiting for the user to start a game.

This Arduino-based reaction tester mini-game is a fun way of testing how fast your reflexes are. You’ll only need a few cost-effective components for this beginner-friendly project, and you can completely assemble it on a solderless breadboard. The firmware for this project is just as simple. It relies on keeping an internal state that the user can change by pressing the push button. The Arduino detects button presses by utilizing a custom interrupt service routine. This ISR listens for the button pin to switch from LOW to HIGH. Whenever that happens, the program checks which mode it is in, and then it acts accordingly.

制造商零件编号 A000066
ARDUINO UNO R3 ATMEGA328P BOARD
Arduino
制造商零件编号 MJTP1230
SWITCH TACTILE SPST-NO 0.05A 12V
APEM Inc.
制造商零件编号 C503B-RCN-CW0Z0AA1
LED RED CLEAR 5MM ROUND T/H
Cree LED
制造商零件编号 CF14JT150R
RES 150 OHM 5% 1/4W AXIAL
Stackpole Electronics Inc
制造商零件编号 CF14JT2K20
RES 2.2K OHM 5% 1/4W AXIAL
Stackpole Electronics Inc
制造商零件编号 1957
JUMPER WIRE M TO M 6" 28AWG
Adafruit Industries LLC
制造商零件编号 MIKROE-1097
BREADBOARD TERMINAL STRIP
MikroElektronika
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum