by Joe Pardue
Get started now with this series!
Joe’s book & kit are available in our
webstore at www.nutsvolts.com
PART 6:
A Menu Navigator
Iknow just enough
French to be dangerous,
so on a visit to Paris, I
insisted on using the
regular menu and got
some very strange meals.
After a few such mishaps,
I discovered that they
usually had an English
menu hidden away for les
barbares so I started using
that one and still got some
very strange meals. C'est
la Vie. In this month’s
■ FIGURE 1. Lé barbare
workshop, after learning
et lé menu.
more about C syntax for
decision making, we are going to write a menu navigation
system similar in concept to the one on your cell phone,
but for the Butterfly using its LCD and joystick. Important
safety tip: Make sure you understand the concepts in
Workshops 1 through 5 before slamming your head into
this article.
C Control Flow
Statements and Blocks
Expressions such as PORTD = ~i or j -= 128
become statements when they are followed by a
semicolon.
PORTD = ~i;
j -= 128;
The semicolon terminates the statement.
Tale of a Bug
I wrote the following statement:
while(QuarterSecondCount < 17600);
QuarterSecondCount = 0;
then decided that the 17600 wait count was too long, so I
changed it to 2200:
But I wanted to remember the 17600 in case I ever
needed it again, so I commented it out and added the new
value. Do you see a problem here?
Well, what I meant to say was:
while(QuarterSecondCount < 2200);
QuarterSecondCount = 0;
which is two statements: the first waits while an
interrupt running in the background increments
QuarterSecondCount, and once that is finished the
QuarterSecondCount is set to zero. What the compiler
saw was:
while(QuarterSecondCount < 2200)
QuarterSecondCount = 0;
But the compiler doesn't see anything following the //
comment delimiter. See the problem yet?
Well, how about the equivalent statement:
while(QuarterSecondCount < 2200)
QuarterSecondCount = 0;
I had accidentally 'commented out' the terminating
semicolon from the first statement. The compiler doesn't
know about the line breaks; all it sees is a single
statement which says that while QuarterSecondCount
is less than 2200, set QuarterSecondCount to 0. So,
each time the interrupt incremented QuarterSecondCount,
this statement set it back to zero. One lousy semicolon
gone and everything changes!
This is the kind of bug that after spending X amount of
time locating, you carefully hide it from your boss lest she
think you are stupid or careless or both. Fortunately, I am
my own boss, so I've learned to live with my stupid and
careless employee. (I fired myself once, but that just didn't
work out.)
Compound statements are made by enclosing a
group of statements or declarations in a block delimited
by braces '{' and '}'. This causes the compiler to handle
the block as a unit.
If-else and else-If
while(QuarterSecondCount < 2200) //17600);
QuarterSecondCount = 0;
80 January 2009
We can make decisions using the if-else statement: