Skip to content

Introducing: Arduino Simple Task Scheduler

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);
}

 

 This will print "Hello: X" where X is the number of milliseconds since startup, starting 5 seconds after startup, and repeating every 1 second.
The Arduino library has some limitations, so I've also included an AVR "library" (just a couple of source files to include in your project).  This one's a little easier to tweak to your specific application, and doesn't suffer some of the same drawbacks as the Arduino library.  That said, the Arduino library will be find for almost every project out there!  The limitations are listed in more detail at the Github site.
If you find any bugs, let me know! Submit a Github issue, fork, fix and submit a pull request, or contact me directly!  If you find this useful, let me know!  It isn't a lot, but I hope it's well documented, and easy to use/read/understand.

Leave a Reply

Your email address will not be published. Required fields are marked *