For some time, I’ve been doodling around with various open-source Real-time operating system (RTOS) options out there. There are quite a few to get lost in…
But first, what is an RTOS, and why would you want one?
The RTOS is code which can manage multiple tasks in a computer. You can see what it does by considering what sort of code you’d write if you wanted to periodically read out some sensors, not necessarily all at the same time or equally often. Then, perhaps you want to respond to external events such as a button press of a PIR sensor firing, and let’s also try and report this on the serial port and throw in a command-line configuration interface on that same serial port…
Oh, and in between, let’s go into a low-power mode to save energy.
Such code can be written without RTOS, in fact that’s what I did with a (simpler) example for the roomNode sketch. But it gets tricky, and everything can become a huge tangle of variables, loops, conditions, and before you know it … you end up with spaghetti!
In short, the problem is blocking code – when you write something like this, for example:
void setup () {}
void loop () {
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(900);
}
The delay() calls will put the processor into a busy loop for as long as needed to make the requested number of milliseconds pass. And while this is the case, nothing else can be done by the processor, other than handling hardware interrupts (such as timer ticks).
What if you wanted to respond to button presses? Or make a second LED blink at a different rate at the same time? Or respond to commands on the serial port?
This is why I added a MilliTimer class to JeeLib early on. Let’s rewrite the code:
MilliTimer ledTimer;
bool ledOn;;
void setup () {
leadTimer.set(1); // start the timer
}
void loop () {
if (ledTimer.poll()) {
if (ledOn) {
digitalWrite(LED, LOW);
ledTimer.set(900);
} else {
digitalWrite(LED, HIGH);
ledTimer.set(100);
}
ledOn = ! ledOn;
}
// anything ...
}
It’s a bit more code, but the point is that this implementation is no longer blocking: instead of stopping on a delay() call, we now track the progress of time through the MilliTimer, we keep track of the LED state, and we adjust the time to wait for the next change.
As a result, the comment line at the end gets “executed” all the time, and this is where we can now perform other tasks while the LED is blinking in the background, so to speak.
You can get a lot done this way, but things do tend to become more complicated. The simple flow of each separate activity starts to become a mix of convoluted flows.
With a RTOS, you can create several tasks which appear to all run in parallel. You don’t call delay(), but you tell the RTOS to suspend your task for a certain amount of time (or until a certain event happens, which is the real magic sauce of RTOS’es, actually).
So in pseudo code, we can now write our app as:
TASK 1:
turn LED on
wait 100 ms
turn LED off
wait 900 ms
repeat
MAIN:
start task 1
do other stuff (including starting more tasks)
All the logic related to making the LED blink has been moved “out of the way”.
Tomorrow I’ll expand on this, using an RTOS which works fine in the Arduino IDE.




