-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTiming.h
49 lines (44 loc) · 1.27 KB
/
Timing.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// easy to use timing that handles millis() overflow
// example:
// create timer: Timing mytimer;
// in setup(): mytimer.begin(0); // 0=catchup, 1=always use delay even if late
// in loop(): if (mytimer.isOver(1000) {
// do something every 1000ms
// }
// This file Copyright (c) 2016 Peter Scargill
//
#ifndef Timing_h
#define Timing_h
#include <Arduino.h>
class Timing
{
private:
unsigned long counter;
boolean catchup;
public:
void begin(boolean cat)
{
counter=millis();
catchup=cat;
}
boolean onTimeout(unsigned long interval)
{
unsigned long newmillis=millis(); // temp vars so easier comparison
if ((newmillis-counter)>=interval)
{
if (catchup) counter=newmillis; else counter+=interval;
return true;
}
return false;
}
void onTimeout(unsigned long interval, void (*g)()) // this version pass function as parameter
{
unsigned long newmillis=millis(); // temp vars so easier comparison
if ((newmillis-counter)>=interval)
{
if (catchup) counter=newmillis; else counter+=interval;
g();
}
}
};
#endif