RGB Led controlled over IR remote

Hello everyone i am having some troubles to use the IRremote library with my project.

I want to control an rgb led with my infrared remote. The behavior i expect from this project is simple, i push to one control button to trigger the “fade” mode or push to another button to instantly change mode to “flash”.

So i create two functions, one with all colors fading and one where all colors flash one after the other.
But here is the problem, i power up my arduino mega i push to the button for start the “flash” mode and after a while i push the button for “fade” mode and i have to wait till the previous mode finish and then i get the “fade” mode.

I’m not having a lot of experience with interrupts or timer but i guess the problem in my code is that i use to match the delay function, since the IRremote library use’s the timer2 in pin 9 for new incoming values from the infrared port.

If you have any suggestion to help me improve my project,

This is my code :

#include <IRremote.h>
#include <IRremoteInt.h>



int led[3] = {
  5, 6, 7};           // the pins that the LEDs is attached to 5 is red, 6 is green, 7 is blue
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int fadeDelay = 30;
unsigned long command = 0;
int RECV_PIN = 9;

IRrecv irrecv(RECV_PIN);

decode_results results;

// the setup routine runs once when you press reset:
void setup()  { 
  Serial.begin(9600);
  irrecv.enableIRIn();
  // declare pin 5, 6, 7 to be an output:
  for(int i=0;i<3;i++){
    pinMode(led[i], OUTPUT);
  }
} 

// the loop routine runs over and over again forever:
void loop()  {    
  if (irrecv.decode(&results)) {

    if(results.value!=0xFFFFFFFF)
      command = results.value;

    Serial.println(command, HEX);

    irrecv.resume(); // Receive the next value
  }

  switch(command){

  case 0x2FDC837:
    fadeRgb(led, brightness, fadeAmount, fadeDelay);
    break;

  case 0x2FD8877:
    flashRgb(led);
    break;

  default:
    break;
  }
}


void fadeRgb(int *led, int brightness, int fadeAmount, int fadeDelay) {

  // RED  
  for(brightness=0; brightness<=256; brightness+=fadeAmount) {
    analogWrite(led[0], brightness); 

    delay(fadeDelay);
  }

  // YELLOW
  for(brightness=0; brightness<=256; brightness+=fadeAmount) {
    analogWrite(led[1], brightness);   

    delay(fadeDelay);
  }

  // GREEN
  for(brightness=255; brightness>=0; brightness-=fadeAmount) {
    analogWrite(led[0], brightness);   

    delay(fadeDelay);
  }

  // CYAN
  for(brightness=0; brightness<=255; brightness+=fadeAmount) {
    analogWrite(led[2], brightness); 

    delay(fadeDelay);
  }

  // BLUE	
  for(brightness=255; brightness>=0; brightness-=fadeAmount) {
    analogWrite(led[1], brightness); 

    delay(fadeDelay);
  }

  // MAGENTA
  for(brightness=0; brightness<=255; brightness+=fadeAmount) {
    analogWrite(led[0], brightness); 

    delay(fadeDelay);
  }

  // WHITE
  for(brightness=0; brightness<=255; brightness+=fadeAmount) {
    analogWrite(led[1], brightness);    

    delay(fadeDelay);
  }

  // ALL COLORS FADE
  for(brightness=255; brightness>=0; brightness-=fadeAmount) {
    for(int i=0;i<3;i++){
      analogWrite(led[i], brightness);   
    }
    delay(fadeDelay);
  }
}

void flashRgb(int *led){

  // RED
  digitalWrite(led[0], HIGH);
  delay(1000);
  digitalWrite(led[0], LOW);

  // YELLOW
  digitalWrite(led[0], HIGH);
  digitalWrite(led[1], HIGH);
  delay(1000);
  digitalWrite(led[0], LOW);
  digitalWrite(led[1], LOW);

  // GREEN
  digitalWrite(led[1], HIGH);
  delay(1000);
  digitalWrite(led[1], LOW);

  // CYAN
  digitalWrite(led[1], HIGH);
  digitalWrite(led[2], HIGH);
  delay(1000);
  digitalWrite(led[1], LOW);
  digitalWrite(led[2], LOW);

  // BLUE
  digitalWrite(led[2], HIGH);
  delay(1000);
  digitalWrite(led[2], LOW);

  // MAGENTA
  digitalWrite(led[0], HIGH);
  digitalWrite(led[2], HIGH);
  delay(1000);
  digitalWrite(led[0], LOW);
  digitalWrite(led[2], LOW);

  // WHITE
  digitalWrite(led[0], HIGH);
  digitalWrite(led[1], HIGH);
  digitalWrite(led[2], HIGH);
  delay(1000);
  digitalWrite(led[0], LOW);
  digitalWrite(led[1], LOW);
  digitalWrite(led[2], LOW);
}

Welcome with your first post....

You are correct....once you start a fade your code is designed to stay with it until the whole sequence is over.

You could redesign your code using timers or just the in main loop and recording the current desired state and timing in variables....and then just incremently set the rgb etc based on the the time elapsed since the initial instruction. When you get a new command you could just restart from the current setting to get to where you want.

Sorry, cant be more helpful