Reaction Game Box 1v1
In this instructable I'll be taking you through the steps to make a simple 2 player game. Both players wait until the green light lights up and whoever hits their respective button first wins!
I started this project for an arduino class after having ruined my first idea involving a lot of blown up capacitors and a fried stepper motor. Nothing like that will keep us from making this fun little project though so let's begin!
Supplies
Necessary supplies:
- 2x 18mm metal pushbuttons (or any other pushbuttons)
- 1x 12mm plastic pushbutton (or any other pushbutton)
- Arduino UNO
- Breadboard (for testing)
- Electrical wires
- 2x blue LEDs
- 1x red LED
- 1x yellow LED
- 1x green LED
- A 9V battery
- 5x 220 Ω resistors
- A box to put everything in
- A PCB prototype board
- Piezo buzzer
Necessary Tools:
- Soldering iron
- Electrical drill
- 5mm drill bit (keep in mind the material choice of your box for the drill bits)
- 16mm hole drill bit (for the metal pushbuttons, adjust for yours)
- 12mm hole drill bit (for the plastic pushbutton, adjust for yours)
- 3mm drill bit
Optional:
- Wire strippers (highly recommended)
- Button cell battery (for checking LEDs)
- Female header electrical wires
- Hotglue gun
Setting Up the Circuit
Shown is a basic diagram of our circuit. It is nothing to complicated but I would still advise you to first build this on your breadboard to test. You can ofcourse pick your own ports for attaching to the UNO. (I would probably recommend doing this since mine turned a bit into spaghetti)
Setting Up the Code
The code works with 3 boolean variables:
- gameOver : to check if the game is currently running and keeping the code from looping
- pressLive: the variable that dictates when the buttons can be pressed
- resetReady: when the reset button can be pressed
With that the beginning of our code looks like this:
#define rLED 3
#define yLED 5
#define gLED 6
#define p1LED 9
#define p2LED 10
#define BUZPIN 11
#define p1BUTTON 2
#define p2BUTTON 13
#define RESETBUTTON 8
bool gameOver = true;
bool pressLive = false;
bool resetReady = true;
void setup() {
Serial.begin(9600);
pinMode(rLED, OUTPUT);
pinMode(yLED, OUTPUT);
pinMode(gLED, OUTPUT);
pinMode(p1LED, OUTPUT);
pinMode(p2LED, OUTPUT);
pinMode(p1BUTTON, INPUT);
pinMode(p2BUTTON, INPUT);
pinMode(RESETBUTTON, INPUT);
digitalWrite(p1BUTTON, HIGH);
digitalWrite(p2BUTTON, HIGH);
digitalWrite(RESETBUTTON, HIGH);
}
One of the things we need is a function that makes our lights 'count down' to the green light and set the pressLive to true:
void trafficlight(int wait) {
tone(BUZPIN, 300, 100);
digitalWrite(rLED, HIGH);
delay(1000);
tone(BUZPIN, 300, 100);
digitalWrite(rLED, LOW);
digitalWrite(yLED, HIGH);
delay(wait);
tone(BUZPIN, 600, 100);
digitalWrite(yLED, LOW);
digitalWrite(gLED, HIGH);
pressLive = true;
}
2 minor functions I used are a function for the 'victory theme' (with thanks to https://youtu.be/iY98jcuKh5E) :
void victoryTheme() {
tone(BUZPIN, 523.25, 133);
delay(133);
tone(BUZPIN, 523.25, 133);
delay(133);
tone(BUZPIN, 523.25, 133);
delay(133);
tone(BUZPIN, 523.25, 400);
delay(400);
tone(BUZPIN, 415.30, 400);
delay(400);
tone(BUZPIN, 466.16, 400);
delay(400);
tone(BUZPIN, 523.25, 133);
delay(133);
delay(133);
tone(BUZPIN, 466.16, 133);
delay(133);
tone(BUZPIN, 523.25, 1200);
delay(1200);
}
And a light flicker function to show when the system can be reset:
void lightFlicker() {
digitalWrite(rLED, HIGH);
digitalWrite(yLED, HIGH);
digitalWrite(gLED, HIGH);
delay(100);
digitalWrite(rLED, LOW);
digitalWrite(yLED, LOW);
digitalWrite(gLED, LOW);
delay(100);
digitalWrite(rLED, HIGH);
digitalWrite(yLED, HIGH);
digitalWrite(gLED, HIGH);
delay(100);
digitalWrite(rLED, LOW);
digitalWrite(yLED, LOW);
digitalWrite(gLED, LOW);
delay(100);
digitalWrite(rLED, HIGH);
digitalWrite(yLED, HIGH);
digitalWrite(gLED, HIGH);
delay(100);
digitalWrite(rLED, LOW);
digitalWrite(yLED, LOW);
digitalWrite(gLED, LOW);
}
Now we just have to write the logic to start the game:
if (gameOver == false) {
trafficlight(random(1000, 10000));
gameOver = true;
}
The logic for the player buttons being pressed while pressLive is true:
if (pressLive == true && digitalRead(p1BUTTON) == LOW) {
pressLive = false;
digitalWrite(p1LED, HIGH);
delay(50);
digitalWrite(p1LED, LOW);
delay(50);
digitalWrite(p1LED, HIGH);
delay(50);
digitalWrite(p1LED, LOW);
delay(50);
digitalWrite(p1LED, HIGH);
delay(500);
victoryTheme();
lightFlicker();
resetReady = true;
}
if (pressLive == true && digitalRead(p2BUTTON) == LOW) {
pressLive = false;
digitalWrite(p2LED, HIGH);
delay(50);
digitalWrite(p2LED, LOW);
delay(50);
digitalWrite(p2LED, HIGH);
delay(50);
digitalWrite(p2LED, LOW);
delay(50);
digitalWrite(p2LED, HIGH);
delay(500);
victoryTheme();
lightFlicker();
resetReady = true;
}
And finally we just have to code the reset:
if (resetReady && digitalRead(RESETBUTTON) == LOW) {
digitalWrite(p1LED, LOW);
digitalWrite(p2LED, LOW);
delay(500);
gameOver = false;
resetReady = false;
}
After feedback it was brought to my attention that the way I'm using the random in the start up code isn't really random. It will be the same sequence every time the UNO is reset. So to alleviate this the following code is added in the setup():
randomSeed(analogRead(0));
Downloads
Testing
With all the coding out of the way now is the time to test the system for any faults. This is one of the steps where it is still easy to adjust or fix parts. The further we go the harder it will become so test early and test often!
In the video you can see the early prototype version I made, completely on a breadboard.
Housing
Now is the time to get your box of choice and start marking out the hole locations. We'd rather measure twice and cut once than the other way around! So take your time and make a layout that you find pleasing.
Next I used Spade bits to drill out the big holes for the buttons and regular wood drill bits for the smaller holes. Test fit during this procedure, you'd rather the holes be a little snug than too big.
The LEDs need a 5mm hole to snuggly fit. This may very for non-standard LEDs.
you can use sanding paper to fix any rough edges left.
Soldering
I am very much a beginner when it comes to soldering so there is a lot left to be desired and improved but this is what I ended up doing.
Since a ton of connections need to go to ground I decided to use a PCB prototype board where I solder electrical wires with female headers together routing to one wire that will connect to ground. I really liked this solution since this way wires were never fully stuck connected. I could decouple and swap out faulty wires more easily. I did the same thing for the LEDs, here color coded by wire.
I ended up securing the wires with some hotglue to keep possible pulling tension away from the solderjoint.
Then it is pushing all of the spaghetti (I really should have trimmed the wires shorter) into the box and attaching it to all the components.
as the last piece you add the 9V battery to the arduino and voila! If you are able to close the box congratulations on finishing your project!
Play
Thank you for taking your time reading this instructable. Maybe you learned something or were amused by my spaghetti. (who could blame you)
Enjoy your tiny contraption to show off to all your friends how much more of a gamer you are with your lightning fast reflexes!