A Blink That Speeds Up
Part of the First Circuits section of Coddy's Arduino journey. Lesson 17 of 22.
delay(waitTime) waits for whatever waitTime holds at that moment. Change the variable while the sketch runs, and the blink changes with it.
waitTime = waitTime - 100; // the next blink is 100 ms shorterPut that at the end of loop() and every blink is shorter than the one before.
Watch what happens when it runs for a while. The wait shrinks to 0, and then below 0. delay() cannot wait a negative time: it reads -200 as an enormous number, about 49 days, so the LED looks frozen. A sketch has to decide for itself when to stop shrinking, and that is what the next lesson is about.
Challenge
EasyMake the LED on pin 13 blink faster and faster:
- at the top, make
int waitTime = 1000; - in
loop(), blink withdelay(waitTime)for both waits - at the end of
loop(), makewaitTime200 smaller
Press Run and watch until it freezes, then Check.
Try it yourself
// make waitTime here
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
// make waitTime smaller
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in First Circuits
3Math on the Board
Named ConstantsArithmetic OperatorsInteger DivisionDecimals with floatA Blink That Speeds UpPractice on your own: Arduino playground