Help with code for IR sensor, switch case and RGBs

Hello and thank you for taking the time to read this,

I am using a MEGA 2560. I have two RGBs. I would like to push the UP button on my IR remote to select the first RGB and push a button 0-9 to choose the color, I then would like to push the DOWN button to select the second RGB and push a button 0-9 to select the second color.

I have fiddled with the code for a few hours but I am missing something. I feel like I have it pretty close. At this point if I push the UP or DOWN button and then numbers 0-9 it will do what it is supposed to half of the time, the other half it will change the wrong RGB.

I truly appreciate any advice, help or direction,

Here is as far as I have got:

#include <IRremote.h>

int ledAnalogOne[] = {2, 3, 4}; 
int ledAnalogTwo[] = {5, 6, 7};

//Defined Colors (different RGB (red, green, blue) values for colors
//(to add your own ie. fuscia experiment and then add to the list)
const byte RED[] = {255, 0, 0}; 
const byte ORANGE[] = {83, 4, 0}; 
const byte YELLOW[] = {255, 255, 0}; 
const byte GREEN[] = {0, 255, 0}; 
const byte BLUE[] = {0, 0, 255}; 
const byte INDIGO[] = {4, 0, 19}; 
const byte VIOLET[] = {23, 0, 22}; 
const byte CYAN[] = {0, 255, 255}; 
const byte MAGENTA[] = {255, 0, 255}; 
const byte WHITE[] = {255, 255, 255}; 
const byte ZINK[] = {158, 4, 79};
const byte TEST[] = {255, 255, 255 };
const byte BLACK[] = {0, 0, 0};



int RECV_PIN = 12;
IRrecv irrecv1(RECV_PIN);
IRrecv irrecv2(RECV_PIN);


decode_results results1;
decode_results results2;



void setup(){
 Serial.begin(9600);
  
  for(int i = 0; i < 3; i++){
   pinMode(ledAnalogOne[i], OUTPUT);   //Set the three LED pins as outputs
  }
  for(int i = 0; i < 3; i++){
   pinMode(ledAnalogTwo[i], OUTPUT);   //Set the three LED pins as outputs
  }
   
  setColor(ledAnalogOne, BLACK);
  setColor(ledAnalogTwo, BLACK);
  
irrecv1.enableIRIn();
irrecv2.enableIRIn();

}



void loop() {
 
  if (irrecv1.decode(&results1)) {
       
    switch(results1.value) {
       
      case 0xFF629D:   //UP
       Serial.println("RGB_LED1");  
       setColor(ledAnalogOne, WHITE);
       setColor(ledAnalogOne, BLACK);
       break;
       
     case 0xFF6897:  //1
      Serial.println("1_RED"); 
      setColor(ledAnalogOne, RED);
      break;
       
     case 0xFF9867:  //2
      Serial.println("1_ORANGE"); 
      setColor(ledAnalogOne, ORANGE);
      break;
       
     case 0xFFB04F:  //3
      Serial.println("1_YELLOW");
      setColor(ledAnalogOne, YELLOW);
      break;
       
     case 0xFF30CF:  //4
      Serial.println("1_GREEN");
      setColor(ledAnalogOne, GREEN);
      break;
       
     case 0xFF18E7: //5
      Serial.println("1_BLUE");
      setColor(ledAnalogOne, BLUE);
      break;
       
     case 0xFF7A85:  //6
      Serial.println("1_INDIGO");
      setColor(ledAnalogOne, INDIGO);
      break;
      
     case 0xFF10EF: //7
      Serial.println("1_VIOLET");
      setColor(ledAnalogOne, VIOLET);
      break;
    
     case 0xFF38C7:  //8
      Serial.println("1_CYAN");
      setColor(ledAnalogOne, CYAN);
      break;
      
     case 0xFF5AA5: //9
      Serial.println("1_MAGENTA");
      setColor(ledAnalogOne, MAGENTA);
      break;
      
     case 0xFF4AB5:  //0
      Serial.println("1_WHITE");
      setColor(ledAnalogOne, WHITE);
      break;
       
     case 0xFF42BD:  //*
      Serial.println("1_ZINK");
      setColor(ledAnalogOne, ZINK);
      break;
       }
   irrecv1.resume();
  }  
   
       
  if (irrecv2.decode(&results2)) {
       
    switch(results2.value) {
      
      case 0xFFA857:   //DOWN
       Serial.println("RGB_LED2");  
       setColor(ledAnalogTwo, WHITE);
       setColor(ledAnalogTwo, BLACK);
       break;
       
     case 0xFF6897:  //1
      Serial.println("2_RED"); 
      setColor(ledAnalogTwo, RED);
      break;
       
     case 0xFF9867:  //2
      Serial.println("2_ORANGE"); 
      setColor(ledAnalogTwo, ORANGE);
      break;
       
     case 0xFFB04F:  //3
      Serial.println("2_YELLOW");
      setColor(ledAnalogTwo, YELLOW);
      break;
       
     case 0xFF30CF:  //4
      Serial.println("2_GREEN");
      setColor(ledAnalogTwo, GREEN);
      break;
       
     case 0xFF18E7: //5
      Serial.println("2_BLUE");
      setColor(ledAnalogTwo, BLUE);
      break;
       
     case 0xFF7A85:  //6
      Serial.println("2_INDIGO");
      setColor(ledAnalogTwo, INDIGO);
      break;
      
     case 0xFF10EF: //7
      Serial.println("2_VIOLET");
      setColor(ledAnalogTwo, VIOLET);
      break;
    
     case 0xFF38C7:  //8
      Serial.println("2_CYAN");
      setColor(ledAnalogTwo, CYAN);
      break;
      
     case 0xFF5AA5: //9
      Serial.println("2_MAGENTA");
      setColor(ledAnalogTwo, MAGENTA);
      break;
      
     case 0xFF4AB5:  //0
      Serial.println("2_WHITE");
      setColor(ledAnalogTwo, WHITE);
      break;
       
     case 0xFF42BD:  //*
      Serial.println("2_ZINK");
      setColor(ledAnalogTwo, ZINK);
      break;
       }
     irrecv2.resume(); // Receive the next value
  }
}


  
 void setColor(int* led, byte* color){
 for(int i = 0; i < 3; i++){             //iterate through each of the three pins (red green blue)
   analogWrite(led[i], 255 - color[i]);  //set the analog output value of each pin to the input value (ie led[0] (red pin) to 255- color[0] (red input color)
                                         //we use 255 - the value because our RGB LED is common anode, this means a color is full on when we output analogWrite(pin, 0)
                                         //and off when we output analogWrite(pin, 255). 
 }
}

/* A version of setColor that takes a predefined color (neccesary to allow const int pre-defined colors */
void setColor(int* led, const byte* color){
 byte tempByte[] = {color[0], color[1], color[2]};
 setColor(led, tempByte);
}

/* Fades the LED from a start color to an end color at fadeSpeed
   led - (int array of three values defining the LEDs pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin))
   startCcolor - (byte array of three values defing the start RGB color (startColor[0] = start Red value, startColor[1] = start Green value, startColor[2] = start Red value
   endCcolor - (byte array of three values defing the finished RGB color (endColor[0] = end Red value, endColor[1] = end Green value, endColor[2] = end Red value
   fadeSpeed - this is the delay in milliseconds between steps, defines the speed of the fade
*/ 
void fadeToColor(int* led, byte* startColor, byte* endColor, int fadeSpeed){
  int changeRed = endColor[0] - startColor[0];                            //the difference in the two colors for the red channel
  int changeGreen = endColor[1] - startColor[1];                          //the difference in the two colors for the green channel 
  int changeBlue = endColor[2] - startColor[2];                           //the difference in the two colors for the blue channel
  int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue))); //make the number of change steps the maximum channel change
  
  for(int i = 0 ; i < steps; i++){                                        //iterate for the channel with the maximum change
   byte newRed = startColor[0] + (i * changeRed / steps);                 //the newRed intensity dependant on the start intensity and the change determined above
   byte newGreen = startColor[1] + (i * changeGreen / steps);             //the newGreen intensity
   byte newBlue = startColor[2] + (i * changeBlue / steps);               //the newBlue intensity
   byte newColor[] = {newRed, newGreen, newBlue};                         //Define an RGB color array for the new color
   setColor(led, newColor);                                               //Set the LED to the calculated value
   delay(fadeSpeed);                                                      //Delay fadeSpeed milliseconds before going on to the next color
  }
  setColor(led, endColor);                                                //The LED should be at the endColor but set to endColor to avoid rounding errors
}

/* A version of fadeToColor that takes predefined colors (neccesary to allow const int pre-defined colors */
void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed){
  byte tempByte1[] = {startColor[0], startColor[1], startColor[2]};
   byte tempByte2[] = {endColor[0], endColor[1], endColor[2]};
   fadeToColor(led, tempByte1, tempByte2, fadeSpeed);
}

Are you sure you are receiving the codes from the remote control correctly?

If so, what variables in your sketch hold the received values?

(I'm trying to separate IR problems from logic problems).

...R

Hi 1rulon1

int RECV_PIN = 12;
IRrecv irrecv1(RECV_PIN);
IRrecv irrecv2(RECV_PIN);
decode_results results1;
decode_results results2;

Creating two instances of IRrecv on the same physical IR receiver won't achieve what you are trying to do. Remove one of them and work on getting the rest of your code to interpret the sequence of codes received from the one receiver.

Each time round loop(), you will need to check if a code has been received. If it is "UP" or "DOWN", you need to remember which LED needs to change, and you know that the next code you are looking for needs to be 0 .. 9. When you receiver the 0 .. 9, you have enough information to call the LED functions.

Your application is an example of a "finite state machine". There are threads on this website about them.

Regards

Ray

@ Robin 2, the IR setup works with just one RGB, it's definitely a logic problem.

@ Ray, thanks for the direction, Ill get working on it and see if I can figure that out.

1rulon1: @ Robin 2, the IR setup works with just one RGB, it's definitely a logic problem.

You didn't say what data the Arduino receives from the IR.

...R