The expression in parentheses after
while
must be true to stay in the
loop
. When it is no
longer true, then the sketch continues running the commands after the final curly brace.
The #define Directive
For constant values like pin assignments that do not change during the running of a sketch,
there is an alternative to using a variable. You
can use a command called
#define
that
allows you to associate a value with a name. Everywhere that this name appears in your
sketch, the value will be substituted before the sketch is compiled.
As an example, you could define a pin assignment for a LED like this:
Note that the
#define
directive does not use an “=” between the name and the value. It
does not even need a “;” on the end. This is because it is
not actually part of the C
language itself; but is called a pre-compiler directive that is run before compilation.
This approach, in the author’s opinion, is less easy to read than using a variable, but it
does have the advantage that you do not use any memory to store it. It is something to
consider if memory is at a premium.
Conclusion
This chapter has got you started with C. You can make LEDs
blink in various exciting
ways and get the Arduino to send results back to you
over the USB by using the
Serial.println
function. You also worked out how to use
if
and
for
commands to control
the order in which
your commands are executed, and learned
a little about making an
Arduino do some arithmetic.
In
the next chapter, you will look more closely at functions.
The chapter will also
introduce the variable types other than the
int
type that you used in this chapter.