-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiThread.ino
61 lines (47 loc) · 1.2 KB
/
MultiThread.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 <LinkedList.h>
#include "Led.h"
#include "MicroSwitch.h"
#include "StateController.h"
#include "Joystick.h"
#include "RGBLed.h"
#include "RotaryEncoder.h"
#define MAX_TIME -1
MicroSwitch toggleSwitchA(2);
MicroSwitch toggleSwitchB(3);
StateController stateController;
unsigned long lastUpdate;
void setup()
{
stateController.Reset();
stateController.Register(&toggleSwitchA);
stateController.Register(&toggleSwitchB);
lastUpdate = micros();
Serial.begin(9600);
}
int lastSwitchAState = -1;
int lastSwitchBState = -1;
void loop()
{
unsigned long now = micros();
long updateTime = 0;
if (now < lastUpdate) {
updateTime = now + (MAX_TIME - lastUpdate);
}
else {
updateTime = now - lastUpdate;
}
lastUpdate = now;
stateController.Update(updateTime);
int switchState = toggleSwitchA.GetState();
if (switchState != lastSwitchAState) {
Serial.write("toggleSwitchA\n");
Serial.write(switchState == LOW ? "Low\n" : "High\n");
lastSwitchAState = switchState;
}
switchState = toggleSwitchB.GetState();
if (switchState != lastSwitchBState) {
Serial.write("toggleSwitchB\n");
Serial.write(switchState == LOW ? "Low\n" : "High\n");
lastSwitchBState = switchState;
}
}