Change the Blink Speed
Part of the First Circuits section of Coddy's Arduino journey. Lesson 3 of 22.
delay() counts in milliseconds (ms), thousandths of a second. delay(1000) is one second, delay(250) a quarter of a second.
One blink is the time the LED is on plus the time it is off. With two waits of 1000 ms, a blink takes 2000 ms. Smaller numbers make shorter waits, so the LED blinks faster:
digitalWrite(13, HIGH);
delay(100); // on for a tenth of a second
digitalWrite(13, LOW);
delay(100); // off for a tenth of a seconddelay() only takes whole milliseconds. delay(0.2) is not a fifth of a second: the board drops the fraction and waits 0 ms. For a fifth of a second, write delay(200).
Both waits matter. Without the second one, the LED turns off and is switched straight back on, so it looks like it never goes off.
Challenge
EasyThe starter blinks slowly. Make it fast:
- on for 200 ms
- then off for 200 ms
Press Run to watch it, then Check.
Try it yourself
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000); // make this wait shorter
digitalWrite(13, LOW);
delay(1000); // and this one
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in First Circuits
1Hello, Board
Blink an LEDSetup and LoopChange the Blink SpeedAn LED of Your OwnTwo LEDs Taking TurnsComments in Your SketchPractice on your own: Arduino playground