The Arduino Inventor's Guide (22 page)

FIGURE 4-19:
Secure the button in place with a lot of glue.

NOTE

If using hot glue, be cautious when gluing the button to the cardboard. Hot glue is hot!

Reconnect the Components to the Arduino

Now, use male-to-female jumper wires to connect the LEDs to the breadboard. Remember that the shorter leg of the LED needs to connect to ground (GND) in the circuit, and each of the longer legs should connect to its respective pin on the Arduino, through a 330 Ω resistor. Because the LED legs are a bit long, you may need to clip them back with wire cutters. A strategy we often use is to cut the shorter leg just a little shorter so that you can always tell which leg is the negative leg, as shown in
Figure 4-20
.

FIGURE 4-20:
Cutting back the LED legs. Keep the short leg short!

If you can’t tell which leg is longer, you can also look at the shape of the plastic lens on the LED. There is typically a flat edge on the side nearest to the negative leg of the LED. The flat edge is subtle, but if you look closely, you should be able to see it.

Press the female ends of the male-to-female jumper wires onto the ends of the LEDs so that they fit snugly, like in
Figure 4-21
. To keep things organized and easy to follow, we recommend using a black wire for the negative (short leg) of the LED.

FIGURE 4-21:
Connecting the LED to the jumper wires

Once the jumper wires are connected to all three LEDs, connect the male ends to the breadboard circuit. The wires for the stimulus LED should go to E2 and E3, for the green LED to E8 and E9, and for the red LED to E12 and E13.

Next, reconnect the button to the circuit. The button has four legs, but you only need to connect to two legs on one side of the button. Connect one male-to-female wire to one leg and another to the other leg, as in
Figure 4-22
. Then, plug one wire into the same breadboard row that connects the 10 kΩ resistor and Arduino pin 3, and plug the other into GND on the breadboard. (If you wired up your breadboard prototype just like the diagram in
Figure 4-14
, then connect these wires to E20 and E22 on the breadboard.) Because the button is just a switch, it doesn’t matter which wire you plug into GND.

FIGURE 4-22:
Connecting the male-to-female jumper wires to two legs on one side of the button

With your components in place, plug your Arduino into the computer and open the Serial Monitor to make sure your circuit still works. You should see your instruction messages on the Serial Monitor. When the blue LED lights, press the button as fast as you can. The Serial Monitor should show your reaction time, and either the red or green LED should turn on, based on how fast you were.

If your circuit doesn’t seem to be working, check that all of your connections are secure, and compare your circuit to
Figures 4-14
and
4-15
to make sure the connections are correct.

Spice Up Your Game Enclosure

To finish up, add some bling to your new game. Use your imagination! You might want to cover your Reaction Timer with your favorite stickers or paint the box. We love using ping-pong balls in our projects, and since we had a half left over from
Project 2
, we decided to glue it on top of the blue LED, as in
Figure 4-23
.

FIGURE 4-23:
Carnival-themed Reaction Timer game

GOING FURTHER

Next, try combining what you learned in the first three projects with what you know from this project to make it more interesting—add even more LEDs, or maybe make the game suitable for two players.

Hack

Add two more LEDs to make a four-LED scale that will show your speed more accurately. Faster reaction times will light up more LEDs. To do this, you’ll need the help of a nested
if()

else if()
control
statement. You can stack your condition statements to tell the code what to do in different conditions, so if the first logical expression is
false
, the next one is tested; if that’s also
false
, the next is tested; and so on until the final
else()
statement, which runs if none of the previous conditions were
true
.
Listing 4-7
shows an example of this conditional logic. It assumes you’ve added two extra LEDs connected to pins 10 and 9. Don’t forget the
pinMode()
commands you’ll have to add to the
setup()
!

LISTING 4-7:
Snippet of nested
if()–else if()
statement


   
if
(reactTime <= 215)
    {
      
//turn all LEDs on
      
digitalWrite
(12,
HIGH
);
      
digitalWrite
(11,
HIGH
);
      
digitalWrite
(10,
HIGH
);
      
digitalWrite
(9,
HIGH
);
    }

   
else if
(reactTime <= 250)
    {
      
//turn three LEDs on
      
digitalWrite
(12,
LOW
);
      
digitalWrite
(11,
HIGH
);
      
digitalWrite
(10,
HIGH
);
      
digitalWrite
(9,
HIGH
);
    }

   
else if
(reactTime <= 300)
    {
      
//turn two LEDs on
      
digitalWrite
(12,
LOW
);
      
digitalWrite
(11,
LOW
);
      
digitalWrite
(10,
HIGH
);
      
digitalWrite
(9,
HIGH
);
    }

   
else
    {
      
//turn one LED on
      
digitalWrite
(12,
LOW
);
      
digitalWrite
(11,
LOW
);
      
digitalWrite
(10,
LOW
);
      
digitalWrite
(9,
HIGH
);
}

The
if()
statement at

checks whether the reaction time is less than or equal to 215 ms and lights up all four LEDs. Then two
else if()
statements catch times between 215 ms and 250 ms

,
lighting up three LEDs, and between 250 ms and 300 ms

, lighting up two LEDs. Finally, an
else
statement

catches all times slower than 300 ms and lights up a single LED.

If you need a little more help with the code, check out our example sketch in the resources at
https://www.nostarch.com/arduinoinventor/
.

Modify

One fun way to modify this project would be to make it a two-player game. You could add a second button and repurpose your LEDs to indicate which player is faster. In this modification, the green LED will light up if Player 1 is faster, and the red LED will light up if Player 2 is faster.

First, add a second button.
Figure 4-24
shows the additional button at the bottom of the breadboard. Notice that it’s just a duplication of the pull-up resistor/button combination circuit that you built for the first button.

FIGURE 4-24:
Adding a second button for two-player mode

NOTE

If both players hit the buttons at exactly the same time, the game will favor Player 1 (the green LED). Although this would be a really rare occurrence, how would you modify the code to light up both LEDs if both buttons are pressed at the same time?

The complete code for the two-player modification is available, along with the wiring diagram for the modification, at
https://www.nostarch.com/arduinoinventor/
.

Now, go and take your new game out on the town. Are you faster than your family? Are you faster than your friends? Who is the fastest person you know?

Other books

Worth the Drive by Mara Jacobs
Less Than Perfect Circumstance by Clarke , Kristofer
Vita Brevis by Ruth Downie
Dark Promises (Dark #29) by Christine Feehan
Cowboy Kisses by Diane Michele Crawford
The Art of Crash Landing by Melissa DeCarlo