In this very simple example we will change the color of a NeoPixel by pushing buttons on the remote. We are using a single pixel but you can modify the sketch to control an entire strip or matrix. For more information on NeoPixels visit this guide in the Adafruit Learning System. Here is the code:
#include <Adafruit_NeoPixel.h> #include <IRLibAll.h> IRrecv myReceiver(2);//receiver on pin 2 IRdecode myDecoder;//Decoder object //One NeoPixel connected to pin 6 Adafruit_NeoPixel strip = Adafruit_NeoPixel(1,6,NEO_GRB + NEO_KHZ800); void setup() { strip.begin(); strip.show(); // Initialize all pixels to 'off' myReceiver.enableIRIn(); // Start the receiver } void loop() { if (myReceiver.getResults()) { myDecoder.decode(); if (myDecoder.protocolNum == NEC) { switch(myDecoder.value) { case 0xfd00ff: //Volume Down strip.setPixelColor(0,255,0,0);//Red break; case 0xfd807f: //Play/Pause strip.setPixelColor(0,0,255,0);//Green break; case 0xfd40bf: //Volume Up strip.setPixelColor(0,0,0,255);//Blue break; } strip.show(); myReceiver.enableIRIn(); //Restart the receiver } } }
We create a NeoPixel strip with one pixel connected to pin 6. Also create a receiver object connected to pin 11 and a decoder object. Reinitialize the pixel strip and the IR receiver in the setup routine. Then in the main loop continuously test the receiver to see if it has received many IR signals. If myReceiver.getResults returns true then we decode the data. We are using the Adafruit Mini Remote we used earlier. It used the NEC protocol so we make sure that we actually received NEC protocol data. Then we use a switch statement to test various 32-bit hex values against the decoded data in My_Decoder.value.
After we have set the pixel color based on the received value, we need to call strip.show() to actually change the color and myReceiver.enableIRIn() to reset the receiver so it can collect another code.
Upload the sketch and try pressing the volume down, play/pause, and volume up buttons. You should see the pixel change to red, green, or blue. You can easily add additional case statements and colors or perhaps have one of the cases call an animation routine to animate the entire strip of pixels. Different buttons on the remote would select different animation patterns.
You will have to modify this sketch if you are using a different remote control. Change the protocol type spuch as:
if (myDecoder.protocolNum==SONY)
for example if you are using a Sony remote. And of course you have to substitute the proper codes in each case statement. The enumerated list of available protocols for comparing decode_type can be found at approximately line 60 of IRLib.h.
Text editor powered by tinymce.