Quick Reaction Game: Distance Version

503

7

1

Introduction: Quick Reaction Game: Distance Version

Hi. This is an Instructable on how to create a game that tests both your reaction time and sense of distance. This project is based on an old project I did involving two players competing to see who had a quicker reaction time by clicking a button when a light turned green. This one has a similar purpose, except it's single-player and rather than a light going off, the player is given a time frame to distance their hand a certain space away from a distance sensor.

Like all Arduino projects, this game is going to require numerous electrical components in the Arduino circuit. The main components, other than the wiring and the Arduino itself, include the breadboard, a servo motor, an LCD display, an RGB LED, and a distance sensor.

Using https://abra-electronics.com, the price excluding the wires and Arduino is $32.12 CAD.

Step 1: Step 1: Distance Sensor

The first step is to setup the ultrasonic distance sensor on the breadboard and wire it to the Arduino. The exact position of the sensor doesn't actually matter, but ideally its close to an edge so that there's room for the other components, as shown in the picture above. There are four pins on the sensor; GND, VCC, TRIG, and ECHO. GND and VCC are to be wired into the ground and power rails respectively, and wire in the other two pins into two pins on the Arduino. The two pins I used were 12 for ECHO and 11 for TRIG. Use two other wires to power the power rail and ground the ground rail by connecting the power rail to the 5V pin and the ground rail to a GND pin.

Step 2: Step 2: Servo Motor

The next step is to setup the servo motor. In this project, the servo motor functions as a timer. It'll start at 1 degree, and over the period of time in which the user has to distance their hands, will rotate to 180 degrees. I used 2 seconds for when the user finds out how far they have to distance their hands, so the servo rotates 179 degrees over a 2 second period, rotating in short intervals. The servo motor has three wires; usually a yellow, a red, and a brown. The red one goes into the power rail which is already wired into 5V, and the brown one goes into the ground rail already wired into GND. The final wire plugs into an Arduino pin. I chose pin #9 for this one. Then, you need a capacitor connecting the same rail that has the servo motor's power and ground wires connected to, as seen in the picture above.

Step 3: Step 3: RGB LED

The function of the LED in this is to act as a scale for the score. When the player's score is around 0, the LED will be white, and will turn more red if the player's score goes down and green if the player's score goes up. This LED has four legs; a red-light leg, a blue-light leg, a green-light leg, and a common cathode shared between the other three legs. The common cathode, the longest leg, is wired into the power rail so it receives 5 volts. Attach 330 ohm resistors to the other three colour legs, and attach the other ends of those resistors to PWM digital pins on the Arduino. The ones I used were digital pins 3, 5, and 6 for the red, green, and blue legs respectively.

Step 4: Step 4: LCD

The final component is the LCD, which stands for liquid crystal display. The purpose of this is to tell the player their current score as well as the distance they need to put their hands away from the sensor. There are four pins here; GND, VCC, SDA, and SCL. GND and VCC will be wired into the ground and power rails of the breadboard respectively. The SDA pin has to be wired into the analog pin A4, and the SCL pin has to be wired into the analog pin A5. Unlike the other components, you must wire the SDA and SCL pins to A4 and A5.

Step 5: Step 5: the Code

Now that we've wired in all the components, we can write the code. The first part of the code is to import the necessary libraries and declare our variables and which pins the components are wired into. We need to import the Wire, LiquidCrystal_I2C, and Servo libraries for this code.

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

#include <Servo.h>

Servo myServo;

int const trigPin = 11;

int const echoPin = 12;

int redPin = 3;

int greenPin = 5;

int bluePin = 6;

int score = 0;

int tim = 500;

int current = random(8,16); //random value where user has to distance their hand away from sensor

LiquidCrystal_I2C lcd(0x27, 16, 2); //LCD setup

Now we need to use the void setup() to declare our pin types and setup other necessary components.

void setup() {
myServo.attach(9); Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); lcd.init(); lcd.backlight(); lcd.begin(16, 2); lcd.clear(); //LCD setup
}

Now we need to setup the RGB LED code using a function and PWM:

void setColor(int red, int green, int blue) {

red = 255 - red;

green = 255 - green;

blue = 255 - blue;

analogWrite(redPin, red);

analogWrite(greenPin, green);

analogWrite(bluePin, blue);

}

Now we need to add the void loop(). Here, we're going to be generating random integers and using a series of if statements to control the game for the player. The current variable, setup above, is for the current distance the player must distance themselves from the sensor.

Because the code in the void loop() is very long, I'm going to paste a link to a document that has that code:

https://docs.google.com/document/d/1DufS0wuX0N6gpv...

Finally, we need to do the actual calculations to convert the ultrasonic distance sensor's values to inches. The ultrasonic distance sensor doesn't directly measure distance; it releases sound and records the time it takes for the sensor to get the sound back from whatever object it bounces off of.

long microsecondsToInches(long microseconds) {

return microseconds / 74 / 2;

}

Now we plug the wired Arduino into the computer with the code, setup the ports, and run it! There are two modes to this game. Either you can only use the LCD display, servo motor, sensor, and the RGB LED and you only know the distance you have to be from the sensor, which is the harder mode. The easier mode involves using the serial monitor in Tools > Serial Monitor, which will update you every second on how far you are from the sensor, so you can make necessary adjustments.

Thanks for reading!

Share

    Recommendations

    • Fix It! Contest

      Fix It! Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Tiny Home Contest

      Tiny Home Contest

    Discussions