Introduction: Making a Reaction-based Game With an Arduino Due

What did we make?

This is a reaction game. The intention is that a LED will light up and that you as the player will have to press the corresponding button to that LED in time. When played with a LCD screen, every time you get a button right, you gain 5 points. Every game you start with 3 lives represented as 3 different colored LEDs on the side. If you don’t press the button in time, one of those LEDs will switch off. Once all the life LEDs are off, its Game Over.

Components and tools for the whole project(the image above is INCOMPLETE):

-Casing( made with a lasercutter(if wanted))


-LEDs

-Lives

-reaction leds

-----

-buttons/extender(made with a 3D-Printer)

-reaction buttons

-start button

-on/off button (optional)

-------

-LCD screen (optional)

-Piezzo Buzzer

-Assembly

- A big number of Jumping Wires(Male-Male and Female-Male)

-100 Ohm resistors and a few 5 Kilo-Ohm resistors

-Soldering Iron

-Breadboard(s)

Step 1: The LEDs and the Buttons

Let's start with the most important aspects. The LEDs ,meant to indictate both the lives and what button to press, and the buttons, that correspond with one specific LED and of course the start-button to begin a new game( and hopefully break your record!). Just use the drawing(source:gadgetronix) to build the base. Keep in mind that there are different kinds of resistors being used. Pay attention to what pins are used. You only need one pin for all the Gamebuttons. Of course you need a seperate pin for the starterbutton. But what is more important is that you set up the pins for the GameLEDs sequentially. For example with 3 GameLEDs you can use pins 3, 4 and 5 or pins 7,8 and 9 (but not pins 2,5 and 9) . You will later see that this is very important to make your project work. As for the lifeLEDs, the same rule counts. Just pick 3 sequential pins for the lives. After you finished recreating the image on a breadbord, you want to disconnect the buttons and LEDs from the breadbords(using Jumper Wires and the soldering iron as seen above). This way you can install them more easily in a separate casing. For the buttons you will need to make some extensions, as the breadbord on which the buttons are placed cannot be put right under the surface due to the wires. For making these extensions we used a 3D-printer(as seen in the image above)

Tip 1:after soldating the LEDs, it becomes very difficult to keep track of where the electricity enters(+) and where it leaves(-) the LED. We advise you to use tape so you when you install the LEDs on the breadboard, you know which side goes where.

Step 2: Optional: the LCD-IC2 Display

In the code there is an option for a LCD screen, but it(the LCD) is not mandatory for it(the reaction game) to work. Putting it together is fairly simple, you only need to connect the wires to it. For some LCDs this means more soldering, for others you only need some wires with a female end to connect it. After that the other end of the wire can be put directly into the correct pin of the arduino. When you want a LCD screen, make sure it works with the code. That means if you will be using our code what you will need is an LCD-IC2 Display with 16x2 dimensions. The LCD I2C might have a different adress(0x3F or 0x27). If your display doesn't seem to be working, this link might be able to help you with that:

https://www.ardumotive.com/i2clcden.html

Step 3: Optional: the Piezzo Buzzer and the On/Off Button

On/Off button

If you don’t want to open up the box everytime you want to switch on the arduino, a On/Off button is a good advise. It works by having a switch between the arduino and the battery. Because if the arduino doesn’t have power, it will simply not work of course. The other benefit of this is that your battery will have a longer lifespan. Because even if you don’t play the game, the battery will drain. Setting it up does mean more soldering, but we think it’s worth it. It’s also very simple to do. Cut the wire which sits between your battery and the arduino, and strip the ends so the metal becomes visible. Also strip the ends of your switch if that hasn’t been done already. Then solder one red wire of the switch to the red wire of the piece that leads to the arduino. After that solder the black wire of the same side of the switch to the black wire of the piece to the arduino. Then do the same to the other side of the switch and the piece that leads to the battery, so red to red and black to black. After this make sure to isolate it with tape for instance to ensure that you prevent a short circuit.

Piezzo Buzzer
Another optional thing that’s in the code is a buzzer. This buzzer will sound when all the life LEDs are off, e.g. when you’re game over. Just like the start button, it’s not worth it to use a breadboard for this. Therefore this needs to be soldered. It’s a very simple circuit, its a wire followed by the buzzer, a resistor and another wire. Once soldered together, isolate it of course and you’re done.

Step 4: The Code

Now you have installed the essential hardware, it's time to upload the software. First things first: If you copy our code(which is fine of course) consider the fact that this code is made with the amount of pins we chose for our own set-up. If you want to use a different amount of GameLEDs, for example 4 GameLEDs, just choose 4 pins(that are next to each other) and in the code change the 1 in ledPinMin=1 into the number of the lowest pin. For ledPinMax you need to change the 10 with the highest number of the four pins. our code will fill in the rest( it automatically selects all the pins between ledPinMin and ledPinMax to be GameLEDs). Also keep in mind that you need to have downloaded a library if you will be using the display: LiquidCrystal_I2C.h. Just use this link:

https://github.com/fdebrabander/Arduino-LiquidCrys...

If you want to change the initial text that appears on the display, just look under the void.setup and change what's between the parentheses after lcd.print (But keep it between the quotes). We advise to keep off the other lcd.print texts, as they are specially made to keep the score. If you don't want to use the display and/or the buzzer you don't neccesarily need to change the code concerning their operating. Just make sure nothing is connected to the pins belonging to the buzzer or display .

<p>// LED game versie 0.12<br>//
//  random ledjes
//  buttons on 24, startbutton on pin 15
//  levens ledjes 11, 12, 13
//  lcd
 
#include <LiquidCrystal_I2C.h>
<liquidcrystal_i2c.h>
 
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27, 16, 2);
 
#define SHORT unsigned short
 
const SHORT ledPinMin = 1;       // the number of the first LED pin
const SHORT ledPinMax = 10;      // the number of the last LED pin (>=ledPinMin)
 
const SHORT buttonPin = 14;      // the number of the pushbutton pin
const SHORT StartbuttonPin = 15; // the number of the Startbutton pin
 
const SHORT buzzerPin = 50;      // the number of the buzzer pin -- this does NOT work
 
const SHORT LifePinMin = 11;     // the number of the first Life indicating LED pin
const SHORT LifePinMax = 13;     // the number of the last Life indicating LED pin (>=LifePinMin)
 
 
// This code is run in the Arduino when starting up and sets the input and output pins and resets all LEDs
 
void setup() {
   // Test if all the LEDS work properly by turning these on and off
  for (SHORT pin=ledPinMin; pin<=ledPinMax; pin++) {
      pinMode(pin, OUTPUT);
      digitalWrite(pin, HIGH);
      delay (100);
      digitalWrite(pin, LOW);   
  } //endfor
 
    pinMode(buttonPin, INPUT);
    pinMode(StartbuttonPin, INPUT);
 
    // Reset all the Life LEDS
    for(SHORT pin=LifePinMin; pin<=LifePinMax; pin++) {
        pinMode(pin, OUTPUT);
        digitalWrite(pin, LOW);
    } //endfor
 
  lcd.init ();                               // LCD initialisation and turn the backlight on
  lcd.backlight ();
  lcd.setCursor(0, 0);                       // set the cursor to column 0, line 0                        
  lcd.print("reACTION by");                  // Display a message to first line of LCD
  lcd.setCursor(0, 1);                       // set the cursor to column 0, line 1                       
  lcd.print("Femke en Vincent");             // Display a message to second line of LCD
 
}
 
 
// This code will turn all game LEDS off
 
void clearLeds() {
    for(SHORT pin=ledPinMin; pin<=ledPinMax; pin++)
        digitalWrite(pin, LOW);
}
 
// This code makes sure only the lives left are shown
 
void showLifeLeds(SHORT ledsToShow) {
 
    for(SHORT pin=LifePinMin; pin<=LifePinMax; pin++) {
        if (pin < (LifePinMin+ledsToShow))
            digitalWrite(pin, HIGH);
        else
            digitalWrite(pin, LOW);
    } // endfor
}
 
// This code is run in the Arduino forever and runs the game
void loop() {
 
    SHORT livesLeft = LifePinMax-LifePinMin+1;  // number of lives dependent on LEDs allocated
    SHORT randomLED = 0;                        // this will contain the number of random LED selected and lit
    SHORT buttonState = LOW;
 
    SHORT timeLoop = 0;
    SHORT timeToReact = 1250;                   // Reactiontime in ms
    const SHORT scanTime = 50;                  // this number might need tweaking, if buttons are missed decrease
  int score = 0;
 
    do {
        buttonState = digitalRead (StartbuttonPin);  // Here we wait until Start button is pressed
        delay (scanTime);
    } while (buttonState == LOW);
 
    buttonState = LOW;
    showLifeLeds(livesLeft);                     // show lives in LifeLEDS
 
    do {
        clearLeds();                               // initialize random LEDS to off
 
        randomLED = random (ledPinMax-ledPinMin+1); // here we decide which LED will be lit
        digitalWrite(ledPinMin+randomLED, HIGH);    // turn on random LED
        timeLoop = 0;
 
        do {
            // Scan buttons for a keypress; correct button will show since all others get no current
 
            buttonState = digitalRead (buttonPin);    // Here we read the pin to see if a button was pressed
 
           timeLoop = timeLoop + scanTime;
 
            delay (scanTime);
 
        } while ((buttonState == LOW) && (timeLoop < timeToReact)); // Loop will run until button is pressed or time is up
 
        // Now we decide if a live is lost
 
        if (buttonState == LOW)
            livesLeft--;
   else
          score += 5;                           // if pressed on time score increases by 5;
 
   lcd.setCursor(0, 0);                        // set the cursor to column 0, line 0 
   lcd.print("SCORE:        ");
 
   lcd.setCursor(7, 0);                       // set the cursor to column 0, line 0 
       lcd.print(score);                           // Display Score
   lcd.setCursor(0, 1);                       // set the cursor to column 0, line 1                       
   lcd.print("              ");               // Remove any text on this line
 
        showLifeLeds(livesLeft);                   // show lives in LifeLEDS
 
    } while (livesLeft > 0);                     // Game will run als long as lives are left
 
    // Here we are out of lives so game is ended. Arduino will restart the loop
 
   lcd.setCursor(0, 0);                        // set the cursor to column 0, line 0                        
   lcd.print("Game over");
   lcd.setCursor(0, 1);                        // set the cursor to column 0, line 1                       
   lcd.print("SCORE: ");
   lcd.print(score);                           // Display Score
 
   clearLeds ();
}</liquidcrystal_i2c.h></p>

Step 5: The Casing

Now generally for the casing, you should leave it up to your own creative genius. However we would like to tell the story of how we made our casing, to give tips and perhaps even inspire you:

For our casing we used a laser cutter and we suggest you do the same. If you don’t have the access to one, you can try making your own box but we won’t provide the instructions for it (for obvious reasons). Our box was 25:15:7 cm in dimensions. But it later proved to be not high enough, so we suggest to make it at least 10 cm high or even 12 or 13 cm. This is also the reason why all that tape is on the box in the picture. The width and depth were perfect for 3 breadboards and our arduino due inside. But if you need more because you want more reaction LEDs, make sure they fit into it. We used the website https://www.makercase.com/#/ to make the notches on the sides so it would be easier to put the box together. In there you can easily adjust the dimensions to your liking. After that we smoothed out the lid and the top parts of the sides in lightburn( the program for lasercutters), so we could take the lid off if we wanted. We did this because we had to access it even after putting it all together. It is strongly advised to do the same. To stop the lid from coming too far down we glued a wooden piece to each side of the box. To get the lid of the box, you need to make a little cutout in the side as shown on the photo.Perhaps if you have some spare time you can even work with engravings as we did. As said earlier, the amount of reaction buttons and LEDs are customizable, so keep that in mind for designing your lid. Also, if you don’t want a LCD screen and/or on/off button, you don’t need to have cutouts for that. So all the necessary things to have in your lid are: 3 holes for the life leds, a hole for the start button, a equal amount of holes for the reaction LEDs and reaction buttons (so the amount that you want) and a optional hole for a possible LCD screen and one for a possible on/off switch.

Step 6: Have Fun!

This last step isn't as much a step as it is more of an encouragement. We just want to wish you the best of luck and just remember to have fun.

Patience is a key element of succes

-Bill Gates