In this section we will show you how to control a pattern of lights on the Circuit Playground Express ring of NeoPixels using an IR remote. You can find the source code in the Adafruit_CircuitPlayground library in the "examples" subfolder. We will not reproduce the entire source listing here but we will make some references to particular portions.
This example has been programmed to use the Adafruit Mini Remote Control which uses NEC protocol. If you do not have that remote available you will have to modify the sketch to use the protocol and codes for your particular remote. We will give you some tips on how to do that later but for now let's look at how the program works as originally written.
You should upload the scanned to your Circuit Playground Express using the Arduino IDE. When the upload has completed the NeoPixels will display a red dot that is moving counterclockwise. This is the default pattern we have programmed.
Try pushing the right arrow button on the remote and the dot will begin to spin clockwise. Pressing the left arrow will make it spin counterclockwise again. If you push the up arrow or down arrow it will go faster or slower. If you press the volume plus or volume minus the brightness will go up or down. If you press the "0" button the pattern will shift to a rotating rainbow pattern. Pressing "1" gives you a red and white "candy cane" pattern. Pressing "2" gives uses a default one red pixel spinning. Pressing "3" or "4" will display a solid color on all pixels and will cycle up and down the rainbow on repeated presses.
How It Works
In the "setup" function which begins at line 65 we initialize the Circuit Playground and enable the IR receiver as well as initialize various variables that control the pattern of the NeoPixels. In the loop function beginning at line 72 we first display the pattern using "Show_Pattern()" followed by a brief delay and then we increment the phase of the pattern wrapping around at 10. Now we check to see if there is a new IR signal. If there is not we return and the loop continues. We then attempt to decode the signal. If that fails we return and the loop executes again. Note that we could get a perfectly valid IR signal but if we can't decode it then we just ignore it, reinitialize the receiver and go on.
If it was decoded, we print out the results on the serial monitor just so we can see what's going on for the fun of it. Next we check to see if this really was NEC protocol. If that succeeds we then go into a "switch" statement online number 107. The switch is based on the decoded value. Depending on what value was received we change the pattern type, direction of spinning, brightness, or speed. Once we made those changes we reinitialize the receiver. On the next execution of the loop when we get "Show_Pattern()" the changes we have made will take effect.
Using a Different Remote
If you are not using the Adafruit Mini Remote you have to modify this program to use the codes of your particular remote. There are 10 functions that you have to support so you will need to pick 10 different buttons on your remote to assign to the various functions. We used the volume up and down, the 4 arrow keys, and the numbers 0-4 but you can pick any buttons that you want on your remote.
You need to load the Infrared_Read sketch from the previous page of this tutorial. Point your remote at the Circuit Playground Express and push one of the buttons that you want to assign to one of the functions of this program. Note the protocol name and number and the hex value that is decoded. Write down that information for each of the 10 buttons you're going to use.
At line 100 of the program we check to see if we received and decoded the proper protocol number. The statement currently says…
if (! CircuitPlayground.irDecoder.protocolNum == NEC) {
If your remote uses something other than NEC protocol you will have to substitute the protocol number for your remote. Here is a list of the supported protocols. This list is defined in "Adafruit_CircuitPlayground/utility/IRLibProtocols.h"
#define UNKNOWN 0 #define NEC 1 #define SONY 2 #define RC5 3 #define RC6 4 #define PANASONIC_OLD 5 #define JVC 6 #define NECX 7 #define SAMSUNG36 8 #define GICABLE 9 #define DIRECTV 10 #define RCMM 11 #define CYKM 12
Look at the "switch" statement at line 107. There are 10 cases corresponding to the 10 different functions in this program. You will see values such as "ADAF_MINI_1" or "ADAF_MINI_2" on each case statement. These values are defined in a file called "adafruit_mini_codes.h" that is included with the program. At the top of your Arduino IDE you can click on the second tab to see this file. It contains the hex codes for all of the buttons on that remote… even the ones we are not using on this project. If you plan to use a remote for more than one project it might be a good idea to create such a file for your particular remote and then just copy that information into whatever program you need.
For example if I was going to use the remote for my Samsung TV it uses NECx protocol (not straight NEC but NECx which is protocol number 7) The code for volume up is "0xE0E0F01F". So I have a choice of either creating a header file with the definition such as
#define MY_TV_VOLUME_UP 0xE0E0F01F
And then I would modify the case statement online 144 which currently reads
case ADAF_MINI_VOLUME_UP:
to instead read
case MY_TV_VOLUME_UP:
or I could just plug in the value directly by saying…
case 0xE0E0F01F:
You will need to make the modifications for all 10 case statements.
Some protocols use special repeat codes and other strange features that may affect the way you implement your code. For complete details about all the protocols supported by IRLib2 and how to write your own protocol decoders for protocols we do not yet support there is extensive information in the IRLib2 manual available on GitHub at https://github.com/cyborg5/IRLib2/tree/master/IRLib2/manuals
The manual is available in Microsoft Word .docx, Adobe acrobat PDF, or e-book .epub formats.
Page last edited July 01, 2017
Text editor powered by tinymce.