Using dual Arduinos to overcome interrupt conflicts
|WS2811 Addressable RGB LED Strip||$17|
|5v 3A Power Supply||$7|
|IR receiver and remote||$10|
While building a remote controlled led beacon for my lighthouse bookshelf, I learned that one single arduino can’t reliably receive commands from an IR module and control a demanding animation on an LED strand at the same time.
The trouble is that both operations are highly time dependent. In simple terms, the IR module needs to be able to interrupt execution the moment it receives a command or the command will be lost or garbled. That is only a problem if the arduino is doing other things that can’t wait a few milliseconds. Unfortunately, updating LEDs is one of those things. The data transmission protocol for most of these LED strands is all based on timing, and you can’t stop in the middle of the stream. And of course, when I am trying to animate the LEDs, I want the arduino streaming updates out to them constantly, meaning there is never an acceptable time to wait around on the IR receiver.
What we need is a buffer. We need something to be free to listen for commands from the IR receiver constantly, and that can store those commands until our LED streaming process has a break to check for new commands. For example, in between LED strand updates, and we want that break to be as short as possible so we can get back to updating LEDs.
Fortunately for us, the serial communications system (called a UART) on the arduino has just such a buffer. When you send something through the serial communications system, it actually buffers what you send in the UART until the receiving end acknowledges that you have something to send and requests the transmission.
So here is how it works: The arduino listening to the IR receiver gets a command, validates it, and then sends an appropriate command out to the LED arduino. That command actually gets buffered in the UART of the IR receiver arduino until the LED arduino requests it. On the LED arduino, each time we finish streaming a set of commands to the LEDs, we take a brief break and check to see if there are any commands waiting on the IR receiver arduino. If so, we grab them and deal with them appropriately, if not, we run the next LED update.
The wiring couldn’t be easier, the serial send pin on one arduino (pin 1) connects to the receive pin on the other (pin 0), and vice versa.
The only hitch here is that the usb connection on the arduino actually uses those pins for sending programs, so as long as the two arduinos are connected this way and powered up, you wont be able to program them through the usb cable. To get around that, I just put a double pole single throw switch between those two connections so that I can disconnect the arduinos from each other for programming by flipping a switch.
Drop by my lighthouse post for some code, and feel free to ask questions!