Read The Arduino Inventor's Guide Online
Authors: Unknown
Finally, add the shades to the Stoplight. For a nice curve, make a number of parallel scores, about 1/8 inch apart, as shown in
Figure 2-23
. There are example score lines in the template, so you can follow those. After making all of your scores, bend each shade into a curve, as shown in
Figure 2-24
.
FIGURE 2-23:
Scoring a shade
FIGURE 2-24:
Bending the shade into a curve
Once you have the shades bent and shaped to your liking, fit them into the housing just above each lens, as shown in
Figure 2-25
, and then glue them in place. If you’re going for a more finished or realistic look, you can spray paint the housing black. Make sure you either remove the lenses or cover them with masking tape first so that they don’t get coated in spray paint.
FIGURE 2-25:
Fitting a shade into the housing
All you have left to do is to connect the LEDs from the new enclosure to your Arduino. First, use two male-to-female jumper wires (SparkFun PRT-09385) to extend each of the LEDs. You’ll need a total of six of these jumper wires. Simply plug each LED leg into the
female end of the jumper wire. To keep things organized, we like to use black wires for the negative (shorter) leg and colored wires for the positive (longer) leg, as shown in
Figure 2-26
.
FIGURE 2-26:
Attaching jumper wires to the LEDs
With the jumper wires connected to the LEDs, plug the male end into the breadboard in the same place where the LED came out, as shown in
Figure 2-27
. Again, pay attention to which LED goes where. If you don’t remember, consult the original diagram in
Figure 2-10
.
FIGURE 2-27:
Inserting the male end of each jumper wire into the breadboard
Check to make sure your connections work by plugging in the Arduino to your computer or to a battery pack. If one of the lights isn’t working, try jiggling the connections or double-checking that the wires are plugged into the correct row on the breadboard.
You can either leave the Arduino and breadboard outside the Stoplight housing or tack them inside the housing with glue or double-sided tape. Whatever you decide, when you’re done, power up your Stoplight, and go find a busy hallway intersection in need of traffic safety.
Figure 2-28
shows the finished Stoplight in all its glory.
FIGURE 2-28:
Finished Stoplight project
The concepts you saw while building the Stoplight, such as timing the control of output (LEDs), can be applied to a number of different uses in your house and life. Here are a couple of suggestions for adapting the Stoplight.
The basic concept of a stoplight is all about timing. When else would a timer be useful? What about changing the code to help you time frying an egg? You could rework the Stoplight so that the red LED is lit while the egg is still in a state of “iffy” or “rare” doneness, the yellow LED lights when it’s almost cooked the way you like it, and then the green LED lights when the egg is done.
We can’t give you the timing, as we probably have different preferences for how we like our eggs cooked. There are also a number of variables that will affect the timing, like the temperature, the type of pan, and the size of the egg. You’ll have to figure that all out on your own.
In the code, you’d need to work with pretty big numbers for the delay, since it’s measured in milliseconds. To set a delay in minutes, all you need is a little multiplication. Remember that 1,000 ms equals 1 second; multiply by 60, and you’ll find that 60,000 ms equals 60 seconds, or 1 minute. For a delay of 3 minutes, you can multiply 3 by 60,000 directly in the
delay()
function, like this:
delay
(60,000 * 3);
You may be wondering how long you can set the
delay()
function for. The data type that
delay()
receives is an
unsigned long
, which is any number that falls in the range of 0 to 4,294,967,295. So the maximum delay is 1,193 hours or so. Pretty cool! Knowing this, is there anything else you’d want to time with the
delay()
function?
If you’re looking to make this project more permanent and sturdy, you can solder wires to the LEDs instead of using the male-to-female jumpers. If you’ve never soldered before, turn to “
How to Solder
” on page
302
for some soldering instructions before you start. You’ll need to snip the end off of a male-to-male jumper wire, strip the insulation back about 1/2 inch using wire strippers, and then solder the stripped end to each leg of a trimmed LED, as shown in
Figure 2-29
. Notice that we twisted the wire around the leg of the LED to hold it securely while soldering. After soldering, the connection will be more durable, and you’ll be able to use the LEDs for other projects since the other end is still a male jumper.
FIGURE 2-29:
Soldering a cut jumper wire to an LED
Though this project looks impressive, the programming and hardware are pretty simple. As you read about sensors and logic over the next few chapters, we encourage you to think back to this project and brainstorm ways you can elaborate on it with what you learn.
We use monitors every day, on our phones, computers, tablets, and TVs. The displays on most present-day monitors are composed of millions of
pixels
, short for
picture elements
. Pixels are tiny points that the computer can light up in different colors; all the pixels together make up the text, images, and videos on the screen.
In this project, you’re going to build a simple monitor using LEDs. You’ll expand on your work with blinking LEDs and learn to use custom functions in Arduino. Finally, you’ll learn how to display secret characters on your very own Nine-Pixel Animation Machine. You can see ours in
Figure 3-1
.
FIGURE 3-1:
A completed Nine-Pixel Animation Machine
You can use the Nine-Pixel Animation Machine to show letters and numbers, draw basic geometric shapes, and make plenty of other fun pixel art.
For this project, you’ll need a few more electronic components than you used in
Project 2
, specifically more LEDs. This project is simpler in terms of enclosure construction, however. The materials you’ll need are shown in
Figures 3-2
and
3-3
.
NOTE
In our project all the LEDs are the same color so the patterns are easier to see, but if you don’t have nine LEDs of the same color, you can mix them up.
• 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; not shown)
• One solderless breadboard (PRT-12002)
• Nine LEDs, preferably of the same color (COM-10049 for a pack of 20 red and yellow LEDs)
• Nine 330 Ω resistors (COM-11507 for a pack of 20)
• Male-to-male jumper wires (PRT-11026)
• Male-to-female jumper wires (PRT-09140*)
• (Optional) One 4 AA battery holder (PRT-09835*; not shown)
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 3-2:
Components for the Nine-Pixel Animation Machine
• Pencil
• Craft knife
• Metal ruler
• Wire cutters
• Glue (hot glue gun or craft glue)
• Graph paper (not shown)
• (Optional) Drill and a 3/16-inch drill bit
• (Optional) Soldering iron
• (Optional) Solder
• (Optional) Helping hands (not shown)
• Cardboard sheet (roughly 8 × 11 inches, or 20.5 × 30 cm; not shown)
• Enclosure template (see
Figure 3-13
on page
83
)
FIGURE 3-3:
Recommended tools
This simple pixel art display will teach you to manage lots of wires in one circuit, which is important as your circuits grow larger. First, you’ll use the breadboard to make sure the circuit works, test a sketch, and get comfortable with all of the jumper wires. (We’ll show you how to transfer the LEDs to a display housing in “
Cardboard Construction
” on page
83
.) Notice that the circuit diagram in
Figure 3-4
looks a lot like
Figure 2-6
on page
43
. That’s because this project uses the same LED circuit, but instead of just three LEDs, it uses nine LEDs, each of which is independently controlled by a pin on the Arduino.
FIGURE 3-4:
Schematic diagram for the Nine-Pixel Animation Machine
In this section, you’ll use the breadboard to connect all nine LEDs to pins on the Arduino. With your components and jumper wires in hand, build the circuit in
Figure 3-5
on your breadboard. If you want to practice building smaller LED circuits first, flip back to “
Connect the Red LED to the Breadboard
” on page
44
for a refresher.
FIGURE 3-5:
Nine LEDs connected to the Arduino, with the pin 13 LED at the top and the pin 5 LED at the bottom
Wiring up nine LEDs can make for a cluttered breadboard. To keep your breadboard organized, connect the ground rail (–) on the left side of your breadboard to the GND pin on the Arduino first. That’s the black wire in
Figure 3-5
. Then, connect your first LED’s negative leg (the shorter one) to this ground rail through a 330 Ω resistor. In
Figure 3-5
, the long leg of the LED is in row 1, and the shorter leg is in row 2. Finally, connect the long leg of the LED to pin 13 with a jumper wire from pin 13 of the Arduino to row 1 of the breadboard. Connect the other eight LEDs to pins 12 through 5 in the same way. And remember: the LED’s short leg is its negative leg. As you’re building this circuit, make sure that the shorter leg of each LED is connected to the ground rail of the breadboard through a resistor. When you’re done, your circuit should resemble the circuit in
Figure 3-6
.
FIGURE 3-6:
Final prototype circuit of nine LEDs connected to the Arduino, with the pin 13 LED at the top and the pin 5 LED at the bottom
Once you’ve wired the nine LEDs, open the Arduino IDE and plug the Arduino into your computer with the USB cable. If you uploaded a sketch to the Arduino in a previous project, you might see the LEDs light up as it runs the last sketch you uploaded. Now let’s take a look at how you’ll code up all nine of these LEDs.
In previous projects, it’s been simple enough to use a bunch of
digitalWrite()
functions and
delay()
functions to control LEDs. But with nine LEDs, you’d have a really messy
loop()
function! Instead, you can write your own
custom function
to blink a single LED, and then use this function to control all the LEDs.
The Arduino language has 60 or so
built-in
(or predefined) functions that make it easier for you to interact with hardware using simple, single-line instructions. The
digitalWrite()
and
delay()
functions are two examples. Behind the scenes, the
digitalWrite()
function consists of more than 20 lines of code. Most of that code is complex, but the
digitalWrite()
function is easy to understand.
Even when you understand a big sketch, typing 20 or more lines of code each time you want to turn an LED on or off is tedious and error prone. Arduino’s built-in functions cover common tasks, but when you need to do something specific to your sketch, you’ll want
to write custom functions. Custom functions allow you to easily reuse code in other sketches, and they’ll make your
loop()
functions easier to read.
You can use custom functions to teach the Arduino new commands. Your first test function will blink an LED on and then off again, using a modified version of the code in
Listing 3-1
.
LISTING 3-1:
A simple sketch that blinks an LED
void
setup
()
{
pinMode
(13,
OUTPUT
);
}
void
loop
()
{
digitalWrite
(13,
HIGH
);
delay
(1000);
digitalWrite
(13,
LOW
);
delay
(1000);
}
This code should look similar to the Blink example from
Project 1
(
Listing 1-1
on page
28
). However, here we are using pin 13 explicitly instead of using the
LED_BUILTIN
system constant. This code turns the LED on, waits for a second, turns the LED off again, and then waits for another second before repeating. You’ll need to do this a lot in later projects, so we’ll show you how to put this code in a custom
blink()
function. To make your custom function as useful as possible, you’ll write it in a way that allows you to use any pin for any delay time.