-
Notifications
You must be signed in to change notification settings - Fork 0
/
flex_timers_v2.ino
76 lines (52 loc) · 1.38 KB
/
flex_timers_v2.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <Eventually.h>
EvtManager mgr;
#define FAST_SLOW_MODE 2
#define AUTO 3
#define CLOCKLED 5
#define BUTTON_PIN 7
void sleep(int sleep_time)
{
long start_time = millis();
while(millis() < start_time + sleep_time)
{
// do nothing
}
}
void toggle_clock()
{
PORTD |= 0b00100000; // sets digital pin 5 HIGH
sleep(10);
PORTD &= 0b00000000; // sets digital pin 5 LOW
sleep(10);
}
bool single_step()
{
toggle_clock();
return false; // tells event manager to continue
}
void manual_auto()
{
mgr.resetContext();
if(digitalRead(AUTO))
{
if(digitalRead(FAST_SLOW_MODE) )
{ mgr.addListener(new EvtTimeListener(1, true, (EvtAction)toggle_clock)); }
else
{ mgr.addListener(new EvtTimeListener(1000, true, (EvtAction)toggle_clock)); }
}
else
{
mgr.addListener(new EvtPinListener(BUTTON_PIN,(EvtAction)single_step)); // Manual stepping listener
}
}
void setup() {
// set up the pins and the event listeners
pinMode(CLOCKLED, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
// Set up interupts for when toggles change
attachInterrupt(0, manual_auto, CHANGE);
attachInterrupt(1, manual_auto, CHANGE);
// Manual or automatic clock
manual_auto();
}
USE_EVENTUALLY_LOOP(mgr) // Uses this instead of the usual loop() function.