A Better Real-time Library for Arduino

For my project I required a simple timer library for the Arduino development environment, something with a simple-but-powerful interface to schedule tasks to complete in the future or at regular intervals.

There doesn’t really seem to be anything suitable online – a host of over-complicated APIs that use several classes, structs and rely heavily on other libraries. Moreover, these tend to poll as part of the main loop whereas I wanted something that will run in the background leaving the processor free to work on more important stuff.

My library (attached here) achieves this – a lightweight, interrupt-driven scheduler that can run tasks at a second resolution.

The API is really simply:

  • Timer();
    Handles all initialisation functionality.
  • timer_h once(int interval, callback_h callback);
    Schedules a new, one-shot timer to invoke the function callback at interval seconds in the future.
  • timer_h repeat(int interval, callback_h callback);
    Schedules a new timer to invoke the function callback every interval seconds, until the timer is deactivated.
  • void enable(timer_h timer);
    Enables a timer, referenced by timer, that has previously been disabled.
  • void disable(timer_h timer);
    Disables a timer, referenced by timer, effectively pausing it, whilst leaving it active on the system.
  • void destroy(timer_h timer);
    Removes a timer from the scheduler, freeing up the space for a new timer.

Currently, up to 10 timers are supported, although the MAX_TIMER definition in Timer.h can adjust this for more functionality or to save memory. The timer variable must be called RTOS.

Code example:

#include

// This is a nasty hack to resolve some weirdo errors in where we
// redefine some system types and functions, for some unknown reason. Rather than
// modify the header file to resolve this conflict, we remove the redefinitions
// here.
#undef int
#undef abs
#undef double
#undef float
#undef round
// End Hack

Timer RTOS = Timer();
timer_h rep;

void setup() {
Serial.begin(9600);

RTOS.once(5, t_once);
rep = RTOS.repeat(1, t_rep);
}

void t_once(void) {
Serial.println("World!");
RTOS.destroy(rep);
}

void t_rep(void) {
Serial.print("\nHello ");
}

void loop() { /* nil */ }

The library has been tested on an Arduino Duemilanove, with an ATmega328.

Download: RTOS.zip

Trackback URL

  1. Dan
    16/06/2009 at 7:47 pm Permalink

    Thanks for this, much appreciated.

  2. Johann
    04/08/2009 at 12:29 pm Permalink

    Awesome little library – simple & sweet. I can’t wait to try it. I do have two questions though:

    On an 8Mhz ATmega328, what is the max number of seconds you can sleep?

    Will “once” and “repeat” wake the ATmega328 up from sleep modes? From the datasheet, I’m guessing so, but it’d be nice to have confirmation.

  3. Daniel Bradberry
    04/08/2009 at 10:08 pm Permalink

    @Johann
    The library uses an int type to keep track of the number of seconds elapsed. On my GCC toolchain this is defined to be a 16-bit number (i.e., a maximum of 65536 different values) so it wait for a little over 18 hours. You can check this by getting the sizeof(int) within a program.

    The library should only require the timer oscillator and Timer 2 interrupt vectors enabled. Table 9-1 in the datasheet shows us that this is the case in 4 of the ATmega’s 6 sleep modes (it will not wake from Power Down or Standby).

    Hope that helps!

  4. Steve
    10/10/2009 at 10:11 pm Permalink

    I’m not sure how the libraries are working in 17 anymore but I was able to get this working by putting the definitions and implementations in a single file and including it. It ain’t pretty but at least its working.

Trackbacks

  1. [...] progress on my arduino capstone project. I thought I was so clever for finding a timer code library that ...