Periodic is an Arduino library that simplifies the Blink Without Delay pattern. It is used in the following form:
#include <Periodic.h>
...
void loop()
{
periodic(ms) {
...
}
...
}
The Blink Without Delay example sketch can be expressed as:
#include <Periodic.h>
static int LED = 13;
static unsigned long INTERVAL = 1000L;
void setup()
{
pinMode(LED, OUTPUT);
}
void loop()
{
periodic(INTERVAL) {
digitalWrite(LED, !digitalRead(LED));
}
}
Multiple periodic blocks are allows. Below is an extended blink example sketch with three blink intervals.
#include <Periodic.h>
static int RED_LED = 13;
static int GREEN_LED = 12;
static int BLUE_LED = 11;
void setup()
{
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
}
void loop()
{
periodic(250) {
digitalWrite(RED_LED, !digitalRead(RED_LED));
}
periodic(500) {
digitalWrite(GREEN_LED, !digitalRead(GREEN_LED));
}
periodic(1000) {
digitalWrite(BLUE_LED, !digitalRead(BLUE_LED));
}
}
For further details please see the interface and example sketch in the repository.
Cheers!
NB: The library has been ported from the Cosa Periodic class.