Arduino-Scheduler
Portable Collaborative Multi-Tasking Scheduler for Arduino
SchedulerBlinkMax.ino
Go to the documentation of this file.
1 
44 #include <Scheduler.h>
45 
46 #if defined(ARDUINO_ARCH_AVR)
47 const size_t STACK_SIZE = 64;
48 #else
49 const size_t STACK_SIZE = 256;
50 #endif
51 
52 int next_pin = 2;
54 
55 void blink()
56 {
57  // Allocate a pin and set output mode
58  int pin = next_pin++;
59  pinMode(pin, OUTPUT);
60 
61  // Toggle the pin on and off
62  while (1) {
63  int bytes = Scheduler.stack();
64  if (bytes < min_stack) min_stack = bytes;
65  digitalWrite(pin, HIGH);
66  delay(500);
67  digitalWrite(pin, LOW);
68  delay(500);
69  }
70 }
71 
72 void setup()
73 {
74  Serial.begin(57600);
75  while (!Serial);
76  Serial.println(F("SchedulerBlinkMax: started"));
77  Serial.flush();
78 
79  // Start a blink task for each pin (use 64 byte stack)
80  int tasks = 0;
81  const int TASKS_MAX = NUM_DIGITAL_PINS - 2;
82  while (tasks < TASKS_MAX && Scheduler.startLoop(blink, STACK_SIZE)) tasks++;
83  Serial.print(millis());
84  Serial.print(F(":setup::task="));
85  Serial.println(tasks);
86 
87  Serial.print(millis());
88  Serial.print(F(":setup::main:stack="));
89  Serial.println(Scheduler.stack());
90 }
91 
92 void loop()
93 {
94  unsigned long start = millis();
95 
96  // Print the iteration count
97  static unsigned int i = 0;
98  Serial.print(millis());
99  Serial.print(F(":loop::i="));
100  Serial.println(i++);
101 
102  Serial.print(millis());
103  Serial.print(F(":loop::main:stack="));
104  Serial.println(Scheduler.stack());
105 
106  Serial.print(millis());
107  Serial.print(F(":loop::min:stack="));
108  Serial.println(min_stack);
109 
110  // Delay gracefully
111  await((millis() - start) >= 1000);
112 }
static bool startLoop(func_t taskLoop, size_t stackSize=DEFAULT_STACK_SIZE)
Definition: Scheduler.h:65
#define await(cond)
Definition: Scheduler.h:162
int next_pin
const size_t STACK_SIZE
int min_stack
void setup()
void blink()
static size_t stack()
Definition: Scheduler.cpp:127
void loop()
SchedulerClass Scheduler
Definition: Scheduler.cpp:53