/
blink_led.ino
44 lines (35 loc) · 954 Bytes
/
blink_led.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
#include <async.h>
void blinkLed();
void stopInterval();
Async asyncEngine = Async(); // Instances the engine
short id = -1;
void setup() {
pinMode(13, OUTPUT);
id = asyncEngine.setInterval(blinkLed, 10); // Save the id to stop later
asyncEngine.setTimeout(stopInterval, 10000); // Stops the blinking function after 10 seconds
}
void loop() {
asyncEngine.run(); // Runing the engine
}
/*
Blink the internal led 1 time per second
Using millis to avoid blocking code
millis: https://www.arduino.cc/reference/en/language/functions/time/millis/
*/
void blinkLed() {
static unsigned long start = millis();
if((millis() - start) >= 500 && (millis() - start) < 1000) {
digitalWrite(13, HIGH);
}
if((millis() - start) >= 1000) {
digitalWrite(13, LOW);
start = millis();
}
}
/*
Interrupts the interval that causes the led to blink
*/
void stopInterval() {
digitalWrite(13, LOW);
asyncEngine.clearInterval(id);
}