-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathBlinkSlowFastRoutine.ino
128 lines (111 loc) · 3.13 KB
/
BlinkSlowFastRoutine.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* This sketch uses 3 coroutines:
* * the 'blinkSlow' coroutine to blink the LED slowly.
* * the 'blinkFast' coroutine blinks the LED quickly.
* * The 'button' coroutine scans the D2 pin for a button press. Each time
* the button is pressed, the blink mode cycles from "both", "fast" only, to
* "slow" only, then "both" again.
*
* Communication between the coroutines happens through the 'blinkState' global
* variable. See BlinkCustomCoroutine to see how this communication can happen
* through methods on custom Coroutine classes.
*/
#include <Arduino.h>
#include <AceRoutine.h>
using namespace ace_routine;
#ifdef LED_BUILTIN
const int LED = LED_BUILTIN;
#else
// Some ESP32 boards do not define LED_BUILTIN. Sometimes they have more than
// 1. Replace this with the proper pin number.
const int LED = 5;
#endif
// Replace with the pin of your button.
const int BUTTON_PIN = 2;
// If the LED of your board is wired in reverse, flip these values.
const int LED_ON = HIGH;
const int LED_OFF = LOW;
const int BLINK_STATE_SLOW = 0;
const int BLINK_STATE_FAST = 1;
const int BLINK_STATE_BOTH = 2;
int blinkState = BLINK_STATE_SLOW;
COROUTINE(blinkSlow) {
COROUTINE_LOOP() {
switch (blinkState) {
case BLINK_STATE_BOTH:
case BLINK_STATE_SLOW:
Serial.print("S1 ");
digitalWrite(LED, LED_ON);
COROUTINE_DELAY(500);
Serial.print("S1a ");
Serial.print("S0 ");
digitalWrite(LED, LED_OFF);
COROUTINE_DELAY(500);
Serial.print("S0a ");
break;
default:
Serial.print("S ");
COROUTINE_DELAY(1000);
}
}
}
COROUTINE(blinkFast) {
COROUTINE_LOOP() {
switch (blinkState) {
case BLINK_STATE_BOTH:
case BLINK_STATE_FAST:
Serial.print("F1 ");
digitalWrite(LED, LED_ON);
COROUTINE_DELAY(200);
Serial.print("F1a ");
Serial.print("F0 ");
digitalWrite(LED, LED_OFF);
COROUTINE_DELAY(200);
Serial.print("F0a ");
break;
default:
Serial.print("F ");
COROUTINE_DELAY(400);
}
}
}
COROUTINE(button) {
static int buttonState = HIGH;
static int prevButtonState = HIGH;
COROUTINE_LOOP() {
buttonState = digitalRead(BUTTON_PIN);
if (prevButtonState == HIGH && buttonState == LOW) {
// primitive debouncing
COROUTINE_DELAY(20);
buttonState = digitalRead(BUTTON_PIN);
if (prevButtonState == HIGH && buttonState == LOW) {
blinkState++;
if (blinkState >= 3) {
blinkState = 0;
}
if (blinkState == BLINK_STATE_BOTH) {
Serial.println("both");
} else if (blinkState == BLINK_STATE_FAST) {
Serial.println("fast");
} else if (blinkState == BLINK_STATE_SLOW) {
Serial.println("slow");
}
}
}
prevButtonState = buttonState;
COROUTINE_DELAY(20);
}
}
void setup() {
#if ! defined(EPOXY_DUINO)
delay(1000);
#endif
Serial.begin(115200);
while (!Serial); // Leonardo/Micro
pinMode(LED, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
CoroutineScheduler::setup();
}
void loop() {
CoroutineScheduler::loop();
}