Read The Arduino Inventor's Guide Online
Authors: Unknown
First, open a new sketch, copy the code from
Listing 3-1
into it, and save it. Then, define the
blink()
function below your
setup()
and
loop()
functions, as shown in
Listing 3-2
.
LISTING 3-2:
A skeleton for a custom
blink()
function
➊
void
➋
blink
(
➌
int
pinNumber,
int
delayTime)
{
//custom function code goes here
}
This is just the skeleton of the function. Function definitions always specify the data type that the function will return first
➊
. The
blink()
function asks the Arduino to perform a task without expecting any data back, so, just like the
setup()
and
loop()
functions, its data type is
void
.
Next comes the function’s name
➋
, which in this case is
blink
. You can name Arduino functions almost anything you want, but they can’t start with a number or include any spaces or special characters. Also, to make sure the function’s purpose is clear when you’re reading over your sketch, we recommend using a name that’s descriptive and easy to remember.
After naming the function, define any parameters the function needs to work in parentheses
➌
. To make
blink()
as reusable as possible, provide a pin number and a delay time as parameters. These allow you to specify which LED to blink and for how long. Each of the
blink()
function’s parameters is an
int
. Notice that defining parameters is similar to declaring variables. That’s because parameters are basically variables that can only be used inside the function.
Finally, a custom function has its own set of curly brackets, which enclose all of the code you want a call to the function to represent. For the
blink()
function, this is the set of
digitalWrite()
and
delay()
functions from
Listing 3-1
, as shown in
Listing 3-3
. Add the code inside the curly brackets to your
blink()
function now.
LISTING 3-3:
Custom
blink()
function
void
blink
(
int
pinNumber,
int
delayTime)
{
digitalWrite
(pinNumber,
HIGH
);
delay
(delayTime);
digitalWrite
(pinNumber,
LOW
);
delay
(delayTime);
}
Notice that in the
digitalWrite()
and
delay()
function calls, the
blink()
function replaces the pin number
13
and the delay of
1000
ms with the
pinNumber
and
delayTime
parameters, respectively.
Now you can use your custom function in place of the code in your
loop()
function, as in
Listing 3-4
.
LISTING 3-4:
Complete sketch using the new custom
blink()
function
void
setup
()
{
pinMode
(13,
OUTPUT
);
}
void
loop
()
{
blink
(13, 1000);
}
void
blink
(
int
pinNumber,
int
delayTime)
{
digitalWrite
(pinNumber,
HIGH
);
delay
(delayTime);
digitalWrite
(pinNumber,
LOW
);
delay
(delayTime);
}
The modified
loop()
function calls the
blink()
function and passes it the pin number of the LED to blink (
13
) and the amount of time the light should stay on or off, in milliseconds (we suggest using
1000
, for 1 second). That’s it! Doesn’t this code look a lot cleaner and clearer?
Upload this sketch to your Arduino, and the LED on pin 13 should blink. Congratulations! You just “taught” the Arduino what
blink()
means, and you condensed four lines of code into a single instruction.
But custom functions are just one key to this project. You’ll also want to plan some nine-pixel patterns ahead of time to make them easier to program, so let’s do that now.
TRY IT OUT: PLAY WITH PATTERNS
Before you finish the Nine-Pixel Animation Machine, spend some quality time with your more powerful
blink()
sketch. For example, change your
loop()
function as follows:
void
loop
()
{
blink
(13, 100);
//short 100 ms blink on pin 13
blink
(13, 2000);
//longer 2000 ms blink on pin 13
}
This makes the LED blink for 100 ms, followed by a longer blink of 2,000 ms. Try blinking the other LEDs to create your own patterns and sequences!
Grab colored pencils and graph paper, and put on your artist’s hat! You’re going to make some pixel art to display on the Nine-Pixel Animation Machine.
Start by drawing several 3 × 3 grids, or print out a copy of the template shown in
Figure 3-7
, which you’ll find in this book’s resource files. These drawings don’t have to be perfect; you’re just sketching out a few ideas.
FIGURE 3-7:
Blank grid planning template
We numbered the pixels with 13 in the upper-left corner and 5 in the lower-right corner. These numbers correspond to the LED pin numbers on the Arduino; this is how you’ll control the LEDs in the Nine-Pixel Animation Machine. When your canvas is ready, get creative: fill in the pixels to make your own patterns.
Figure 3-8
shows some examples that we came up with during a department meeting . . . don’t tell our boss!
FIGURE 3-8:
Some pixel pattern examples
When you show these patterns and shapes in sequence, you can create animations! First, we’ll teach you to program two simple shapes, and then we’ll tackle a complete animation.
Create a new sketch in the Arduino IDE, and add the
setup()
and
loop()
functions in
Listing 3-5
.
LISTING 3-5:
setup()
function for the Nine-Pixel Animation Machine
//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
()
{
}
NOTE
We also suggest adding a comment with a diagram of the LED placement. Describing the circuit you’re writing code for in the sketch will help others re-create it and will remind you of the structure you’re working with as you develop your code.
This project uses nine LEDs on nine digital GPIO pins (13 through 5), so this
setup()
function has nine
pinMode()
functions. They’re all set as
OUTPUT
since they’re controlling LEDs.
With the
pinMode()
functions in place, look at the shapes you drew by hand and pay attention to the numbers; they’ll help you write your custom function.
Figure 3-9
shows an example to test with—a simple
X
pattern.
FIGURE 3-9:
A nine-pixel
X
The
X
pattern in
Figure 3-9
translates to the set of
digitalWrite()
functions in
Listing 3-6
.
LISTING 3-6:
Code for displaying an
X
on the nine LEDs
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
);
These
digitalWrite()
function calls turn on only the LEDs on the grid’s diagonals and turn off the rest. But drawing a single
X
takes nine lines of code! Instead of writing all of this out by hand every time, you can create a custom function to execute all of these calls with just one line of code.
Below the curly brackets of the
loop()
function, create a custom function named
xChar()
with the code in
Listing 3-7
.
LISTING 3-7:
The
xChar()
custom function
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
);
}
We named this custom function
xChar()
because it displays an
X
character. This function won’t return anything, so its data type is
void
. Since the
digitalWrite()
calls from
Listing 3-6
are inside this single custom function, you can keep your
loop()
code simple. Call the function
xChar()
inside your existing
loop()
, as shown in Listing 3-8.