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.
Dostları ilə paylaş: