-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToggle.c
More file actions
73 lines (67 loc) · 2.03 KB
/
Toggle.c
File metadata and controls
73 lines (67 loc) · 2.03 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
/* Finite State Machine Implementation Example
* Toggle
*
* ┌─┐
* │ │
* ├─┤
* └─┘
* |
* |
* ┌───────────┐
* +-->│State1 │---+
* | ├───────────┤ |
* | │Set LED Off│ |
* | └───────────┘ |
* |Input |Input
* |Rising Edge |Rising Edge
* | ┌───────────┐ |
* +---│State2 │<--+
* ├───────────┤
* │Set LED On │
* └───────────┘
*
* Notes:
* Condition evaluation priority is determined by the order in the
* evaluation sequence.
* State actions should be short and non-blocking. Long or complex
* actions should be subdivided into additional states or into a
* sub-machine.
*
* I/O scanning can be on-demand (each condition reads its I/O) or
* snapshot-based (all I/O is read once at the beginning of the loop
* and then frozen).
* The scanning method impacts the evaluation priority of the conditions.
*
* Created 25-Sep-2024 by Simón Plata
* Revised 25-Aug-2025 by Simón Plata
*
*/
void fsm_update() {
enum fsm_states {
state_0,
state_1,
state_2
};
static enum fsm_states state = state_0;
static int8_t Previous_Input_Value = LOW;
int8_t Input;
Input = !digitalRead(PushButton);
switch(state) {
case state_0: if(1) {
state = state_1;
digitalWrite(LED, LOW);
}
break;
case state_1: if((Input == HIGH) && (Input != Previous_Input_Value)) { // state_1 --> state_2 transition for Input rising edge
state = state_2;
digitalWrite(LED, HIGH);
}
break;
case state_2: if((Input == HIGH) && (Input != Previous_Input_Value)) { // state_2 --> state_1 transition for Input rising edge
state = state_1;
digitalWrite(LED, LOW);
}
break;
}
Previous_Input_Value = Input;
}