The Arduino Inventor's Guide (21 page)

BOOK: The Arduino Inventor's Guide
3.9Mb size Format: txt, pdf, ePub

FIGURE 4-15:
Completed wiring of the new circuit with two extra LEDs

Update the Code for Extra LEDs

Now that you have these two indicator LEDs, you’ll add a few extra lines of code to your project to turn on the green LED if you’re faster than the time to beat and the red LED if you’re not.

You’ll need to add a
pinMode()
command for pins 12 and 11 and set these up as
OUTPUT
s to control your new LEDs. The changes to the
setup()
function are shown in
Listing 4-3
(the existing code is shown in light gray).

LISTING 4-3:
The modified
setup()
function for the Reaction Timer with the extra speed-indicator LEDs

void setup()
{
  
Serial.begin(9600);   //sets up serial
                        
//communication
  
pinMode(13, OUTPUT);  //sets pin 13 as an OUTPUT for the
                        
//stimulus LED
  
pinMode
(12, OUTPUT);  
//sets pin 12 as an OUTPUT for the
                        
//green LED
  
pinMode
(11, OUTPUT);  
//sets pin 11 as an OUTPUT for the
                        
//red LED
  
pinMode(3, INPUT);    //sets pin 3 as an INPUT for the
                        
//button
}

You simply inserted two extra
pinMode()
instructions for the two extra LEDs that you’re going to add.

Control the Flow with if() and else()

Now you’ll need to add a little bit of decision logic into your sketch. In Arduino programming, an
if()
statement allows you to control the direction and flow of a sketch. It tells the code “if this is true, run the code in the following curly brackets.” The general syntax for the
if()
statement is shown in
Listing 4-4
.

LISTING 4-4:
Generic
if()
statement in Arduino

  
if
(

expression
)  
//if
expression
is true, run the code in
                   
//the following loop
    {
    

    }

else
//otherwise, run the code in this loop instead
    {
    

    }

The
expression

is a Boolean expression that is either
true
or
false
, like the ones we discussed earlier in “
Logical Comparison Operators
” on page
106
. If the expression is
true
, the sketch will start executing any code between the curly brackets

. If it’s not
true
, the sketch skips over the curly brackets and goes to the next statement. Oftentimes, the
if()
statement is paired with an
else

. If the expression is not
true
, the sketch skips over the first set of curly brackets

and continues on to the code that is part of the
else
statement

.

For the Reaction Timer game, you’ll use an
if()
statement to turn on the green LED if the reaction time is less than or equal to
215 ms and the red LED if the reaction time is greater than 215 ms. You’ll be able to change this value to make it harder or easier, but this is a good middling value for now.
Listing 4-5
shows the code to do this.

LISTING 4-5:
Code snippet of the
if()
statement for the Reaction Timer game


if
(

reactTime <= 215)
  {

   
digitalWrite
(12,
HIGH
);  
//green LED ON

   
digitalWrite
(11,
LOW
);   
//red LED OFF
  }

else
  {
    
digitalWrite
(12,
LOW
);   
//green LED OFF
    
digitalWrite
(11,
HIGH
);  
//red LED ON
  }

You can see at

that the sketch uses the
if()
statement to perform this logic. The Boolean expression,
reactTime <= 215

, checks whether the value from
reactTime
is less than or equal to
215
, and if it is, the green LED turns on

. When the green LED turns on, the red LED needs to be off, so you add one extra instruction

to do that. Finally, you add the
else
statement

to turn the red LED on if the
if()
statement evaluates to
false
.

Upload the Complete Code for the Reaction Timer

The new
if()
statement should be placed within the
loop()
, after the code to turn off the red LED, as shown in
Listing 4-6
. (The
snip
indicates where existing code has been omitted on the page for length.)

LISTING 4-6:
Adding the
if()
statement for the green and red LED game indicators

--
snip
--
  
//loop to wait until button is pressed
  
while(digitalRead(3) == HIGH)
  
{
  
}
  
reactTime = millis() - startTime;  //calculation of reaction
                                     
//time
  
digitalWrite(13, LOW);             //turn off LED!
  
if
(reactTime <= 215)
  {
    
digitalWrite
(12,
HIGH
);  
//green LED ON
    
digitalWrite
(11,
LOW
);   
//red LED OFF
  }
  
else
  {
    
digitalWrite
(12,
LOW
);   
//green LED OFF
    
digitalWrite
(11,
HIGH
);  
//red LED ON
  }
//display information to Serial Monitor
--
snip
--

After you’ve added the new code, upload the whole sketch to your Arduino. Open the Serial Monitor and play a game to make sure it all works as expected. Can you get the green light to turn on? You have to be fast!

GAMING THE GAME: CONTROL THE DIFFICULTY

As with all games, with the Reaction Timer, sometimes you’ll need to adjust the difficulty level. Since you’re the programmer, you get to control the game. If 215 ms is too fast or too slow, you can adjust your game
threshold
by changing the number in the line
if (reactTime <= 215)
.

Want to make it impossible to beat? Change this value to a low number like 100 ms. Want to be nice and make it easier? Change it to a number like 500 ms. You’re writing the code, so you get to decide the rules of the game!

BUILD THE REACTION TIMER ENCLOSURE

When your prototype works, it’s time to build a more permanent enclosure. To keep this project as simple as possible, our version of the Reaction Timer is designed to fit into a small SparkFun box. The top of the box measures 3 7/8 inches × 5 inches, but you can put your game in anything you have lying around the house—an old cereal box, oatmeal container, or anything else made of a sturdy cardboard.

We made our Reaction Timer look like an old-school carnival game with a few fun clip-art drawings we found, but you can make yours look however you want.
Figure 4-16
shows a template for the outside of our box. For this design, we have a hole for the button, a
hole for the stimulus LED, and two holes for indicator LEDs—one to show that you’re faster than a ninja and the other to show that you’re as slow as a turtle. You can download the template with this book’s resources via
https://www.nostarch.com/arduinoinventor/
or just cut holes for the LEDs and buttons anywhere you like on your box.

FIGURE 4-16:
Template for the Reaction Timer carnival game cover art (not full size)

Cut Out the Cardboard

If you use our template, print it and then glue or tape it to the front of your box. Whether you use the template or not, you’ll need to make a total of four holes in the cardboard for the three LEDs and the button. You can use a craft knife or a drill to
carefully
cut out the holes, as shown in
Figure 4-17
. The LEDs are 5 mm in diameter, so a 3/16-inch drill bit is a pretty close fit. For the button hole, we recommend using a 5/16-inch drill bit, if you’re drilling, or a sharp pencil.

FIGURE 4-17:
Cutting out the holes from a cardboard box

Assemble the Electronics

Now that you have the holes cut out of your Reaction Timer box, you need to add the electronic components. You’re going to move the three LEDs and the button from the breadboard to the exterior of your new cardboard box so that players can see them.

Attach the LEDs and Button to the Cardboard

First, press the LEDs through their three holes from the back side of the cardboard, making sure that each sits snugly. If the holes you cut are too big and the LEDs are a little loose, simply add a small dab of glue to keep them in, as in
Figure 4-18
.

FIGURE 4-18:
Moving the LEDs to your project box/ cardboard

Next, add the button. The push buttons that come in the SparkFun Inventor’s Kit have a cap that pops off. Remove the button cap, insert the button from the inside of the box, and glue the button onto the cardboard, as in
Figure 4-19
. Reattach the button cap on the top side of the cardboard. Players are going to mash this button as they try to get the best score possible, so use a lot of glue to make sure it’s secure! When the glue is dry, try the button out. You need to be able to press the button in all the way, so make sure the cap doesn’t get caught on any cardboard when you press it down.

Other books

Deceived by Camilla Isles
Doglands by Tim Willocks
A Catered St. Patrick's Day by Crawford, Isis
FATAL eMPULSE by Mark Young
Poison by Sarah Pinborough
Tears on My Pillow by Elle Welch
Planet of Pain by B. A. Bradbury
Alice-Miranda to the Rescue by Jacqueline Harvey