having problems using the IR Remote library.

I hope I am posting this on the right forum.

So, I am building a color organ using the RGB LED pixels. I got the led part to work properly. I am now trying to add in an IR sensor so that I can turn it on and off with a press of a button. I am using IR Remote library by Ken Shirriff. Here is the part of the code I am having trouble with.

     if (irrecv.decode(&results)) {
    codevalue = results.value;
    irrecv.resume(); // Receive the next value
     Serial.println(codevalue);
     
  }

  if (codevalue==2295)
  {
    z = 2;
  }
  if (codevalue==14535){
  z= 1;
}

When I press the same button on the remote, I am getting different codes out. Initially, I would get the right codes if I keep on pressing on one button, but as soon as I start pressing different buttons, it would give me random codes for the same button press.

This doesn't happen at all for some reason if I remove the 2nd and third if statements from the above code. I have attached the file with entire code.

Color_Organ_FHT_ino.ino (6 KB)

What type is results.value? Hint: It's not an int.

You should only check codevalue when a new value arrives:

    if (irrecv.decode(&results)) {
      codevalue = results.value;
      irrecv.resume(); // Receive the next value
      Serial.println(codevalue);

     if (codevalue==2295) {
       z = 2;
     }
     if (codevalue==14535) {
       z = 1;
     }
   }

PaulS:
What type is results.value? Hint: It's not an int.

output of results.value is hex. I tried using results.value instead of a new variable (codevalue), but I got the same problem.

Upon testing further, I figured what was causing the problem. I disabled the strip.show() command in the code, and now I am getting the correct hex codes. However, I need to use the strip.show() command in my program to light up the LEDs.

sjpatel1992:
Upon testing further, I figured what was causing the problem. I disabled the strip.show() command in the code, and now I am getting the correct hex codes. However, I need to use the strip.show() command in my program to light up the LEDs.

I think the problem is interrupts. The IRRecv library uses a timer to sample the IR every 50 microseconds. The Adafruit_NeoPixel library, on the other hand, disables interrupts while updating the strip. Every time you call .show() the interrupts are disabled for a significant time. The two libraries may not be compatible.