Introduction: Arduino Reflexes Game
We created a small Arduino based game that allows you to measure your reflexes. Simply, soon as you see the LED light turn on, you should press the button. Arduino measures your response time and prints it on the LCD display. It also keeps track of the best score so far (of course, it is reset every time the Arduino is reset).
All the needed hardware was included in Arduino Uno kit that I bought off eBay for about 20€.
Hardware used:
- Arduino Uno, or a similar
- LCD 16x2 character display
- Potentiometer for regulating the contrast
- Push button
- LED diode
- 220 ohm resistor
- Jumper wires
Step 1: Hardware
Connecting LCD character display to Arduino is well explained elsewhere, I was using this tutorial, among others:
https://learn.adafruit.com/character-lcds/wiring-a...
In short, data leads LCD <-> Arduino are as follows:
- RS <-> D7
- EN <-> D8
- DB4 <-> D9
- DB5 <-> D10
- DB6 <-> D11
- DB7 <-> D12
Push button is directly connected to Arduino (one pin to Ground, other to D3), using internal pull-up resistor (therefore using INPUT_PULLUP mode for the pin 3).
LED light is connected to Arduino pin D2 over a 220 ohm resistor.
Step 2: Software
The software is simple, it uses only LiquidCrystal library.
Arduino chooses a random pause between 1 and 3 seconds and turns on the LED once the pause has passed.
The user needs to press the button as quick as possible, and Arduino measures the time. It then displays the time using LCD display, and updates the high score if needed. Then the LED is turned off and another pause starts.
#include <LiquidCrystal.h> // initialize the library by associating any needed LCD interface pin // with the arduino pin number it is connected to const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12; const int buttonPin = 3; const int ledPin = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); bool waitingForLed = true; unsigned long startTime; unsigned long hiScore = 10000; unsigned long onTime; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); // set up the LCD's number of columns and rows: lcd.begin(16, 2); onTime = millis() + random(1000, 3000); } void processResult(unsigned long diffTime) { lcd.setCursor(0, 0); lcd.print(diffTime); lcd.print(" "); if (diffTime < hiScore) { hiScore = diffTime; lcd.print(" "); lcd.setCursor(0, 1); lcd.print("Hi score: "); lcd.print(hiScore); lcd.print(" "); } } void loop() { int buttonValue = digitalRead(buttonPin); if (buttonValue == LOW) { if (waitingForLed) { // "punish" the player for pressing the button before LED is on // effectivelly preventing the rapid clicking onTime += 1000; } else { digitalWrite(ledPin, LOW); unsigned long nowTime = millis(); unsigned long diffTime = nowTime - startTime; processResult(diffTime); onTime = millis() + random(1000, 3000); waitingForLed = true; } delay(100); } else { if (waitingForLed && (millis() > onTime)) { startTime = millis(); digitalWrite(ledPin, HIGH); waitingForLed = false; } } }
Step 3: Future Work
I'd try to reimplement the game using the 4 digit 7-segment display and using Arduino Pro Mini, because of the form factor, and pack it in own enclosure.