by Joe Pardue
Get started no w with this series!
Joe’s book & kit are available in our
webstore at www.nutsvolts.com
PART 7:
The Wearable
Alarm Clock
Lboth of those binary 10
kinds of people. This month,
ast month, we learned about
we will apply it to understanding
Binary Coded Decimal (BCD)
to use in code for a wearable
Butterfly Alarm Clock (see the
safety pin shown in Figure 2).
We will also discuss functions and
variables and learn that 'automatic'
variables can only be used in the
function where they are declared
but 'external' variables — declared
outside any function — can be
used anywhere in the software
module.
A function must be
declared
or defined in a module (a single
text file of code) — usually in a
header file or before the main()
function — before it is encountered
by the compiler.
In Workshop 4, you saw a
function declaration in smws4.h:
uint8_t receiveByte(void);
Function Basics
We've been using functions
for a while, so you should have a
feel for them. But let’s go ahead
and take a more detailed look.
Functions encapsulate a
computation. Think of them as
building material for C programs.
A house is built of studs, nails, and panels. The architect
is assured that all 2x4 studs are the same, as are each of
the nails and the panels, so there is no need to worry
about how to make a 2x4 or a nail or a panel — you just
stick them where needed. Likewise, a function can be
considered a standard component for building software.
It will always do whatever it does and you don't have to
worry about how it does it.
Encapsulation is a key idea in programming and
provides the possibility of making chunks of code
convenient to use. And just as important, it provides a
way to make tested code reusable while not allowing
the programmer to mess with it and chance breaking
something. We saw some of this in our earlier
introduction to libraries.
Functions also help clarify code. A function should do
only one thing. If you find yourself writing a function that
seems to be doing two separable things, try separating it
into two functions for clarity.
74
February 2009
This told the compiler that
we would be using a function
named receiveByte that takes no
parameters (void) and returns a
byte; that to prevent confusion, we
call uint8_t (a standard data type
for avrlibc). Okay, this seems like
a mighty confusing way to prevent
confusion, but in regular old-fashioned C we would call a
byte an 'unsigned char,’ however,
there is no guarantee what size a
char is, and having an unsigned
character doesn't make a lot of
sense anyway. What would a
character '-A' be? So anyway, we use the uint8_t telling us
that our data type is an unsigned integer made of eight
bits and God only knows what the '_t' is there for other
than to make typing harder.
Functions can have parameters — a list of variables
that may be used by the function — such as a1 and a2 in:
■ FIGURE 1. World's Geekiest
Shirt Contest.
uint8_t adder(uint8_t a1, uint8_t a2)
They can return values, such as a uint8_t in the
adder() function.
One thing that often confuses folks about function
parameters is that the function only sees a copy of the
parameter, not the original variable. The confusion comes
when one thinks that by changing the received parameter
in the function body that on return, the caller will see that
parameter changed. It won't.
Let’s look at a bad function: adder() that has three
parameters — adds the first two and puts the results