Add a timer to turn off after a period of time. Move the LED name to reduce string literals.

This commit is contained in:
Robert Marshall 2021-08-27 09:02:35 +01:00
parent 59ec76b52f
commit c3e91d269e
5 changed files with 93 additions and 12 deletions

32
src/Timer.cpp Normal file
View file

@ -0,0 +1,32 @@
#include <Arduino.h>
class Timer{
private:
unsigned long _interval, _lastTick;
void (*_callback)(void);
bool _running;
public:
Timer(void (*callback)(void)){
_callback = callback;
_running = false;
}
void reset(unsigned long interval){
_interval = interval;
_lastTick = millis();
_running = true;
}
void loop(){
if (!_running)
return;
unsigned long tick = millis();
if (tick - _lastTick >= _interval){
_callback();
_running = false;
}
}
};