1

First of all, I'm very new to this so sorry if it's a dumb question.

I have an IR module which I'm using to control an LED strip. I have set buttons 1–3 on the remote to select three basic static colours and up and down to adjust brightness. So far so good.

I have set button 4 to gradually shift through different colours. Once I activate this code, the IR receiver appears to stop working, and Serial.print shows that it's now reporting random values whatever button I press on the remote.

I have tried Vishay TSOP4838 and KY-022 type receivers, and tried varying combinations of resistors and capacitors on the receiver power supply. I've tried powering it from 3.3V and 5V.

The code that I'm using is only changing numerical values to adjust the colour, so short of there being 'noise' somewhere in the hardware I'm at a loss as to why only this code breaks it. The LED is powered by a 12V adaptor. The IR receiver is the only thing powered by the Arduino.

My full code is as follows, I know it's untidy. I am a noob:

Thank you

#include "IRremote.h"
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

//To change the LED/IR reciever pins, you just need to change these values
#define PIN 13 //LED strip
int receiver = 10; //IR receiver

unsigned long debouncing = 0;
unsigned long rotatedelay = 6;
unsigned long fadegap = 5;
byte state = 0; //maintains function based on last button press
byte red = 0;
byte blue = 0;
byte green = 0;
byte brightness = 250;
byte rotate = 0; //used for cycling through colours in state 4

Adafruit_NeoPixel strip = Adafruit_NeoPixel(50, PIN, NEO_GRB + NEO_KHZ800);

//Declare IR objects
IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

//Setup IR buttons with names
void translateIR() // takes action based on IR code received

// Naming remote IR codes and assigning a 'state' 

{

  switch(results.value)

  {
  case 0xFFA25D: state = 10; Serial.println("POWER"); break;
  case 0xFF629D: state = 11; Serial.println("VOL+"); break;
  case 0xFFE21D: state = 12; Serial.println("FUNC/STOP"); break;
  case 0xFF22DD: state = 13; Serial.println("FAST BACK");    break;
  case 0xFF02FD: state = 14; Serial.println("PLAY/PAUSE");    break;
  case 0xFFC23D: state = 15; Serial.println("FAST FORWARD");   break;
  case 0xFFE01F: Serial.println("DOWN");    break;
  case 0xFFA857: state = 17; Serial.println("VOL-");    break;
  case 0xFF906F: Serial.println("UP");    break;
  case 0xFF9867: state = 19; Serial.println("EQ");    break;
  case 0xFFB04F: state = 20; Serial.println("ST/REPT");    break;
  case 0xFF6897: state = 0; Serial.println("0");    break;
  case 0xFF30CF: state = 1; Serial.println("1");    break;
  case 0xFF18E7: state = 2; Serial.println("2");    break;
  case 0xFF7A85: state = 3; Serial.println("3");    break;
  case 0xFF10EF: state = 4; Serial.println("4");    break;
  case 0xFF38C7: state = 5; Serial.println("5");    break;
  case 0xFF5AA5: state = 6; Serial.println("6");    break;
  case 0xFF42BD: state = 7; Serial.println("7");    break;
  case 0xFF4AB5: state = 8; Serial.println("8");    break;
  case 0xFF52AD: state = 9; Serial.println("9");    break;
  case 0xFFFFFFFF: Serial.println("REPEAT");break;  

  default: 
    Serial.println(" other button   ");

  }// End Case


} //END translateIR

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the IR receiver
  strip.begin();
  colorBlock(strip.Color(0, 0, 0)); 
  strip.show(); // Initialize all pixels to 'off'

//Fades LED strip light in at startup
for(uint8_t b = 0; b <255; b++) {
     for(uint8_t i=0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, 255 * b/255, 255 * b/255, 255 * b/255);
     }

     strip.show();
     delay(5); //speed of fade in (20 x 255 = total time)
  };
}

void loop() {

//IR detection code
//if (irrecv.decode(&results) && millis() - debouncing > 1000) // have we     received an IR signal? ignores button press if less than a second after previous
if (irrecv.decode(&results)) // have we received an IR signal?  
{
    translateIR(); 
    irrecv.resume(); // receive the next value
    Serial.println(results.value, HEX);

if (state == 1){
  rotate = 0;
  red = 0; blue = 255; green = 0;
colorBlock(strip.Color(red, blue, green));  
}

if (state == 2){
  rotate = 0;
  red = 0; blue = 0; green = 255;
colorBlock(strip.Color(red, blue, green));  
}

if (state == 3){
  rotate = 0;
  red = 255; blue = 0; green = 255;
colorBlock(strip.Color(red, blue, green));  
}

//brightness
if (results.value == 16769055 && brightness > 0){
  brightness = brightness - 10;
  strip.setBrightness(brightness); strip.show();  
  Serial.println(brightness);
}

if (results.value == 16748655 && brightness < 241){
  brightness = brightness + 10;
  strip.setBrightness(brightness); colorBlock(strip.Color(red, blue, green));     strip.show();  
  Serial.println(brightness);
}
}

//rotating colours
if (state == 4){
  //Serial.println (state);
  //Serial.println (rotate);
  //Serial.print ("red"); Serial.print (red); Serial.print ("blue"); Serial.print (blue); Serial.print ("green"); Serial.println (green);
  //Serial.println (rotatedelay);
}

if (state == 4 && rotate == 0 && red < 255 && millis() - rotatedelay > 1){
  red = red + fadegap; 
  if (blue >= 1){
    blue = blue - fadegap;} 
  if (green >= 1){
    green = green - fadegap;}
  colorBlock(strip.Color(red, blue, green)); 
  rotatedelay = millis();
}

if (state == 4 && rotate == 0 && green > 0 && millis() - rotatedelay > 1){
  green = green - fadegap; 
  colorBlock(strip.Color(red, blue, green)); 
  rotatedelay = millis();
}

if (state == 4 && rotate == 0 && red == 255 && green == 0 && blue == 0 &&         millis() - rotatedelay > 1){
  rotate = 1;
}

if (state == 4 && rotate == 1 && blue < 255 && millis() - rotatedelay > 1){
  blue = blue + fadegap; 
  if (red >= 1){
    red = red - fadegap;} 
  if (green >= 1){
    green = green - fadegap;}
  colorBlock(strip.Color(red, blue, green)); 
  rotatedelay = millis();
}

if (state == 4 && rotate == 1 && red == 0 && green == 0 && blue == 255 &&     millis() - rotatedelay > 1){
  rotate = 2;
}

if (state == 4 && rotate == 2 && green < 255 && millis() - rotatedelay > 1){
  green = green + fadegap; 
  if (red >= 1){
    red = red - fadegap;} 
  if (blue >= 1){
    blue = blue - fadegap;}
  colorBlock(strip.Color(red, blue, green)); 
  rotatedelay = millis();
}

if (state == 4 && rotate == 2 && blue == 0 && red == 0 && green == 255 &&     millis() - rotatedelay > 1){
  rotate = 0;
}
}

//Solid colour
 void colorBlock(uint32_t c) { //This seems to be the way to colour the whole strip
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    }
 }
  • Perhaps build two alternate test setups: one where the IR receive can "see" the LED strip but there is no electrical connection of any sort (ie, a second setup with another Arduino on a different computer). Another with your typical electrical connections, but where the IR receiver is fully shielded from any sight of the LEDs - wrapped in multiple layers of electrical tape or something like that. – Chris Stratton Jun 6 '18 at 21:24
  • remove all code from inside of if (state == 4 && statements and insert Serial.println( statements for debugging – jsotola Jun 6 '18 at 21:25
  • tip: you can use code like this if (blue >= 1) blue -= fadegap; – jsotola Jun 6 '18 at 21:29
  • I've had some problems of the LED's PWM interfering with the IR signal. Or so it seemed at least. In theory, the receiver should have a 38 kHz band pass filter. Also, your code doesn't compile: there's a missing '}' at the end of your loop function. – tttapa Jun 6 '18 at 22:23
  • Thank you for the ideas. I tried isolating the light from the receiver and no luck. Perhaps it's PWM, I need to read up on how the filter works. Oh and the code compiles, it was just a copy/paste error. Thanks – Simontheintrepid Jun 8 '18 at 9:31
1

Before the lines

//rotating colours
if (state == 4){

You have an extra closing brace. IF that is in you real code, I'm not sure how it is compiling.

Assuming it isn't that then you might want to change you code to make it more readable. This is just part of you code from the same place mentioned before:

if (state == 4)
{
    if (millis() - rotatedelay > 1)
    {
        if (rotate == 0)
        {
            checkFadeGap (red, blue, green);

            if (green > 0)
            {
                green -= fadegap; 
                colorBlock(strip.Color(red, blue, green)); 
                rotatedelay = millis();
            }//if (green > 0)
            if (red == 255  && green == 0 && blue == 0)
            {
                rotate = 1;
            }//if (red == 255)
        }
        if (rotate == 1)
        {
            checkFadeGap (blue, red, green);
            if (red == 0 && green == 0 && blue == 255)
            {
                rotate = 2;
            }//if (blue == 255)
        }
        if (rotate == 2)
        {
            checkFadeGap (green, reb, blue);
            if (red == 0  && green == 255 && blue == 0)
            {
                rotate = 0;
            }//if (green == 255)
        }
    }//if (millis() - rotatedelay > 1)
}    // end of if (state == 4)

This function removes some of the complexity in the code above.

void checkFadeGap (byte& inc, byte& dec1, byte& dec2)
{
    if (inc < 255)
    {
        inc += fadegap;
        if (dec1 > 0)
            dec1 -= fadegap;
        if  (dec2 > 0);
            dec2 -= fadegap;
        colorBlock(strip.Color(red, blue, green)); 
        rotatedelay = millis();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.