Read The Arduino Inventor's Guide Online
Authors: Unknown
LISTING 3-8:
loop()
function with
xChar()
function call
void
loop
()
{
xChar();
}
This is a small step, but it’s very important. If you forget to actually call your custom function, its code will never run, and you’ll never display your
X
.
Upload this sketch to your Arduino now. Your LEDs are still in a vertical line rather than a grid, but your sketch can still help you test that you’ve got everything wired and coded correctly. Instead of an
X
, you should see every other LED turn on, starting at the top, as shown in
Figure 3-10
. If the LEDs don’t light up as expected, double-check your wiring against the diagram in
Figure 3-5
on page
71
, and check your
digitalWrite()
functions to make sure they’re all correct as well.
When you can see the right pattern, read on to create a second character.
FIGURE 3-10:
Prototype and correct sequence for an
X
Next, you’ll create an
O
like the one in
Figure 3-11
to go with the
X
.
FIGURE 3-11:
A nine-pixel
O
Pro tip: you can work smarter rather than harder here. Copy the entire
xChar()
function, paste the copy after the last curly bracket in
xChar()
, change its name to
oChar()
, and tweak it to look like Listing 3-9.
LISTING 3-9:
The
oChar()
custom function
void
oChar()
{
digitalWrite
(13,
HIGH
);
digitalWrite
(12,
HIGH
);
digitalWrite
(11,
HIGH
);
digitalWrite
(10,
HIGH
);
digitalWrite
(9,
LOW
);
digitalWrite
(8,
HIGH
);
digitalWrite
(7,
HIGH
);
digitalWrite
(6,
HIGH
);
digitalWrite
(5,
HIGH
);
}
The only difference between
xChar()
and
oChar()
is which LEDs are turned on and which are turned off. Whereas
xChar()
turns on alternating LEDs,
oChar()
turns on every LED except the center one.
TRY IT OUT: WRITE A CUSTOM FUNCTION FOR YOUR OWN IMAGE
We showed you how to write functions to draw an
X
and an
O
, but we’re sure you have your own lovely pixel art images in mind. Create a function that will blink out the patterns you made, and hang on to it for when you’re done building the Nine-Pixel Animation Machine.
The goal now is to show an
X
character for a bit, then show an
O
character, and finally go back to the
X
. To show each character for a set time, you can add the
oChar()
function to your existing loop and slow the loop down with
delay()
calls. Update your sketch so that it looks like
Listing 3-10
.
LISTING 3-10:
The
loop()
looks similar to the one in the Blink sketch but uses
xChar()
and
oChar()
instead of
digitalWrite()
.
//LED array is set up in this arrangement:
// 13 ---- 12 ---- 11
// 10 ---- 9 ----- 8
// 7 ---- 6 ----- 5
void
setup
()
{
pinMode
(13,
OUTPUT
);
pinMode
(12,
OUTPUT
);
pinMode
(11,
OUTPUT
);
pinMode
(10,
OUTPUT
);
pinMode
(9,
OUTPUT
);
pinMode
(8,
OUTPUT
);
pinMode
(7,
OUTPUT
);
pinMode
(6,
OUTPUT
);
pinMode
(5,
OUTPUT
);
}
void
loop
()
{
//blink between x and o characters
xChar();
delay
(500);
oChar();
delay
(500);
}
void
xChar()
{
digitalWrite
(13,
HIGH
);
digitalWrite
(12,
LOW
);
digitalWrite
(11,
HIGH
);
digitalWrite
(10,
LOW
);
digitalWrite
(9,
HIGH
);
digitalWrite
(8,
LOW
);
digitalWrite
(7,
HIGH
);
digitalWrite
(6,
LOW
);
digitalWrite
(5,
HIGH
);
}
void
oChar()
{
digitalWrite
(13,
HIGH
);
digitalWrite
(12,
HIGH
);
digitalWrite
(11,
HIGH
);
digitalWrite
(10,
HIGH
);
digitalWrite
(9,
LOW
);
digitalWrite
(8,
HIGH
);
digitalWrite
(7,
HIGH
);
digitalWrite
(6,
HIGH
);
digitalWrite
(5,
HIGH
);
}
This loop displays an
X
for 500 ms and then switches to an
O
for 500 ms. Upload the updated sketch to your Arduino, and run it to see how it works. Every LED except the middle one lights up when
oChar()
is called.
Figure 3-12
shows what you’ll see as the LEDs blink.
FIGURE 3-12:
Switching between two patterns
Save your sketch now, because you’ll build on it later. But as long as the LEDs in this circuit are on the breadboard, they won’t display any recognizable picture. So next, we’ll show you how to make the display to see the Nine-Pixel Animation Machine in all its tiny glory.
The enclosure for this project is simply a cardboard display with holes for LEDs. There’s wiring to do as well, but once that’s done, you’ll be able to make all kinds of pixel art.
Find a sheet of cardboard that is clean and free of creases and bends. Our designs are based around cardboard about 1/8 inch thick, but you can use any similar board or panel materials. Some materials will be easier to cut than others, so pick yours based on the tools you have.
Open the template shown in
Figure 3-13
in this book’s resource files (
https://www.nostarch.com/arduinoinventor/
) and trace it onto your cardboard. Try to line your templates up with the edge of the cardboard to make cutting easier.
FIGURE 3-13:
Enclosure template for the Nine-Pixel Animation Machine (not full size)
Once you’ve traced your pieces, cut them out. We highly recommend using a sharp craft knife and a metal ruler to get clean edges for your project. Remember craft knife safety: always pull (don’t push) the blade, and make multiple passes rather than digging in deeply on your first go.
After cutting your cardboard parts, make the LED holes in the front piece. You can drill them as shown in
Figure 3-14
, punch them
with a hole punch, or even poke them out with a pencil. Just be sure to have a free LED on hand to test the size of each hole for a snug fit. If the holes are a little too large and you don’t mind making the LEDs a permanent feature of the project, you can hot glue them in.
FIGURE 3-14:
Drilling holes for LEDs. Use caution when drilling or ask an adult for help.
You should have four pieces cut out, as shown in
Figure 3-15
. The base has the big hole in the middle; use its center piece to cut out two triangles to use as support braces. These triangles have tabs that fit into slots connecting the bottom piece to the front. Before assembling the parts, however, add labels so that you can keep your LEDs straight when you wire up your circuit.
FIGURE 3-15:
Cardboard pieces for the enclosure