Function Parameters
Part of the Inputs section of Coddy's Arduino journey. Lesson 20 of 22.
blinkOnce() always blinks pin 13 for 200 ms. To blink any pin for any time, give the function parameters: variables listed in its parentheses, each with a type, that get their values from the call.
void blinkLed(int pin, int ms) {
digitalWrite(pin, HIGH);
delay(ms);
digitalWrite(pin, LOW);
delay(ms);
}The values in a call are its arguments. They fill the parameters in order: blinkLed(12, 200); runs the block with pin as 12 and ms as 200, and blinkLed(13, 600); with 13 and 600.
Because the order decides, blinkLed(200, 12) would try to blink pin 200 for 12 ms.
Challenge
EasyA green LED is on pin 12 and a red one on pin 13:
- write
void blinkLed(int pin, int ms): the pin on, waitms, off, waitms - in
loop(), blink the green LED for 200 ms, then the red one for 600 ms
Press Run, then Check.
Try it yourself
const int greenPin = 12;
const int redPin = 13;
// write void blinkLed(int pin, int ms) here
void setup() {
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
}
void loop() {
// blink the green LED for 200 ms, then the red one for 600 ms
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Inputs
Practice on your own: Arduino playground