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:

 

 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.

Comments on this entry (17 comments)

Did you like this post? You can share your opinion with us! Simply click here.

Henning

Hello Brad,

I tried the example, but I will only get the following error message:

Queue.cpp: In function ‘void setup()’:
Queue.pde:-1: error: ‘class Queue’ has no member named ‘run’

Can You please help and fix it?

Regards,
Henning.

Reply
AWOL

Doesn’t it have a member called “Run”?

Reply

Try “myQueue.Run(millis());” with a capital R.
Then you get another error about function “loop”. Just add a loop in the code, after the setup() function, something like “void loop() { delay(10); }”

Reply
Henning

Hi iard,

that does the trick!

Thanks.

Reply
effgee

Thanks for tips above. The example as given has a couple of capitalisation typos, and slips random milliseconds apparently due to the delay(10) in void setup().

This worked perfectly for me:
//==================================
#include

void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
Serial.println(“Alive”);

Queue myQueue;
myQueue.scheduleFunction(testFunction, “Test”, 5000, 1000);

while(1) {
myQueue.Run(millis());
}
}

int testFunction(unsigned long now)
{
Serial.print(“Hello: “);
Serial.println(now);
}

void loop() {

}

Reply
effgee

d’oh. First line should read

#include

Reply
effgee

ok, pointy braces are being interpreted as tags. Third time lucky:
First line should be #include queue.h, i.e. lower case q

Reply
Tad

This reminds me of the task scheduler used on the Apollo Guidance Computer.

Reply

I tend to use a similar, but slightly less dynamic approach:

http://petevidler.com/2011/03/simple-co-operative-scheduling/

Reply
David

Hi,

I’ve downloaded the Arduino IDE. I looked under the Arduino directory which is located under the Library directory on my Mac. I downloaded your Arduino folder, moved its contents the MY arduino directory. When I started the Arduino IDE again and ran the sample code I got the error message

Reply

What error message are you getting? Are you using the Arduino 1.x IDE? What does your Arduino folder directory structure look like?

Reply
David

On my Mac /dharr19/Library/Arduino/ at this point I created a directory called Library

/dharr19/Library/Arduino/Library/

The error I get is “Queue was not declared in this scope”. Oh I did capitalize the r in run

Reply
David

these were the files from your Arduino directory that I put into my library folder

Examples directory
Queue.cpp
Queue.h
Test directory

Reply

David, you shouldn’t have needed to create the Library directory. This error indicates that the arduino IDE couldn’t find the library. If you could, email me with a complete file listing for your arduino tree. Screenshots or terminal copy-paste is fine. My email address is BradLuyster@gmail.com

Reply
Joseph

Useful !
For the polish, if you want it ? :)

Queue KEYWORD1
queuedFunction KEYWORD1

scheduleFunction KEYWORD2
scheduleRemoveFunction KEYWORD2
scheduleChangeFunction KEYWORD2
Run KEYWORD2

Reply

Add Your Comment