[tutorial] IRRemoteControl

IRRemoteControl Tutorial from codebender on Vimeo.

IRRemoteControl is a wrapper on the IRremote library by Ken Shirriff. IRremote aims to handle the communication between remotes, Arduinos, and devices controlled with IR. IRRemoteControl, on the other hand, doesn't add anything on that part, but all it does is help you structure your code better. All you have to do is define a function, match it with a button, and you are done.

project_zps303b93c1

The library has been set up for the IR Remote Control from Sparkfun.

The library has two accompanying examples, IRControlTest and IRLEDStrip. You can use the IRControlTest example to check that everything is working properly, and the IRLEDStrip example to set up your remote for controlling an addressable LED strip.

We will examine the first example to get introduced to the interface of the library, and then we will look at the IRLEDStrip example to see how the remote operates with an LED strip.

First, make the following connections. You will need the LED strip for the second example. Please read the notes on this page from Adafruit for powering the NeoPixels. (Personally, I was lucky enough in my experimentation to damage only 3 of the NeoPixels and not the whole strip.)

schematic_zps754bf158

IRControlTest


The code, as you can see, is very short. At the beginning, we make sure to include the necessary libraries (lines 8,9), we declare the IR receiver pin (line 11) and then we create an IRRemoteControl instance (line 14).

Inside setup, we call enable to start the receiver (line 18) and then we match buttons with functions (lines 19-27) that we will have to have defined earlier (lines 39-47). We do this by calling the pair method. Its first argument is the button and the second argument is the name of the function to pair with the button.

Inside loop, we call check (line 36) to see if we have received any data, and if so, the data will get decoded and the appropriate function will be called.

The functions you will define for the buttons will have to take no arguments and return no value. If you want to pass or return a variable to a function, declare it as a global variable. You can see examples of this on the IRLEDStrip example.

IRLEDStrip


The idea here is the same as before. So now I will just explain how to buttons act on the LED strip and let you verify that by reading the code.

The POWER button turns the strip on and off.

The A, B, C buttons set a red, green, and blue color on the strip, respectively.
If we press an A-B-C sequence with successive button presses executed within 1 second, this adds the corresponding values and lets us set our own color. Pressing A-B gives yellow. Pressing A-C gives magenta. Pressing B-C gives cyan. And pressing A-B-C gives white.

The SELECT button sets a random color.

The LEFT and RIGHT buttons display a window on the rainbow colors and shift this window in opposite directions.

And finally the UP and DOWN buttons, increase and decrease brightness on the strip, respectively.

That's all folks. We hope you enjoy the library, and if you have any comments or suggestions you can contact us at girder [at] codebender [dot] cc


Sounds are from freesound.org. Schematics were based on Fritzing.