Arduino-Scheduler
Portable Collaborative Multi-Tasking Scheduler for Arduino
SchedulerBlinkController.ino
Go to the documentation of this file.
1 
23 #include <Scheduler.h>
24 
25 // Check for SparkFun SAMD21 Breakout
26 #if defined(ARDUINO_ARCH_SAMD) && (USB_PID == 0x8D21)
27 #define Serial SerialUSB
28 #endif
29 
30 const int LED = 13;
31 volatile unsigned int period = 0;
32 
33 void setup()
34 {
35  Serial.begin(57600);
36  while (!Serial);
37  Serial.println(F("SchedulerBlinkController: started"));
38  Serial.println(F("0 - stop, 1 - start controller"));
39 
40  pinMode(LED, OUTPUT);
41  digitalWrite(LED, LOW);
42  Serial.print(millis());
43  Serial.println(F(":LED off"));
44  Serial.flush();
45 
48 }
49 
50 void loop()
51 {
52  await(period != 0);
53  digitalWrite(LED, HIGH);
54  delay(period);
55  digitalWrite(LED, LOW);
56  if (period == 0) return;
57  delay(period);
58 }
59 
61 {
62  unsigned int ms = 100;
63  for (; ms < 1000; ms += 100) {
64  delay(2000);
65  if (period == 0) return;
66  period = ms;
67  Serial.print(millis());
68  Serial.print(F(":period = "));
69  Serial.println(ms);
70  }
71  for (; ms > 100; ms -= 100) {
72  delay(2000);
73  if (period == 0) return;
74  period = ms;
75  Serial.print(millis());
76  Serial.print(F(":period = "));
77  Serial.println(ms);
78  }
79 }
80 
81 void buttonLoop()
82 {
83  unsigned long start = millis();
84  await(Serial.available() != 0);
85  unsigned long stop = millis();
86  unsigned long ms = stop - start;
87  int c = Serial.read();
88  if (c == '0' && period != 0) {
89  Serial.print(stop);
90  Serial.print(':');
91  Serial.print(ms);
92  Serial.println(F(":LED off"));
93  period = 0;
94  }
95  else if (c == '1' && period == 0) {
96  Serial.print(stop);
97  Serial.print(':');
98  Serial.print(ms);
99  Serial.println(F(":LED on"));
100  period = 100;
101  }
102 }
const int LED
static bool startLoop(func_t taskLoop, size_t stackSize=DEFAULT_STACK_SIZE)
Definition: Scheduler.h:65
#define await(cond)
Definition: Scheduler.h:162
void controllerLoop()
void buttonLoop()
volatile unsigned int period
SchedulerClass Scheduler
Definition: Scheduler.cpp:53
void setup()