LED Chaser Reaction Game With Arduino and LCD
by Neeraj N in Circuits > Arduino
6 Views, 1 Favorites, 0 Comments
LED Chaser Reaction Game With Arduino and LCD
This interactive reflex game for Arduino is known as the LED Chaser Reaction Game. It allows players to improve their reaction time and hand-eye coordination by using a combination of three standard LEDs (green, yellow, and red) and a blue channel from an RGB LED to form a repeating chaser sequence. An LCD display alerts players to the color they should be watching for, and at the moment that the LED lights up, players are to press the button with as much speed as they can. The Arduino measures the amount of time it takes for the player to react and will display their reaction time in milliseconds.
The project has demonstrated how Hub Controllers can merge digital outputs (LEDs, buzzer), analog inputs (potentiometer), as well as HMI (Human-Machine Interface) elements (buttons, LCD) into a single system. The game implements timing functions through the use of millis(), logical comparison with if statements, and visual and audible feedback. To increase or decrease the difficulty, players can control the speed of the chaser LED sequence with the potentiometer. This demonstrates how to link analog signals to control dynamic difficulty.
To summarise, this project combines all three aspects of electronics, programming logic and video gaming design into one fun to play project. In this project, you will see how the code's structure defines what is being done by certain variables (which hold information) and by functions (which represent what does what), thus creating a working interactive system that can respond instantaneously after receiving some sort of human input.
Supplies
The game which be made has the following main hardware components used for development:
- Microcontroller (Arduino Uno) (1) - The main controller for all components and to execute the game's logic.
- Display (LCD 16x2 via I2C) (1) - This display will inform players of their target color, how long they were able to react before pressing the button, and any messages that will assist them during gameplay.
- RGB LED (Common Anode) (1) - To provide background colors representing a more complex visual challenge for players.
- Standard LED Lights (3 any colors) - Further provide simple visual indications for cycling through the sequence.
- User Input (Push Button) (1) - The player will use this device to indicate that they have responded as soon as the target color is displayed.
- Buzzer (Sound Output) (1) - This device will sound an audible indication of success when a player correctly presses a button in response to the target color.
- Potentiometer (Speed Control) (1) - The player uses this device to modify how quickly the target color will appear in succession; therefore, controlling how difficult it is for them to react.
- Resistors (330Ω & 10K) - To prevent excessive current flow to both the standard LEDs and the RGB LED.
- Breadboard/Jumper Wires - For all components to be connected together in an organized manner, and securely disconnected as needed.
Green, Yellow & Red LEDs – Visual Chaser Sequence
Players must pay close attention to the sequence of colors displayed by the three standard LEDs, with each LED representing one of the different colors that could be targeted during the game, with each color lighting in succession and repeating, giving the player the ability to stay focused and quickly respond when they see the right color displayed. The use of different colored LEDs also supports making it easier for players to visually understand what was going on because of the visual contrast among the colors between the three LEDs; if there were no LED lights, there would be nothing visual the players could respond to.
Here’s how you can hook them up:
- Green LED Anode→ Arduino Pin 13
- Yellow LED Anode → Arduino Pin 12
- Red LED Anode → Arduino Pin 11
- All LED cathodes → Connect to Ground with resistor.
IMPORTANT NOTE: Each LED will require an individual resistor in order to protect both the LED and the Arduino pin. The safest option for the resistor is about 220 Ω.
RGB LED – Blue Target Color
A fourth color (Blue) is added to the game using an RGB LED. This makes gameplay more difficult and fun since now there are four different options the player has to react to, instead of just three. The use of an RGB LED allows the Arduino to create different colors using only one component (instead of needing multiple separate LEDs). While in this game, only the blue channel is being used; however, the wiring allows for complete color control in the future as the game evolves. Adding an RGB LED adds further depth to the game and provides an improved experience visually.
Wiring can be done as follows:
- Common anode to 5V rail
- Red pin to Resistor to Arduino Pin 10
- Green pin to Resistor to Arduino Pin 9
- Blue pin to Resistor to Arduino Pin 6
NOTE: Since this is a common anode RGB LED, colors will turn on when the Arduino pin goes LOW. If you have a common cathode LED you will have to modify the code a little bit.
Push Button – Player Reaction Input
Using the push button to play the game gives the player the opportunity to react when they see the color that corresponds with the target on the board light up by pressing down on the button. The Arduino monitors the button continuously so it knows if there has been a response from the player. Without a way for the player to interactively play the game through pressing a button there would not be any way for the player to measure their reaction time. By pressing the push button it turns your circuit from merely displaying the target color on an LCD screen to being able to truly interact with it as well as inputting/outputting data.
Here's how the connection of the push button should look like:
- Connect the push button to the centre gap of the bread board.
- One terminal of the push button should connect to the Arduino pin 2.
- The other terminal will connect to the 5V rail of the breadboard.
- On the same terminal as the 5V rail, a resistor (10K ohm) will be connected to GND.
Note: This configuration will ensure that the Arduino will read LOW when not pressed and it will read HIGH when pressed.
Buzzer – Sound Feedback
The player receives audio feedback in the form of a buzzer, helping to create an interactive experience with the game. The buzzer produces a successful “rising” tone when the player presses the corresponding button at the right time and a “falling” tone when the button is pressed at an unintended moment. This audio feedback allows for immediate feedback for whether or not the player pressed the button at an appropriate time, instead of just referring to an on-screen display. This adds to the “arcade feel” of an action reaction game.
Buzzer Wiring Configuration:
- The positive Buzzer to Pin #3 on Arduino
- The negative Buzzer to Ground Rail
Note: The buzzer must be controlled by the Arduino, and cannot be hard-wired to 5V.
Potentiometer – Difficulty Control
The potentiometer allows players to control the rate of changing of the LEDs. If you turn the knob in one direction, the lights will change at a slow rate making it much easier. If you turn it in the opposite direction, the lights will quickly change which makes it even harder! This allows for each gamer to determine their own level of difficulty rather than being stuck with a pre-set speed. Additionally, it demonstrates how an analog input can have an effect on a digital system in real-time.
Wiring the potentiometer can be done as follows:
- Left pin goes to Ground Rail;
- Middle Pin goes to Arduino A0 pin;
- Right Pin goes to 5V Rail.
Note: It is imperative that the middle pin to go to an analog input so that the Arduino may read the changing values.
LCD Display – Game Instructions & Feedback
The primary method through which the game interacts with the player is through the LCD screen. The LCD will display the game title at the beginning, show the player which colour they need to look for and finally show the player if they were correct or incorrect along with their reaction time. If there was no LCD screen, the player wouldn't know what colour they were supposed to target, making the game appear more random instead of being skill based. The LCD keeps track of the player's score and encourages the player to become faster and better with each round. The inclusion of a screen also changes the game from only having blinking lights to becoming an actual interactive game with defined rules and feedback.
The following shows how to connect a 16x2 LCD to your Arduino:
- GND connects to the breadboard ground
- VCC connects to the 5V
- SDA connects to the A4 on an Arduino
- SCL connects to the A5 on an Arduino
Note: Since the LCD communicates over I2C, make sure that the SDA and SCL are connected to the proper pins; otherwise, everything will be blank.
Arduino Code – Game Logic & Control
The Arduino code below demonstrates how we built an LED Chaser Reaction Game that consists of many different stages related to controlling the LEDs, reading the buttons, measuring the player's reaction time, updating the LCD with the player's score, and controlling the motor speeds with a potentiometer.
First, the code specifies which pins on the Arduino provide the display information to the LCD screen. The code also configures all of the LED pins for controlling and lighting the LED path of the LED chaser game (individual RGB channels) as well as for controlling the LED chaser motor speed (with the use of a potentiometer). Next, the code randomly selects a target colour for the player to watch for and displays that colour name on the LCD.
The main loop of the code continuously checks the potentiometer for how fast to light up the LED chaser; after that, it lights up the next LED one-by-one until the last LED turns on. Once the target colour is illuminated, the program begins timing using the millis() function. If the player presses the button while the target colour is lit, the code calculates the reaction time, adds to the score, displays the result to the player on the LCD, and lets the player hear the winning sound using the buzzer as well as providing the appropriate sound for losing if the player presses the button at the wrong time. After each player's turn, the code reselects another target colour and resumes play.