Cross-posted from my personal blog:Â http://www.meatandnetworking.com/code/introducing-arduino-simple-task-scheduler/
Get the code here:Â https://github.com/Zuph/AVRQueue
Introducing the Arduino Simple Task Scheduler. Â This is part of the balloon flight computer code I wrote for White Star, with some more polish. This library allows you to create a schedule queue of functions to be executed at specified times, on specified intervals. Â For example, say you're trying to log some sensor data and update a display in the same program. With the task scheduler, you can simply write a function to gather sensor data, write a function to update the display, add them to your queue, and let the library handle the rest.
This isn't really useful for blinking LEDs, but it's great for complex systems. For example, the balloon computer was gathering sensor data, sending short reports, sending long reports, monitoring vertical speed, monitoring GPS Status, monitoring flight state, managing ballast, and managing a backup high-frequency radio at the same time. Â Halfway through development, it was obvious that we would need to integrate a watchdog timer to keep other systems from freezing the flight computer. Â If all of these tasks had been occurring simultaneously, spread throughout spaghetti code, it would have been very difficult to add watchdog resets in all the right places. Â With the task queue, I simply defined another function that reset the watchdog, and put it in the queue. Â Two minutes, tops!
You can find extensive documentation and examples in the Github project. Â To install, just copy the "Arduino" directory contents to the "Libraries" folder of your Arduino IDE install. Â Restart your IDE, and it should pop right up. Â Here's a really simple example program:
#include <Queue.h> void setup() { pinMode(13, OUTPUT); Serial.begin(9600); Serial.println("Alive"); Queue myQueue; myQueue.scheduleFunction(testFunction, "Test", 5000, 1000); while(1) { myQueue.run(millis()); delay(10); } } int testFunction(unsigned long now) { Serial.print("Hello: "); Serial.println(now); }