Programming Arduino (1) Pages 69
Programming Arduino (1) Pages 69
digitalWrite(ledPin, HIGH);
delay(delayPeriod);
digitalWrite(ledPin, LOW);
delay(delayPeriod);
So, all we have really done here is to move the four lines of code that flash
the LED from the middle of the for loop to be in a function of their own called
flash . Now you can make the LED flash any time you like by just calling the
new function by writing flash() . Note the empty parentheses after the function
name. This indicates that the function does not take any parameters. The delay
value that it uses is set by the same delayPeriod variable that you used before.
Parameters
When dividing your sketch up into functions, it is often worth thinking about
what service a function could provide. In the case of flash , this is fairly obvious.
But this time, let’s give this function parameters that tell it both how many times
to flash and how short or long the flashes should be. Read through the following
code and then I will explain just how parameters work in a little more detail.
// sketch 4-02