Arduino · 2 August 2012 17

looper, a software scheduler for Arduino

After the release of leOS, someone brought my attention on the fact that often third-party libraries use timer 2 so they are not compatible with my leOS. Someone else asked me for a simplier scheduler, that could be beyond microcontroller’s timers & interrupts.

So, working on the code of leOS I’ve obtained looper, that is just a routine that executed at fixed intervals other sub-routines. looper doesn’t use timers nor interrupts: it only uses the millis() function of the Arduino core.

Unlike leOS, the user has to manually call looper inside the main loop of his sketch but, unlike leOS, it consumes much less resources and it doesn’t rely on the hardware on which it’s running, so it can be compiled on any kind of microcontroller that can be supported by the Arduino IDE.

To use looper, just download the attached packaged and extract it into your /libraries folder (usually /home/user/sketchbook/libraries on Linux boxes, \Documents\Arduino\libraries on Windows boxes). After that, include the core library into your sketch’s code and create a new instance of looper:

#include "looper.h"
 looper myScheduler;

Now you’re ready to use looper. It has these methods with which you can manage your routines, that now have the name of “jobs”:

myScheduler.addJob(function, interval_in_ms[, ONETIME]);
 myScheduler.removeJob(function);
 myScheduler.pauseJob(function);
 myScheduler.restartJob(function);

The sintax is exactly the same of that one of leOS with the difference that now the keyword “Task” has been replaced by “Job”. With .addJob you add a job named “function” into the scheduler to be executed every “interval_in_ms” milliseconds. Using the optional paramether ONE_TIME you tell looper that this task has to be executed only once.

.removeJob just removes a job while .pauseJob and .restartJob pause a task and start it again, respectively. To let looper work, you have to call the .scheduler() method in any point of your main loop:

void loop() {
 ...
 myScheduler.scheduler();
 }

An interesting feature of looper is the new method .myDelay() that can stop the execution of the code of the main loop() while it still continues to execute the jobs that are present into the scheduler. To use this feature you just have to use the method .myDelay instead of the Arduino function delay():

...
 myScheduler.myDelay(interval in ms);
 ...

Due to the fact that looper doesn’t use interrupts, its precision relies completely on the duration of the main loop. The main loop must have an execution time that has to be less than the shortest interval of your scheduled tasks otherwise they will be executed with an interval what will be equal to that one of the main loop. Remember that looper is just a routine that executes other routines.

Some examples are included into the library to show how to use looper.

[notice]The code that used the previous version of looper has to be checked to change old methods and use the new syntax.[/notice]

[important]NOTE FOR leOS USERS:
since version 1.0.0, looper can be used together with leOS: an example shows how to use both in a sketch.[/important]

Looper
Looper
looper_1.1.1.zip
Version: 1.1.1
150.1 KiB
3438 Downloads
Details...