/
light_switch.ino
61 lines (50 loc) · 1.29 KB
/
light_switch.ino
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
#include "Fsm.h"
// State machine variables
#define FLIP_LIGHT_SWITCH 1
State state_light_on(on_light_on_enter, NULL, &on_light_on_exit);
State state_light_off(on_light_off_enter, NULL, &on_light_off_exit);
Fsm fsm(&state_light_off);
// Transition callback functions
void on_light_on_enter()
{
Serial.println("Entering LIGHT_ON");
}
void on_light_on_exit()
{
Serial.println("Exiting LIGHT_ON");
}
void on_light_off_enter()
{
Serial.println("Entering LIGHT_OFF");
}
void on_light_off_exit()
{
Serial.println("Exiting LIGHT_OFF");
}
void on_trans_light_on_light_off()
{
Serial.println("Transitioning from LIGHT_ON to LIGHT_OFF");
}
void on_trans_light_off_light_on()
{
Serial.println("Transitioning from LIGHT_OFF to LIGHT_ON");
}
// standard arduino functions
void setup()
{
Serial.begin(9600);
fsm.add_transition(&state_light_on, &state_light_off,
FLIP_LIGHT_SWITCH,
&on_trans_light_on_light_off);
fsm.add_transition(&state_light_off, &state_light_on,
FLIP_LIGHT_SWITCH,
&on_trans_light_off_light_on);
}
void loop()
{
// No "fsm.run_machine()" call needed as no "on_state" funcions or timmed transitions exists
delay(2000);
fsm.trigger(FLIP_LIGHT_SWITCH);
delay(2000);
fsm.trigger(FLIP_LIGHT_SWITCH);
}