Skip to content

Commit 7e60fc1

Browse files
authored
Update TaskSync.ino
1 parent 97b53d9 commit 7e60fc1

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

examples/TaskSync/TaskSync.ino

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
#include <Taskfun.h>
22

3-
// synchronize access to this shared global variable
3+
// shared variable to synchronize tasks
44
SyncVar<bool> _on;
55

6-
// function that turns LED on or off depending on argument
7-
void OnOff(bool isOn) {
6+
// task to turn the LED on
7+
void On(int&) {
88
while (1) {
9-
if (_on == isOn) {
10-
digitalWrite(LED_BUILTIN, isOn);
9+
if (_on) {
10+
digitalWrite(LED_BUILTIN, HIGH);
11+
delay(1000);
12+
_on = false;
13+
}
14+
}
15+
}
16+
17+
// task to turn the LED off
18+
void Off(int&) {
19+
while (1) {
20+
if (!_on) {
21+
digitalWrite(LED_BUILTIN, LOW);
1122
delay(1000);
12-
_on = !_on;
23+
_on = true;
1324
}
1425
}
1526
}
1627

1728
void setup() {
1829
pinMode(LED_BUILTIN, OUTPUT);
19-
20-
// initialize multitasking
2130
setupTasks();
22-
23-
// ensure no task switching while initializing
2431
noInterrupts();
25-
// add OnOff task as a task responsible for LED On state
26-
runTask(OnOff, true);
27-
// add OnOff task as a task responsible for LED Off state
28-
runTask(OnOff, false);
29-
// resume multitasking
32+
runTask(On, 0 /* unused argument */);
33+
runTask(Off, 0);
3034
interrupts();
3135
}
3236

3337
void loop() {
3438
yield(); // give up CPU
35-
}
39+
}

0 commit comments

Comments
 (0)