Simple IR sensor and NEC remote.

I am just trying to change the color of an RGB LED by pressing buttons on the remote. I can get the LED to change color according to what button i press, but every so often i get an unknown IR signal that sort of stops an other codes from being received by the sensor. Is there some way to flush the sensors input? This is the first time i have ever messed with IR.

  • the sensor im using is CHQ1838

  • arduino nano

#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() {
  Serial.begin(9600);
  strip.begin();
  strip.setPixelColor(0,150,0,150);
  strip.show(); // Initialize all pixels to 'off'
  myReceiver.enableIRIn(); // Start the receiver
  Serial.println(F("ready to recieve IR signals"));
}

void loop() {
  if (myReceiver.getResults()) {
    myDecoder.decode();
    myDecoder.dumpResults(false);
    if (myDecoder.protocolNum == NEC) {
      switch(myDecoder.value) {
        case 0xFF30CF:  //Volume Down
          strip.setPixelColor(0,255,0,0);//Red
          break;
        case 0xFF18E7:  //Play/Pause
          strip.setPixelColor(0,0,255,0);//Green
          break;
        case 0xFF7A85:  //Volume Up
          strip.setPixelColor(0,0,0,255);//Blue
          break;
      }
    strip.show();
    myReceiver.enableIRIn(); //Restart the receiver
    }
  }
}

Add a default case that executes when a non valid code is received. So if the IR code is not handled by your other cases you can.clear the bad code and return to the sketch.

The switch case reference show how to include a default case.

Have you tried IRremote.h

Try this:

#include <Adafruit_NeoPixel.h>
#include <IRremote.h>

IRrecv myReceiver(2);//receiver on pin 2
decode_results results;

//One NeoPixel connected to pin 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, 6, NEO_GRB + NEO_KHZ800);

void setup()
{
  Serial.begin(9600);
  strip.begin();
  strip.setPixelColor(0, 150, 0, 150);
  strip.show(); // Initialize all pixels to 'off'
  myReceiver.enableIRIn(); // Start the receiver
  Serial.println(F("ready to recieve IR signals"));
}

void loop()
{
  if (myReceiver.decode(&results))
  {
    Serial.println(results.value, HEX);
    switch (results.value)
    {
      case 0xFF30CF:  //Volume Down
      //case 0xFFE01F:  //Volume Down
        strip.setPixelColor(0, 255, 0, 0); //Red
        break;
      case 0xFF18E7:  //Play/Pause
      //case 0xFFA857:  //Play/Pause
        strip.setPixelColor(0, 0, 255, 0); //Green
        break;
      case 0xFF7A85:  //Volume Up
      //case 0xFFC23D:
        strip.setPixelColor(0, 0, 0, 255); //Blue
        break;
    }
    strip.show();
    myReceiver.resume(); //Restart the receiver
  }
}

Amazing! many thanks, that's a good starting point for my purposes!

Hey guys, I'm new to Arduino, I just wanted to know how can I use a NEC remote button (0x20DF8877) with the same function as the button from

''.. // read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
if(buttonPushCounter==14) {
buttonPushCounter=1;}
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}