IR receiving random codes

Hello all. First time poster here on the forms. I’m working on an LED lighting project and I’m trying to incorporate an IR sensor so that I can control the LED’s. What I don’t get is that when I have code in my project that updates my lighting, the IR sensor gets random codes. When I only have the IR code, it receives the right codes no problem. I have a feeling this is a timing issue and I’m trying to figure out how to reliably get correct codes so that I can eventually change patterns, colors, etc. on my led strip. Below is the serial output when it’s not working correctly.

 other button 
54970BD9
 other button 
3DFE8207
 other button 
BBC3DDA
 other button 
4325BA15
 other button 
8A0F61A8
 other button 
A1E3DE0C
 other button 
CEF52AD2

And blow is my code.

#include <FastLED.h>
#include <IRremote.h>

//Strip Light Objects
#define NUM_LEDS 240 // How many LEDs we're lighting
CRGB leds[NUM_LEDS]; // Declare Strip Array
#define PIN 6 // Pin that controls strip
byte hue; // Use to hold colors

//IR Remote Objects
int receiver = 3; // Pin that listens for IR
IRrecv irrecv(receiver); // Declare IR Object
decode_results results; // Holds IR Result Codes

/* IR Receive Codes
 0xFFA25D: POWER
 0xFFE21D: FUNC/STOP
 0xFF629D: VOL+
 0xFF22DD: FAST BACK"
 0xFF02FD: PAUSE
 0xFFC23D: FAST FORWARD
 0xFFE01F: DOWN
 0xFFA857: VOL-
 0xFF906F: UP
 0xFF9867: EQ
 0xFFB04F: ST/REPT
 0xFF6897: 0
 0xFF30CF: 1
 0xFF18E7: 2
 0xFF7A85: 3
 0xFF10EF: 4
 0xFF38C7: 5
 0xFF5AA5: 6
 0xFF42BD: 7
 0xFF4AB5: 8
 0xFF52AD: 9
 0xFFFFFFFF: Repeat
 */

void setup()
{
  //Setup LED Strip
  FastLED.addLeds<NEOPIXEL, PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(255);
  FastLED.clear();

  //Setup IR Receiver
  irrecv.enableIRIn();

  Serial.begin(9600);
}

void loop() { 

  if (irrecv.decode(&results)) {
    switch(results.value) {
      case 0xFFA25D: Serial.println("POWER"); break;
      case 0xFFE21D: Serial.println("FUNC/STOP"); break;
      case 0xFF629D: Serial.println("VOL+"); break;
      case 0xFFFFFFFF: Serial.println(results.value, HEX);break;  

      default: Serial.println(" other button ");
    }
    irrecv.resume();
    delay(500);
    Serial.println(results.value, HEX);
  }
  
  if (random(2)) {
    hue = 0;
  } else {
    hue = 160;
  }
  
  if( random8() < 200) {
    leds[random16(NUM_LEDS)] = CHSV(hue, 255, 255);
  }
  
  fadeToBlackBy(leds, NUM_LEDS, 10);
  FastLED.show();
  delay(20);
}

Any insight is greatly appreciated.

timer collision ? (try pin 9, for timer 1)
IR-lib info

I switched the IR sensor to pin 9 but I'm still having the same issues. What I've found is that if I put a long enough delay right before the code tries to read the IR sensor that it will most of the time get the correct IR code. What I'm thinking is that there must be a way to tell the sensor to only try reading at a certain point in code and if it doesn't receive anything, stop trying to and continue with the rest of the code before trying to read it again. For an example:

void loop() {
enable IR sensor

if signal coming in
    do something with the code
if no signal coming in
    disable IR sensor

proceed with lighting code
}

I know that you enable the sensor with IRrecv.enableIRIn(), but there doesn't seem to be a disableIRIn() to turn it back off.

It is extremely important to isolate the IR sensor from electrical noise in the power wiring. If you are powering LEDs from the Arduino power supply, or an LED strip share the same supply, that can cause problems.

One approach is to filter the IR sensor power using a 100 Ohm resistor followed by a 100 uF capacitor, like this:
IR-Vcc.gif

1 Like