My Blink Without Delay Timer using Lambda and Callbacks:
ZTimer has come about with help from many people on this forum and I must thank them for the ideas and concepts they provided that helped me out.
Nick Gammon for your time helping with Lambda I couldn't have made this without your help :
LarryD From your structure timer I created the powerful class library Post #6 You Shared
Coding Badly For helping me understand timer rollover along time ago
and many others who gave suggestions and support
Thank You!
Please try and share you ideas and improvements.
I have included a section that triggers every function included in the class with detailed descriptions on how they work. After watching this discussion I will add Documentation to the class and post any changes you may suggest to improve the code.
#include "ZTimer.h"
ZTimer TimerZ;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(115200);
Serial.println("Testing ZHomeslices ZTimer Library");
// Lets set the Callback functions to trigger code each time CheckTime() triggers true
TimerZ.SetCallBack([&]() { // Call back and Lamba to allow for many functions to be called when CheckTime() returns true.
digitalWrite(13, !digitalRead(13)); // Blink onboard LED
Serial.println (TimerZ.GetNow()); // send the time now
if( TimerZ.GetNow() == 50000) TimerZ.StopTimer(); // Stop the timer after 50 seconds
});
/*************************************************************************************************/
// Testing all functions:
// The below functions return a pointer to itself so that you can simply put a .function to trigger additional functions
TimerZ.StopTimer() // Stopps the timer but and freezes the time delay to be continued upon StartTimer() call.
.StartTimer() // Resumes the timer with exactly the time that is left from when the StopTimer() was called.
.ResetTimer(true) // Resets the timer to continue with full delay from this point. (true timer will not pause at end of run, false Timer will pause see .Pause() for warnings)
.SetWaitTime(1000) // Sets the wait time.
.SetLastTime(TimerZ.GetNow()) // Sets the time when the last trigger would have occured. (We will set it to right now for this example)
.Pause() // At end of timer the timer will stop resetting (*** Waning *** the CheckTime() function will continuously return true after timer completes).
.Every() // Wait Time is added to Last time to get the next time If a long pause occures this will wind up and cause multiple triggers catching up to present time.
.After() // After Wait time the Last time is given the current time to use which could cause loss in time depending on code.
.Micros() // Uses micros() to calculate durations.
.Millis(); // Uses millis() to calculate durations.
ZTimer * ZPoiner = &TimerZ.This(); // Returns the pointer to the ZTimer
unsigned long x = TimerZ.GetNow(); // Returns the time now based on which clock was chosen micros() or millis()
unsigned long y = TimerZ.WaitTime(); // Gets the delay time set with SetWaitTime().
unsigned long z = TimerZ.LastTime(); // Gets the last time that was set when last triggered.
TimerZ.CheckTime(); // returns true upon timer completion . This also triggers the callback function we set with SetCallBack()
/*************************************************************************************************/
// now that we messed arround above lets set up the timer for real
TimerZ.Millis().After().SetWaitTime(1000).ResetTimer(true); // We are using millis() waiting at least 1000 milliseconds and starting the timer now with auto restart on
}
void loop() {
if(TimerZ.CheckTime() == true){ // Check to see if time has expired and if it has run the callback set in the setup then return true for the optional if statement
Serial.println(" The Other way");
}
}
ZTimer.cpp (2.97 KB)
ZTimer.h (913 Bytes)