IR Receiver code messing with RGB code

I found some code online that's for RBG leds to cycle through the RGB colors seamlessly, it just flows through the color spectrum. I connected an RGB strip I bought to the arduino since the remote it came with didn't have a seamless rgb cycle.

The code and lights were perfect but I had to unplug the arduino from my pc every time I wanted to turn the lights off

I had an IR reciever for arduino and its remote so I decided to learn how to use it to be able to shut off the lights using the remote.

I coded the IR correctly and it turned the lights off and on, but the flow the RGB was now choppy, it just cuts to each color now and doesn't fade.

I figure out that the function irrecv.enableIRIn() in the setup was making the colors cut instead of fade, and if I deleted that line, everything would be back to normal.

Is there any reason why? Or any fix?

Also, the RGB is active something I get the two mixed up, where if I digitalWrite(red, LOW), the light is actually at it's highest value, which makes the fader reversed or in a different color tone, is there any way I can fix this too?

Thanks!

What type of led strip ? Some require precise timing that the IR listener can mess around with...

Show your code...

I got the led code online, and the RGB strip is from like walmart or something, but has female pins, so I just connected them directly to the arduino.

#include <IRremote.h>

#define GREEN 9
#define BLUE 10
#define RED 11
#define delayTime 2
int rece = 22, temp;

IRrecv irrecv(rece);
decode_results results;

void setup() {
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(RED, HIGH);

temp = 0;
}

int redVal;
int blueVal;
int greenVal;

void loop()
{
irrecv.enableIRIn();
// if (irrecv.decode(&results))
// {
// temp = temp + 1;
//
// if (temp == 2)
// {
// temp = 0;
// }
// irrecv.resume();
// }
//
// if (temp == 0)
// {
// digitalWrite(GREEN, HIGH);
// digitalWrite(BLUE, HIGH);
// digitalWrite(RED, HIGH);
// delay(1000);
// }

// if (temp == 1)
// {
int redVal = 255;
int blueVal = 0;
int greenVal = 0;
for ( int i = 0 ; i < 255 ; i += 1 ) {
greenVal += 1;
redVal -= 1;
analogWrite( GREEN, 255 - greenVal );
analogWrite( RED, 255 - redVal );

delay( delayTime );
}

redVal = 0;
blueVal = 0;
greenVal = 255;
for ( int i = 0 ; i < 255 ; i += 1 ) {
blueVal += 1;
greenVal -= 1;
analogWrite( BLUE, 255 - blueVal );
analogWrite( GREEN, 255 - greenVal );

delay( delayTime );
}

redVal = 0;
blueVal = 255;
greenVal = 0;
for ( int i = 0 ; i < 255 ; i += 1 ) {
redVal += 1;
blueVal -= 1;
analogWrite( RED, 255 - redVal );
analogWrite( BLUE, 255 - blueVal );

delay( delayTime );
}
// }
}

you code has delays in for loops, so when you do that you don't do anything else. the code commented out that deals with the remote contains also a 1 second pause.

Well yes I get that, I took out the delays that weren't needed, and kept the ones for the color fade, and it looked fine, but as I'm trying to say, using the IR remote, even without any additional code except for enabling it in the setup, makes my code all choppy and I'm not sure why. My code ONLY gets choppy if I include the irenable in the setup.

Please correct your post above and add code tags around your code:

[code]

[color=blue]// your code is here[/color]

[/code]

.

It should look like this:

// your code is here

(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

—————

which arduino are you using ?

I’m assuming you are on a MEGA since you use pin 22 for the receiver.

irrecv.enableIRIn(); should be in the setup() not in the loop. When you call this, you enable an interrupt Service Routine which fires every 50µs to go and listen to any IR. that interrupt is handled by default using Timer2 (a parameter set in boarddefs.h).

Now - You also use pin 9,10 and 11 through PWM. What you need to understand is that timers are also used to control PWM and pins are associated to timers.

On the Arduino Mega there are 6 timers and 15 PWM outputs:
Pins 4 and 13: controlled by timer0
Pins 11 and 12: controlled by timer1
Pins 9 and10: controlled by timer2
Pin 2, 3 and 5: controlled by timer 3
Pin 6, 7 and 8: controlled by timer 4
Pin 46, 45 and 44:: controlled by timer 5

So you have now a conflict as your IR Library modified the configuration of timer2, which changes the behavior of pins 9 and 10. (pin 11 is on timer1 which you have not touched).

two options from there:

1/ change the timer:
find the boarddefs.h file in your system and find the section that looks like this:

// Arduino Mega
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
	//#define IR_USE_TIMER1   // tx = pin 11
	#define IR_USE_TIMER2     // tx = pin 9
	//#define IR_USE_TIMER3   // tx = pin 5
	//#define IR_USE_TIMER4   // tx = pin 6
	//#define IR_USE_TIMER5   // tx = pin 46

Comment out the IR_USE_TIMER2 line and pick another timer that does not conflict with your setup. for example uncomment IR_USE_TIMER5

2/ change the pins 9 and 10 for something else. You could go for example to any pin controlled by other timers, for example Pin 6, 7 and 8 (controlled by timer 4) or Pin 46, 45 and 44 (controlled by timer 5).


As a side note, if you want to expand on your code, you should move your code to a structure that uses millis() (see blink without delay and other millis() related tutorials) so that you don’t lock up the loop() in active wait mode.

typed here, but this could be an example on how you could do so with a small state machine

// connect LEDs to pins 44,45,46
const byte GREEN_PIN = 44;
const byte BLUE_PIN = 45;
const byte RED_PIN = 46;

enum : uint8_t {STEP1, STEP2, STEP3} blinkState = STEP1;
const uint32_t updatePeriod = 2UL; // one step in color animation every 2ms


void animate(bool forceStart = false)
{
  static unsigned long lastChrono = 0;
  static unsigned int stepNumber = 0;
  static int redVal = 255;
  static int blueVal = 0;
  static int greenVal = 0;

  if (forceStart) {
    blinkState = STEP1;
    redVal = 255;
    blueVal = 0;
    greenVal = 0;
    stepNumber = 0;
  }

  if (millis() - lastChrono >= updatePeriod) { // is it time to take one tock
    lastChrono += updatePeriod;

    switch (blinkState) {

      case STEP1:
        if (stepNumber < 255) {
          stepNumber++;
          greenVal += 1;
          redVal -= 1;
          analogWrite( GREEN_PIN, 255 - greenVal );
          analogWrite( RED_PIN, 255 - redVal );
        } else {
          blinkState = STEP2;
          stepNumber = 0;
          redVal = 0;
          blueVal = 0;
          greenVal = 255;
        }
        break;

      case STEP2:
        if (stepNumber < 255) {
          stepNumber++;
          blueVal += 1;
          greenVal -= 1;
          analogWrite( BLUE_PIN, 255 - blueVal );
          analogWrite( GREEN_PIN, 255 - greenVal );
        } else {
          blinkState = STEP3;
          stepNumber = 0;
          redVal = 0;
          blueVal = 255;
          greenVal = 0;
        }
        break;

      case STEP3:
        if (stepNumber < 255) {
          stepNumber++;
          redVal += 1;
          blueVal -= 1;
          analogWrite( RED_PIN, 255 - redVal );
          analogWrite( BLUE_PIN, 255 - blueVal );
        } else {
          blinkState = STEP1;
          redVal = 255;
          blueVal = 0;
          greenVal = 0;
          stepNumber = 0;
        }
        break;
    }
  }
}

void setup()
{
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  pinMode(RED_PIN, OUTPUT);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, HIGH);
  digitalWrite(RED_PIN, HIGH);

  animate(true); // force start the animation
}

void loop()
{
  animate();
}

Thanks, I never knew about timers! Changing the pins to 8,7,6 helped.

TWLGHT:
Thanks, I never knew about timers! Changing the pins to 8,7,6 helped.

glad that helped

I would still appreciate if you edit your post and add code tags... keep the forum "clean"...