-
Notifications
You must be signed in to change notification settings - Fork 0
Home
The Interval library for Arduino allows you to perform asynchronous actions with your Arduino board, using intervals.
You want to flash a LED. You will use the the delays like this :
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
But you want also emit a sound with tone() while the LED is flashing.
These two actions can't be done at the same time, because the delay() function is blocking.
The program flashes the LED, stops, wait 500 ms and then emits the sound.
The key is timing.
In Arduino, we can get the number of milliseconds since the power up of the board (millis ()function).
By declaring an interval and a timer in the loop, we can check if the interval has been exceeded since the last action. If this is the case, an action is executed. Otherwise, the program continues and rechecks each time. During this time the counter increments.