Programming Arduino Getting Started with Sketches


delay at all. The best solution depends on the application. while



Yüklə 5,42 Kb.
Pdf görüntüsü
səhifə27/78
tarix22.12.2023
ölçüsü5,42 Kb.
#189286
1   ...   23   24   25   26   27   28   29   30   ...   78
Arduino SimonMonk 2011

delay
at all. The best solution depends
on the application.
while
Another way of looping in C is to use the 
while
command in place of the 
for
command.
You can accomplish the same thing as the preceding 
for
example using a 
while
command
as follows:


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.




Yüklə 5,42 Kb.

Dostları ilə paylaş:
1   ...   23   24   25   26   27   28   29   30   ...   78




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin