- Refresh an LCD or OLED every 500ms
- Read sensors every few minutes
- Flash an LED continuously, or a preset number of times
- Heartbeat LED
- Anywhere you need millisecond timing...
- Repeating / Multiple shot / Single shot timing
- Disable/enable
- Timing progress monitor
- Simplifies your code
- Multiple SimpleTimers can run simultaneously
- Schedules functions to run one or more times
- Tiny code footprint, non-blocking, no interrupts used
To run code every 500ms
if(timer1.done(500){...}
Multiple-shot : To run code every 500ms, but only run it 10 times
if(timer1.done(500, 10){...}
Single-shot : To run code once after a 500ms delay, use a repeat of 1
if(timer1.done(500, 1){...}
To prevent a SimpleTimer from running
timer1.enabled(false);
To restart a SimpleTimer
timer1.enabled(true);
To check if a SimpleTimer is enabled
if(timer1.isEnabled()){...}
Returns the time (ms) a SimpleTimer has been running during a timing period
unsigned long t = timer1.elapsed();
The code in the SimpleTimer_examples
folder uses the board's LED_BUILTIN to demonstrate most of SimpleTimer's features. Simply paste the code into your 'main.c' or '.ino' file and ensure the SimpleTimer header is an 'include' file.
- Flashing an LED continuously is simple, here it's 250ms on and 250ms off (2Hz)
if(timer1.done(250)){digitalWrite(ledPin1,!digitalRead(ledPin1));
- Flashes LED 5 times, then stops
if(timer1.done(200,10)){digitalWrite(ledPin1, !digitalRead(ledPin1));
- Flashes LED 5 times, repeats every 5 seconds
if(timer1.done(200,10)){digitalWrite(ledPin1,!digitalRead(ledPin1));} if(timer2.done(5000)){timer1.enabled(true);}
- Flashes an LED with different on/off periods
toggleLED(600,100);
- Makes the LED glow and dim slowly.
heartbeat();
- Turns LED on fast then dims it slowly
heartbeatpulse();
- Runs various functions at various times after start-up, in this example it writes to the serial port
It can be messy keeping track of millis() if you use more than one timer or delay.
So I wanted an easy to use self-contained non-blocking timer.
SimpleTimer gives you more!
You can run several SimpleTimers at once, and each timer can encapsulate your own private variables that you can use for whatever you want e.g to keep track of LED state, the number or state of, well, just about anything...
If all you want to do is flash an LED, this is the simplest way. More complex tasks are also made easier, including a slow glowing/dimming heartbeat to show your software is running OK, see the example code.