0% found this document useful (0 votes)
17 views1 page

Programming-Arduino (1) - Pages-75

Uploaded by

axl1994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Programming-Arduino (1) - Pages-75

Uploaded by

axl1994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

}

Sketch 4-03 is based on sketch 3-09, but attempts to use a local variable
instead of the global variable to count the number of flashes.
This sketch is broken. It will not work, because every time loop is run, the
variable count will be given the value 0 again, so count will never reach 20 and
the LED will just keep flashing forever. The whole reason that we made count a
global in the first place was so that its value would not be reset. The only place
that we use count is in the loop function, so this is where it should be placed.
Fortunately, there is a mechanism in C that gets around this conundrum. It is
the keyword static . When you use the keyword static in front of a variable
declaration in a function, it has the effect of initializing the variable only the first
time that the function is run. Perfect! That’s just what is required in this
situation. We can keep our variable in the function where it’s used without it
getting set back to 0 every time the function runs. Sketch 4-04 shows this in
operation:
// sketch 4-04

const int ledPin = 13;

const int delayPeriod = 250;

void setup()

pinMode(ledPin, OUTPUT);

void loop()

You might also like