-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPLC_timers.h
61 lines (56 loc) · 1.14 KB
/
PLC_timers.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
50
51
52
53
54
55
56
57
58
59
60
61
class TON_ms
{
private:
unsigned long tref = 0;
unsigned long delta = 0;
bool done = false;
public:
bool Q = false; //output true when PT reached.
unsigned long ET = 0; //elapsed time. saturates to PT.
bool IN(bool input, unsigned long PT)
{
if (input) {
if (done) {
ET = PT;
Q = true;
} else {
delta = millis() - tref;
Q = (delta > PT);
if (Q) {
done = true;
ET = PT;
} else {
ET = delta;
}
}
} else {
tref = millis();
ET = 0;
Q = false;
done = false;
}
return Q;
}
};
class Finite_State_Machine
{
public:
int nowState = -1;
int nextState = 0;
uint32_t timeRef = 0;
uint32_t timeout = 0;
bool stateChanged = true;
int update()
{
if (nowState != nextState) {
stateChanged = true;
nowState = nextState;
timeRef = millis();
timeout = 0;
} else {
stateChanged = false;
timeout = millis() - timeRef;
}
return nowState;
}
};