Setting Brightness of RGB LED with a remote control

The "Error Code" which is given on the serial monitor says "0xFF"

That sounds like a run time error. I have never seen one before on the Arduino, in fact I don’t think they actually exist.

Can you post the full text if this error message along with your code so we can try and reproduce it ourselves.

Hi,

Here is my latest Code:

#include <IRremote.h>

//Setting the Pins
const int recvPin = 5; //IR Input
int redPin = 11;
int greenPin = 10;
int bluePin = 9;

unsigned long rgb;
byte r,g,b;

//Defining the Hex-Code for each button.
//Each Button has 2 Hex-Codes since I got 2 different Hex-Codes as I executed another Program to determine the Codes
#define RED               0xFFA25D
#define RED_val2          0xE318261B
#define GREEN             0XFF629D
#define GREEN_val2        0x511DBB
#define BLUE              0xFFE21D
#define BLUE_val2         0xEE886D7F
#define BRIGHT_UP         0xFF18E7
#define BRIGHT_UP_val2    0x3D9AE3F7
#define BRIGHT_DOWN       0xFF4AB5
#define BRIGHT_DOWN_val2  0x1BC0157B
#define OFF               0xFF38C7
#define OFF_val2          0x488F3CBB

#define BRIGHT_SCALE 10



//Initializing the Receiver Pin
IRrecv irrecv(recvPin);
decode_results results; //decoding Decode-Results in variable "results"
unsigned long key_value = 0; //storing code when same button is pressed again



void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); //Starting Receiver
  irrecv.blink13(true); //Arduino led blinking when receiving an incoming button-code
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

//Setting the Brightness. 
//But not for each color individually
void brightness(byte* color, char steps){
    if(steps > 0){
      if(*color + steps <= 255){
        *color += steps;
      }else{
        *color = 255;
      }
    }else{
      if(*color + steps >= 0){
        *color += steps;
      }else{
        *color = 0;
      }
    }
}




void RGB(unsigned long num){
  r = num >> 16;
  g = (num >> 8) & 0xFF;
  b = num & 0xFF;

}


void loop() {
  if(irrecv.decode(&results)){
    if(results.value != 0xFFFFFFFF){
      //results.value = key_val; //Storing button hex-code if same button is pressed again
    
    switch(results.value){
     case RED_val2:
     case RED:             RGB(0x00FF0000); break;
     case GREEN_val2:
     case GREEN:           RGB(0x0000FF00); break;
     case BLUE_val2:
     case BLUE:            RGB(0x000000FF); break;
     case OFF_val2:
     case OFF:             r = g = b = 0; break;
     case BRIGHT_UP_val2:
     case BRIGHT_UP: 
                       
                     brightness(&r, BRIGHT_SCALE);
                     brightness(&g, BRIGHT_SCALE);
                     brightness(&b, BRIGHT_SCALE);
                     break;
     case BRIGHT_DOWN_val2:
     case BRIGHT_DOWN:
                         
                       brightness(&r, -BRIGHT_SCALE);
                       brightness(&g, -BRIGHT_SCALE);
                       brightness(&b, -BRIGHT_SCALE);
                       break;
    }

    Serial.println(results.value, HEX);
    Serial.println(r, DEC);
    Serial.println(g, DEC);
    Serial.println(g, DEC);
    analogWrite(redPin, r);
    analogWrite(greenPin, g);
    analogWrite(bluePin, b);
    }
    
    //key_value = results.value;
    irrecv.resume(); //Clearing button for next incoming button
  }
}

I've investigated my problem further more.
There are some differences when I press several Buttons:
If I press

  • Button 1 (0xFFA25D) for "RED" and decrement the color by 10 the red Light isn't going down in 10 steps as it should be. After 245 it just turns off and the programm freezes. If I Increment "RED" by 10 and Decrement it again the programm also freezes
  • Button 2 (0xFF629D) for "GREEN" and Increment it the programm freezes aswell. If I Decrement and Increment the color green the programm freezes and tells me on my serial monitor instead of the button 2 (0xFF629D) a Hex-Code of "FF" and the programm freezes.
  • Button 3 (0xFFE21D) for "BLUE" and decrement it everything is working fine and it goes down to 0. But after Incrementing it again it also tells me "FF" or "3D".

I don't know where this comes from.
So this isn't an actual error message, it's just an Output of the Serial Monitor.
The IR-Receiver is still receiving incomfing messages from my Remote Control, but the serial monitor doesn't display anything.

I've attached some photos of the output of each listed option

PS: @PaulRB: I didn't want to be mean in my last comment to you. I am sorry.

Blue_Increment_Decrement_Freeze.PNG

Green_Decrement_Encrement_merged.PNG

Green_Decrement_Increment_Freeze.PNG

Green_Increment_Freeze.PNG

Red_Increment_Decrement_Freeze.PNG

Red_light_Decrement_off.PNG

Blue_Increment_Decrement_Freeze.PNG

Green_Decrement_Encrement_merged.PNG

Green_Decrement_Increment_Freeze.PNG

Green_Increment_Freeze.PNG

Red_Increment_Decrement_Freeze.PNG

Red_light_Decrement_off.PNG

Thanks. But in the future, please just select & copy the text from the serial monitor and paste into your post between code tags.

Try this:

#include <IRremote.h>

//Setting the Pins
const byte recvPin = 5; //IR Input
const byte redPin = 11;
const byte greenPin = 10;
const byte bluePin = 9;

unsigned long rgb;
byte r, g, b;
int brightness = 100;

//Defining the Hex-Code for each button.
//Each Button has 2 Hex-Codes since I got 2 different Hex-Codes as I executed another Program to determine the Codes
#define RED               0xFFA25D
#define RED_val2          0xE318261B
#define GREEN             0XFF629D
#define GREEN_val2        0x511DBB
#define BLUE              0xFFE21D
#define BLUE_val2         0xEE886D7F
#define BRIGHT_UP         0xFF18E7
#define BRIGHT_UP_val2    0x3D9AE3F7
#define BRIGHT_DOWN       0xFF4AB5
#define BRIGHT_DOWN_val2  0x1BC0157B
#define OFF               0xFF38C7
#define OFF_val2          0x488F3CBB

#define BRIGHT_SCALE 10



//Initializing the Receiver Pin
IRrecv irrecv(recvPin);
decode_results results; //decoding Decode-Results in variable "results"
unsigned long key_value = 0; //storing code when same button is pressed again



void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); //Starting Receiver
  irrecv.blink13(true); //Arduino led blinking when receiving an incoming button-code
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void RGB(unsigned long num) {
  r = num >> 16;
  g = (num >> 8) & 0xFF;
  b = num & 0xFF;

}

void adjust_brightness(int adjust_amount) {
  brightness = constrain(brightness + adjust_amount, 0, 100);
}


void loop() {
  if (irrecv.decode(&results)) {
    if (results.value != 0xFFFFFFFF) {
      //results.value = key_val; //Storing button hex-code if same button is pressed again

      switch (results.value) {
        case RED_val2:
        case RED:             RGB(0x00FF0000); break;
        case GREEN_val2:
        case GREEN:           RGB(0x0000FF00); break;
        case BLUE_val2:
        case BLUE:            RGB(0x000000FF); break;
        case OFF_val2:
        case OFF:             r = g = b = 0; break;
        case BRIGHT_UP_val2:
        case BRIGHT_UP:

          adjust_brightness(BRIGHT_SCALE);
          break;
          
        case BRIGHT_DOWN_val2:
        case BRIGHT_DOWN:

          adjust_brightness(-BRIGHT_SCALE);
          break;
      }

      Serial.print("Code received: "); Serial.println(results.value, HEX);
      Serial.print("Red: "); Serial.println(r);
      Serial.print("Green: "); Serial.println(g);
      Serial.print("Blue: "); Serial.println(b);
      Serial.print("Brightness: "); Serial.println(brightness);
      byte r_adjusted = r * brightness / 100;
      byte g_adjusted = g * brightness / 100;
      byte b_adjusted = b * brightness / 100;
      Serial.print("Adjusted Red: "); Serial.println(r_adjusted);
      Serial.print("Adjusted Green: "); Serial.println(g_adjusted);
      Serial.print("Adjusted Blue: "); Serial.println(b_adjusted);
      analogWrite(redPin, r_adjusted);
      analogWrite(greenPin, g_adjusted);
      analogWrite(bluePin, b_adjusted);
    }

    //key_value = results.value;
    irrecv.resume(); //Clearing button for next incoming button
  }
}

Thanks. But in the future, please just select & copy the text from the serial monitor and paste into your post between code tags.

I will have that in mind thank you.
Your suggested Code worked for me and I really appreciate it. I had something similiar in mind, but I didn't know how to accomplish this.

So the brightness of the color remains when I change it. I think I have a faulty LED. Since the value of "red" is changing by decrement it the LED just shuts off on the first value. And I still don't know why since it works for the other 2 fine.

SchnoppDog:
I think I have a faulty LED. Since the value of "red" is changing by decrement it the LED just shuts off on the first value. And I still don't know why since it works for the other 2 fine.

I found your description confusing. Other 2 what? RGB LEDS? If you have 3 RGB leds and one behaves differently from the other 2 then I agree it is faulty. Or did you mean the other 2 colours in the same RGB LED, i.e. the green and blue? If so, swap the pins around. Does the fault move to another colour?

I found your description confusing

I am sorry. I have 1 RGB-LED and within the three colors.
What do you mean with that:

If so, swap the pins around

Which pin shall I swap? The red one with what exactly?
And no it doesn't chage the color. The red color in the rgb-led is turning off. When it is on full brightness (255) and I decrement it by 10 so it has the brightness value of "245" the color isn't dimming it just turns off (with no reason) although the brightness value of red is set to "245". But the two other colors "green" and "blue" are working fine on "245".

I use for all three colors 220 Ohm resistors. Is this maybe the problem that I have the wrong resistance for red?

Swap red with green. If the red led is still not behaving correctly, then I think it must be the led that is faulty (but I don't understand the nature of the fault). If the red is ok but green does not behave correctly then I think the Arduino pin could be faulty.

You could also try the RGB led on pins 3, 4, 5.

Normally, a higher value resistor is used for the red led. But this is to balance the current that flows through the red led to be similar to the current flowing through the green and blue LEDs, and therefore balance the brightness of all 3 LEDs to achieve a white appearance when all 3 are on together. Using the same resistor value for red would unbalance the current and brightness, making the led appear pink instead of white. But it would not have the effect of turning the red led off at lower pwm values as you have described.