Project 1.03 Blink Faster
We’ll now make a small change to Project 1.01 by replacing the fixed delay with an int variable that is decreased by 25% after each blink.
Concepts: integer math, if statement
Circuits:
Concepts: integer math, if statement
Circuits:
Most of this sketch is the same as in Project 1.01. We add an addition int variable, wait, to keep track of the delay between blinks. We set its initial value to 1000:
int wait = 1000;
After blinking LED2, we multiply the variable wait by 3 and divided by 4. It would be logical to simply multiply by 0.75 but that would mix an int with a float value, which can have unexpected effects:
wait = wait*3/4;
The result of this math now replaces the previous value held by wait.
In the first pass through the loop() block, wait is changed from 1000 to 750. In the next pass, wait starts at 750 and is changed to 562. It keeps getting smaller with every pass. Eventually, LED2 is switched on and off so fast that we can’t even tell its blinking. We need to reset wait. We use a conditional statement to change wait back to 1000 when it falls below 5:
if(wait < 5) wait = 1000;
}
And the closing bracket } finishes the loop() block.
int wait = 1000;
After blinking LED2, we multiply the variable wait by 3 and divided by 4. It would be logical to simply multiply by 0.75 but that would mix an int with a float value, which can have unexpected effects:
wait = wait*3/4;
The result of this math now replaces the previous value held by wait.
In the first pass through the loop() block, wait is changed from 1000 to 750. In the next pass, wait starts at 750 and is changed to 562. It keeps getting smaller with every pass. Eventually, LED2 is switched on and off so fast that we can’t even tell its blinking. We need to reset wait. We use a conditional statement to change wait back to 1000 when it falls below 5:
if(wait < 5) wait = 1000;
}
And the closing bracket } finishes the loop() block.