The Arduino Inventor's Guide (18 page)

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

Since you’re using the same LEDs you were in the
X
and
O
sketch, you can just copy the code from there into your new sketch.

Write Custom Functions

Now, create a custom function for each image of your animation. This animation has four frames, so you’ll need four functions. Add the functions in
Listing 3-12
to your sketch, after the closing bracket in the
loop()
function.

LISTING 3-12:
Custom functions to draw a spinning line


void
verticalLine()
  {
    
digitalWrite
(13,
LOW
);
    
digitalWrite
(12,
HIGH
);
    
digitalWrite
(11,
LOW
);
    
digitalWrite
(10,
LOW
);
    
digitalWrite
(9,
HIGH
);
    
digitalWrite
(8,
LOW
);
    
digitalWrite
(7,
LOW
);
    
digitalWrite
(6,
HIGH
);
    
digitalWrite
(5,
LOW
);
  }

void
topLeftDiagonal()
  {
    
digitalWrite
(13,
HIGH
);
    
digitalWrite
(12,
LOW
);
    
digitalWrite
(11,
LOW
);
    
digitalWrite
(10,
LOW
);
    
digitalWrite
(9,
HIGH
);
    
digitalWrite
(8,
LOW
);
    
digitalWrite
(7,
LOW
);
    
digitalWrite
(6,
LOW
);
    
digitalWrite
(5,
HIGH
);
  }

void
horizontalLine()
  {
    
digitalWrite
(13,
LOW
);
    
digitalWrite
(12,
LOW
);
    
digitalWrite
(11,
LOW
);
    
digitalWrite
(10,
HIGH
);
    
digitalWrite
(9,
HIGH
);
    
digitalWrite
(8,
HIGH
);
    
digitalWrite
(7,
LOW
);
    
digitalWrite
(6,
LOW
);
    
digitalWrite
(5,
LOW
);
  }

void
topRightDiagonal()
  {
    
digitalWrite
(13,
LOW
);
    
digitalWrite
(12,
LOW
);
    
digitalWrite
(11,
HIGH
);
    
digitalWrite
(10,
LOW
);
    
digitalWrite
(9,
HIGH
);
    
digitalWrite
(8,
LOW
);
    
digitalWrite
(7,
HIGH
);
    
digitalWrite
(6,
LOW
);
    
digitalWrite
(5,
LOW
);
  }

The
verticalLine()
function

shows the first image in
Figure 3-23
, the
topLeftDiagonal()
function

shows the second image, the
horizontalLine()
function

shows the third, and the
topRightDiagonal()
function

shows the last. As with your previous custom function, these custom functions have the
void
data type, since they won’t return a value.

Custom functions can call other custom functions, too, so let’s call the four line functions inside a single
spinningLine()
function. Add the following code to your sketch, after the closing bracket in the
topRightDiagonal()
function.

void
spinningLine(
int
delayTime)
{
  verticalLine();
  
delay
(delayTime);
  topLeftDiagonal();
  
delay
(delayTime);
  horizontalLine();
  
delay
(delayTime);
  topRightDiagonal();
  
delay
(delayTime);
}

This code shows a vertical line, a diagonal line, a horizontal line, and another diagonal line, with a delay after each line. Now, all you have to do is call
spinningLine()
inside the
loop()
function.

NOTE

You’ll find a complete listing of this code in the resource files at
https://nostarch.com/arduinoinventor/
.

Tweak Your loop() Function

Add a call to your custom function inside your
loop()
function, as in
Listing 3-13
. Remember that you still need to have all of those
pinMode()
commands in your
setup()
function.

LISTING 3-13:
Completed
loop()
function with the new custom function call
spinningLine(200)
;

void
loop
()
{
  spinningLine(200);
}

Once you add the function and pass it a delay time parameter (the code uses
200
), upload your sketch to your Arduino. You’ll see a rotating line on your display. With this knowledge of sequencing LEDs and custom functions, you can make your own nine-pixel animation!

GOING FURTHER

Custom functions will be useful when you want to reuse code later or organize your code. To take this project further, try designing more elaborate animations; you could even come up with your own alphabet and use your monitor to display a secret message.

Hack

To take this project further, start by creating more elaborate animations. As you work through the next few projects, look for ways to incorporate your monitor—for example, maybe you could use a sensor to control an animation speed or display a sensor value in some interesting ways. Download a blank design template at
https://www.nostarch.com/arduinoinventor/
.

Modify

You’ve learned how to control a number of electronic components by using digital pins and custom functions. Try replacing your individual LEDs with different components. We suggest a
seven-segment display
, as shown in
Figure 3-24
.

FIGURE 3-24:
A single seven-segment display

Each segment is an LED that you can control. There are seven individual segments (plus the decimal points), as shown in
Figure 3-25
, and by turning specific segments on and off, you can make numbers and most letters of the English language.

FIGURE 3-25:
Illustration of the seven individual segments and the decimal point with a corresponding wiring diagram

You can control one of these displays the same way you control the Nine-Pixel Animation Machine: just create custom functions for each number. For a challenge, create a single function that lets you pass a number to display to it.

4
Reaction Timer

An average human reacts to a visual
stimulus
, like a light turning on, in about 215 milliseconds. This is the time it takes for a signal you see with your eyes to travel to your brain and out to your limbs to respond. The reaction timer is a great project to demonstrate this time delay, and it also makes for a fun game! How fast are you and your friends?

In this chapter, you’ll learn how to build your own reaction timer using Arduino. The full project is shown in
Figure 4-1
. The concept behind it is simple: the Arduino will turn on an LED, start a timer, and wait until you press a button. When you see the LED turn on, you press the button as quickly as you can, and the Arduino will report back to your computer the time between the light coming on and you pressing the button. The Arduino has a 16 MHz clock, which means that it can process 16 million instructions per second! That’s
fast
, and it makes the Arduino perfect for this project.

FIGURE 4-1:
The completed Reaction Timer project

MATERIALS TO GATHER

Like the previous projects in this book, the Reaction Timer uses LEDs, resistors, wires, and an Arduino. Unlike other projects, this one also includes a button to make the game interactive, and to make it look spiffy we suggest a custom cardboard enclosure.
Figures 4-2
and
4-3
show the parts and materials you’ll need for this project.

Electronic Parts

• One SparkFun RedBoard (DEV-13975), Arduino Uno (DEV-11021), or any other Arduino-compatible board

• One USB Mini-B cable (CAB-11301 or your board’s USB cable)

• One solderless breadboard (PRT-12002)

• One red LED, one blue LED, and one green LED (COM-12062)

• Three 330 Ω resistors (COM-08377, or COM-11507 for a pack of 20)

• One 10 kΩ resistor (COM-08374, or COM-11508 for a pack of 20)

• One push button (COM-10302)

• Male-to-male jumper wires (PRT-11026)

• Male-to-female jumper wires (PRT-09140*)

NOTE

The parts marked with an asterisk (*) do not come with the standard SparkFun Inventor’s Kit but are available in the separate add-on kit.

FIGURE 4-2:
Components and materials for the Reaction Timer

Other Materials and Tools

• Pencil

• Craft knife

• Metal ruler

• Glue (hot glue gun or craft glue)

• (Optional) Drill and 3/16-inch and 5/16-inch drill bits

• (Optional) Wire cutters (not shown)

• Cardboard (about 12 inches square) or a cardboard box

• Enclosure template (see
Figure 4-16
on page
115
)

• (Optional) Ping-pong ball

FIGURE 4-3:
Recommended tools

NEW COMPONENT: THE PUSH BUTTON

This project revolves around two components: an LED and a button. The button switch is an
input
to an Arduino pin, which means that the sketch can react to a change in the voltage on that pin. Inputs like buttons let you create circuits that people can interact with.

How Push Buttons Work

There are many different kinds of push buttons, but they all work in a similar manner. A
push button
is really an electrical switch. Push buttons like the ones in
Figure 4-4
are small, spring-loaded devices that connect two sides together electrically for as long as you apply pressure, like the keys on your keyboard. And they’re everywhere—in remote controls, garage door openers, coffee makers, radios, game controllers, and so much more!

FIGURE 4-4:
A variety of push buttons

These nifty input devices are really simple on the inside.
Figure 4-5
shows the schematics for both a push button and a switch.

FIGURE 4-5:
Schematic drawings for a push button and a switch

When you flip a switch on, a piece of metal inside closes the gap between two contacts, like a gate. When you press a push button, metal pushes straight down to bridge that gap. Find a push button in your supplies for this project and examine it. Even though the schematic symbol in
Figure 4-5
shows only two contacts, most standard push buttons for breadboards have four legs.
Figure 4-6
shows a more accurate illustration of the contacts inside, along with how a button like that might look on the breadboard. When you plug one in, the legs should straddle the ditch in the middle of the breadboard.

FIGURE 4-6:
Push button schematic and button correctly placed on a breadboard with legs straddling the ditch

Push buttons are fantastic inputs in projects, because everyone knows how they work. Push buttons are also pretty simple to connect in a circuit with an Arduino. Let’s look at how that works.

Using Resistors with Push Buttons

To use any button as an input to an Arduino, you’ll need to use a
pull-up resistor circuit
like the one in
Figure 4-7
. A pull-up resistor connects to a power source on one side and to an input component (like a button) on the other. The part of a circuit that needs to detect input is connected at the intersection of the resistor and the button.

In the configuration shown in
Figure 4-7
, the resistor to 5 V
pulls
the Arduino pin’s default voltage
up
to 5 V, which is considered
HIGH
. When the button is pushed, a path is created between the Arduino pin and ground, and the Arduino pin reads a
LOW
voltage. This works because current always flows along the path of least
resistance: when the button isn’t pressed, the Arduino pin 10 kΩ resistor is the only path the current can access, but when the button
is
pressed, it offers a path with effectively zero resistance.

FIGURE 4-7:
Pull-up resistor and push button circuit

BUILD THE REACTION TIMER PROTOTYPE

The Reaction Timer combines an LED circuit similar to the ones in previous projects with the button circuit from
Figure 4-7
to make the supercircuit in
Figure 4-8
, which lights an LED and detects button presses.

FIGURE 4-8:
Schematic diagram for the Reaction Timer prototype

Other books

His Paradise Wife by Tina Martin
My Body-Mine by Blakely Bennett
Cole in My Stocking by Jessi Gage
Crisis (Luke Carlton 1) by Frank Gardner
El Paso Way by Steven Law
Taking Care by Joy Williams
Paving the New Road by Sulari Gentill
Shifting Targets by Austina Love
Playing the 'Son' Card by Wilson James