In real life, all sorts of things happen at the same time: the milk truck eases into your space, and somebody screams somewhere… But in programming, how do you chew gum and walk at the same time?
Today’s microprocessors—including the ATMEGA processors that power the Arduino—run by executing a linear stream of commands very, very quickly. The illusion of concurrency can be created and maintained if the processor is fast enough to make one action seem as though it is done at the same time, even if they are only one microsecond apart. Even so, there is a problem with the C/C++ language that the Arduino development environment uses when you want to manage concurrency.
Take this example: you want to blink a red LED on and off every 500 ms, and at the same time you want to blink a green LED on and off every 250 ms. The delay() function cannot be used, because delays basically put the processor into zombie mode, disabling it from receiving or sending any information. There are Arduino libraries available that can help the programmer with timing to juggle multiple actions, such as Metro, but they are wordy solutions, hamstrung by the linear nature of C itself.
Enter Plumbing, an Arduino interactive development environment which specializes in its ability to conceptualize and implement parallel programming. Plumbing is adapted from the language called occam-π (occam-pi). Occam-π, together with the Transterpreter (which allows Occam-π to be run on small microcontrollers like the AVR ATMEGA chips), runs on an open-sourced Java-based development toolchain.
Throughout the setup process, I referred to an excellent introductory book called Plumbing for the Arduino, by Matthew Jadud, Christian Jacobsen and Adam Sampson, freely available as a downloadable PDF file.
Setting up an Arduino Duemilanove for Plumbing is relatively straightforward. You need to install the latest JAVA distribution, which is available for all major operating systems. The Plumbing IDE is available for Mac, Windows or Ubuntu Linux, but since the dual-boot netbook I use for my development machine uses Ubuntu 10.10 (Maverick Meerkat), and Plumbing hasn’t caught up, I felt compelled to switch over to Windows XP.
Programming using Plumbing/occam-π on the Arduino is made possible when you upload the Transterpreter firmware into the Arduino. Nothing permanent is done to your Arduino unit in the process, because the firmware is really, in the words of the authors of Plumbing for the Arduino, just an overgrown Arduino sketch. So if you want to go back to Wiring, the conventional Arduino development environment, there’s nothing you need to do. The obvious drawback is that the Transterpreter takes up a good 23k of memory, leaving room for only small programs.
Once installed, the book guides you through parallel programming using Plumbing. One unusual thing (from my perspective as an artist/designer) is how the authors encourage the use of process diagrams to visualize what you’d like to do.
In this 11-line program, four LEDs blink at different rates, while a button toggles a fifth LED.