The Arduino Inventor's Guide (12 page)

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

Start a new sketch, and add the code in
Listing 2-1
to the global namespace of your sketch.

LISTING 2-1:
Variables that represent pin numbers

byte
redPin = 13;
byte
ylwPin = 12;
byte
grnPin = 11;

Again, these three variables store the pin numbers for the three LEDs. On the Arduino, pin numbers are limited to whole numbers between 0 and 13, so we use the
byte
data type. We can use
byte
because we know that the pin number will be less than 255. Notice that each variable’s name describes what it contains:
redPin
is for the red LED pin,
ylwPin
is the yellow LED pin, and
grnPin
is the green LED pin. And, just as
Figure 2-10
shows, the red pin is pin 13, yellow is pin 12, and green is pin 11. Now, anytime you use a pin number in your sketch, you can use the descriptive variable name instead.

NOTE

For legibility, we
camel-cased
the variable names by capitalizing the
p
in
pin
.
Camel-casing is a coding convention that allows you to separate words in a variable without using spaces.

Write the setup() Function

To continue writing the Stoplight sketch, add the
setup()
function in
Listing 2-2
.

LISTING 2-2:
setup()
code for the Stoplight

void
setup
()
{
  
//red LED
  
pinMode
(redPin

,
OUTPUT

);
  
//yellow LED
  
pinMode
(ylwPin,
OUTPUT
);
  
//green LED
  
pinMode
(grnPin,
OUTPUT
);
}

Just like the “Hello, world!” sketch in
Project 1
(see “
The
setup()
Function
” on page
30
), this sketch configures the digital pins of the Arduino in
setup()
with the
pinMode()
function.

This project uses three different digital pins, so the sketch has three separate
pinMode()
functions. Each function call includes a pin number as its variable

(
redPin
,
ylwPin
, and
grnPin
) and the constant
OUTPUT

. It uses
OUTPUT
because this sketch controls LEDs, which are output devices. We’ll introduce
INPUT
devices in
Project 4
.

Write the loop() Function

Next comes the
loop()
function. Normal stoplights cycle from red to green to yellow and then back to red, so this project does, too. Copy the code from
Listing 2-3
into the
loop()
portion of your sketch.

LISTING 2-3:
loop()
code for the Stoplight

void
loop
()
{
  
//red on
  
digitalWrite
(redPin,
HIGH
);
  
digitalWrite
(ylwPin,
LOW
);
  
digitalWrite
(grnPin,
LOW
);
  
delay
(2000);
  
//green on
  
digitalWrite
(redPin,
LOW
);
  
digitalWrite
(ylwPin,
LOW
);
  
digitalWrite
(grnPin,
HIGH
);
  
delay
(1500);
  
//yellow on
  
digitalWrite
(redPin,
LOW
);
  
digitalWrite
(ylwPin,
HIGH
);
  
digitalWrite
(grnPin,
LOW
);
  
delay
(500);
}

The Stoplight will have only one light on at a time, to avoid confusing your hallway traffic and causing chaos. To maintain order, each time an LED is turned on, the other LEDs should be turned off. For example, if you wanted the red light to be on, you’d call the function
digitalWrite(redPin, HIGH)
, followed by
digitalWrite(ylwPin, LOW)
and
digitalWrite(grnPin, LOW)
. The first call writes
HIGH
to
turn on the red LED on
redPin
(pin 13), and the other two calls write
LOW
to
ylwPin
and
grnPin
(pins 12 and 11) to turn off the yellow and green LEDs. Because the Arduino runs at 16 MHz (roughly one instruction per 16 millionth of a second), the time between these commands is on the order of a few microseconds. These three commands run so fast that you can assume they all happen at the same time. Finally, notice the function
delay(2000)
. This function pauses the sketch and keeps the red light on for 2,000 ms, or 2 seconds, before executing the next set of instructions.

The code for the yellow and green LEDs repeats the same concept, setting the corresponding pin to
HIGH
and the others to
LOW
and delaying for different lengths of time. For your own Stoplight, try changing the delay times to something a little more realistic for your hallway’s traffic. Remember that the value you pass to the
delay()
function is the amount of time you want the LED to stay on in milliseconds.

Upload the Sketch

After you’ve typed in all of the code, double-check that it looks like the code in
Listing 2-4
, save your sketch, and upload it to your Arduino by clicking
Sketch

Upload
or pressing
CTRL
-U. If the IDE gives you any errors, double-check your code to make sure that it matches the example code exactly. Your instructions should have the same spelling, capitalization, and punctuation, and don’t forget the semicolon at the end of each instruction.

When everything works, your LEDs should turn on and off in a cycle that is similar to a real stoplight—starting with a red light, followed by a green light, and then a short yellow light before returning to the top of the
loop()
function and going back to red. Your sketch should continue to run this way indefinitely while the Arduino is powered.

LISTING 2-4:
Complete code for the Stoplight

byte
redPin = 13;
byte
ylwPin = 12;
byte
grnPin = 11;
void
setup
()
{
  
pinMode
(redPin,
OUTPUT
);
  
pinMode
(ylwPin,
OUTPUT
);
  
pinMode
(grnPin,
OUTPUT
);
}
void
loop
()
{
  
//red on
  
digitalWrite
(redPin,
HIGH
);
  
digitalWrite
(ylwPin,
LOW
);
  
digitalWrite
(grnPin,
LOW
);
  
delay
(2000);
  
//green on
  
digitalWrite
(redPin,
LOW
);
  
digitalWrite
(ylwPin,
LOW
);
  
digitalWrite
(grnPin,
HIGH
);
  
delay
(1500);
  
//yellow on
  
digitalWrite
(redPin,
LOW
);
  
digitalWrite
(ylwPin,
HIGH
);
  
digitalWrite
(grnPin,
LOW
);
  
delay
(500);
}

Make the Stoplight Portable

When your Arduino is connected to your computer, it’s receiving power through the USB port. But what if you want to move your project or show it around? You’ll need to add a portable power source—namely, a battery pack. The Arduino board has a barrel jack power port for plugging battery packs into, as well as an on-board voltage regulator that will accept any voltages from about 6 V to 18 V. There are many different battery adapters available, but we like using a 4 AA battery adapter for a lot of our projects, as shown in
Figure 2-12
.

FIGURE 2-12:
A 4 AA battery pack with a barrel jack adapter

Unplug the USB cable from your computer, insert four AA batteries into your battery pack, and plug your portable battery pack into your Arduino, as shown in
Figure 2-13
. If your batteries are charged, you can move your project around or embed it directly into a model stoplight!

FIGURE 2-13:
Making the Stoplight portable by adding a battery pack

Now you’ll level up this project. In the next section, we’ll show you how to turn these LEDs into a model stoplight that you can mount in high-traffic areas of your house.

BUILD THE STOPLIGHT ENCLOSURE

Once your Arduino isn’t tethered to a computer, you can build any electronics project into a more permanent enclosure. The circuit on your breadboard is great, but you probably have to use your imagination to picture it as a stoplight. For maximum effect, the Stoplight just needs a good housing and lenses that will make the lights visible from a distance. The enclosure is optional if all you want to do is prototype, but we hope you’ll try it out.

For this project, we’ll show you how to build a more realistic-looking stoplight with some cardboard or cardstock, but you can use any material that you happen to have lying around. Be creative! Our example, shown in
Figure 2-14
, is made from some cardboard, ping-pong balls, and a bit of crafting skill.

FIGURE 2-14:
An enclosure made from cardboard and ping-pong balls

You can either build a stoplight on your own using this project only as an inspiration or, if you want to reproduce this project exactly as you see it here, download the ZIP file of templates and sketches at
https://www.nostarch.com/arduinoinventor/
. Each project in this book includes templates that you can print, trace, and hand-cut the old-fashioned way with a craft knife and a metal ruler.

NOTE

If you’re lucky enough to have access to a cutting machine like a Cricut, a Silhouette Cameo, or a laser cutter, these files should easily translate to those tools, too.

Extract the
Project 2
files from the ZIP file, and print the Stoplight template PDF at full size if you’d like a cutting guide. With your templates in hand, collect the other items listed in “
Other Materials and Tools
” on page
39
and start building.

Cardboard Construction

Other books

Cruise to Murder (Z & C Mysteries, #2) by Kane, Zoey, Kane, Claire
Until Alex by J. Nathan
The Right Temptation by Escalera, Diane
Waiting for Cary Grant by Mary Matthews
Clear to Lift by Anne A. Wilson