Programming Arduino Getting Started with Sketches


int s. So to define a variable called  ledPin



Yüklə 5,42 Kb.
Pdf görüntüsü
səhifə21/78
tarix22.12.2023
ölçüsü5,42 Kb.
#189286
1   ...   17   18   19   20   21   22   23   24   ...   78
Arduino SimonMonk 2011

int
s. So to define a variable
called 
ledPin
with a value of 13, you need to write the following:
Notice that because 
ledPin
is a name, the same rules apply as those of function names.
So, there cannot be any spaces. The convention is to start variables with a lowercase letter
and begin each new word with an uppercase letter. Programmers will often call this
“bumpy case” or “camel case.”
Let’s fit this into your Blink sketch as follows:


We have also sneaked in another variable called 
delayPeriod
.
Everywhere in the sketch where you used to refer to 13, you now refer to 
ledPin
, and
everywhere you used to refer to 500, you now refer to 
delayPeriod
.
If you want to make the sketch blink faster, you can just change the value of
delayPeriod
in one place. Try changing it to 100 and running the sketch on your Arduino
board.
There are other cunning things that you can do with variables. Let’s modify your sketch
so that the blinking starts really fast and gradually gets slower and slower, as if the
Arduino is getting tired. To do this, all you need to do is to add something to the
delayPeriod
variable each time that you do a blink.
Modify the sketch by adding the single line at the end of the 
loop
function so that it
appears, as in the following listing, and then run the sketch on the Arduino board. Press
the Reset button and see it start from a fast rate of flashing again.


Your Arduino is doing arithmetic now. Every time that 
loop
is called, it will do the
normal flash of the LED, but then it will add 100 to the variable 
delayPeriod
. We will
come back to arithmetic shortly, but first you need a better way than a flashing LED to see
what the Arduino is up to.

Yüklə 5,42 Kb.

Dostları ilə paylaş:
1   ...   17   18   19   20   21   22   23   24   ...   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