Building CylonEyes Hardware
We built the AVR Workshop Learning Platform in the
last workshop. Details for the construction can be found
in: Smiley’s Workshop 1 Supplement: AVR Learning
Platform Foamcore Base and Box also available on the
websites. Follow the schematic in Figure 10 and photo in
Figure 11. If you haven’t done this before, please refer to
the supplement: Using a Breadboard included in the downloads. Cycle the power. The LCD will be blank. Click the
joystick up and your LEDs should be making like a Cylon’s
eyes with the light moving back and forth. Cool, huh?
NOTE: The Butterfly LCD dances like crazy with
each LED message pass because some of the port D pins
are also tied to the LCD. Will it harm the LCD? Probably
not, but I don’t know for sure, so don’t leave CylonEyes
When you compile CylonEyes.c, you may suspect that
a lot of stuff is going on in the background, and you
On the first pass, the code evaluates the ‘for’ statement, notes that variable ‘i’ is equal to 1 which is less
than 128, and runs the block of ‘Do something’ code.
Next, the ‘for’ expression is reevaluated with ‘i’ now
multiplied by 2 ‘i = i*2’ which is 2 and 2 < 128 is true, so
the block runs again. Next, i = 4, and so on till i = 128,
and ‘128 < 128’ is false. The program stops running
the loop and goes to the next statement following the
Quick now, how many times does this loop run?
The series of ‘i’ values evaluated against the ‘< 128’ is
‘1,2, 4, 8, 16, 32, 64,128’ and since it takes the 128 as the
cue to quit, the loop runs eight times.
The while(‘expression’) statement tests the
expression to see if it is true (meaning that it is not equal
to 0, which is defined as false) and allows the block to
run if true. After running through the loop, it retests the
‘expression,’ looping through the block each time it is
true. The program proceeds to the next statement when
the expression becomes false.
int 1 = 0;
while(i < 10)
// load the x array with the y array
x[i] = y[i++];
The ‘while’ loop runs 10 times since the ‘i’ is
incremented (has 1 added to the current value) 10 times,
putting the first 10 values of the y array into the x array —
■ FIGURE 10. CylonEyes schematic.
would be right. Fortunately for us, we don’t really need to
know how it does what it does. We only need to know
how to coax it to do what we need it to do: convert
more on arrays later.
A while(1) runs the loop forever because ‘1’ is
true (false is 0). We use this to keep the ‘main’ function
A function encapsulates a computation. Think of
them as building material for C programs. A house might
be built of studs, nails, and panels. The architect knows
that all 2x4 studs are the same, as are each of the nails
and each of 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 and don’t worry how they
were made. In the CylonEyes program, the main()
function uses the _delay_loop_2() function twice.
The writer of the main() function doesn’t need to know
how the _delay_loop_2(30000) function does its job, he
only needs to know what it does and what parameters
to use, in this case, 30000 will cause a delay of about
The _delay_loop_2() function is declared in the
header delay.h and the AVRStudio is set up so that the
compiler knows where to look for it.
Encapsulation of code in functions is a key idea in
C programming and helps make chunks of code more
convenient to use. And just as important, it provides a
way to make tested code reusable without having to
rewrite it. The idea of function encapsulation is so
important in software engineering that the C++ language
was developed primarily to formalize these and related
concepts and force their use.