Waiter class
This class lets you do a non-blocking delay (so the Arduino can continue to execute other code)
- waiter.h
typedef uint32_t clock_t; const clock_t INFINITE = ( (clock_t)(-1) ); const clock_t SEC = 1000; // // waiter: returns true and resets if more than time has passed // class waiter { clock_t last; public: waiter() : last(millis()) {} bool wait( const clock_t time ) { clock_t now = millis(); if( now - last >= time ) { last = now; return true; } return false; }; };
For example, to flash an LED:
- flash.pde
#include <waiter.h> const byte ledPin = 13; const clock_t flashTime = 2 * SEC; waiter ledFlash; bool ledState; void setup() { pinMode( ledPin, OUTPUT ); }; void loop() { if( ledFlash.wait( flashTime ) ) { ledState = !ledState; digitalWrite( ledPin, ledState ); } };
You can reset the timer with:
ledFlash.wait( 0 );