RGB LED controlled by controller.

Hi everyone!

Very recently I’ve been messing around with my Arduino Kit. I only have very limited knowledge with programming, hence why I’m on here hoping to find a solution.

I’ve hooked up an RGB light to be controlled by a controller with a keypad. So i can change the colour from Red, Blue and green by clicked 1, 2 or 3.

I wanted to add to the script and be able to change the brightness on whatever selected colour i have by pressing the up and down arrow on the controller.

I’ve tried searching around for a solution but i cannot seem to get it work and im obviously missing something crucial. Any help would be much appreciated. I’ll post the code below if anyone could take a look.

#include <IRremote.h>

  const int RECV_PIN = 7;
  IRrecv irrecv(RECV_PIN);
  decode_results results;
  
  int redPin = 11;
  int greenPin = 10;
  int bluePin = 6;

  int redValue = 255;
  int greenValue = 255;
  int blueValue = 255;

  int colourMode = 0;
  
void setup() {
  // put your setup code here, to run once:
  irrecv.enableIRIn();
  irrecv.blink13(true);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (irrecv.decode(&results)){

    switch(results.value){
      case 0xFF30CF: //Keypad button 1
      redValue = 255;
      greenValue = 0;
      blueValue = 0;
      RGB_colour(redValue, 0, 0);
      colourMode = 1;
      }

     switch(results.value){
      case 0xFF18E7: //Keypad button 2
      redValue = 0;
      greenValue = 255;
      blueValue = 0;
      RGB_colour(0, greenValue, 0);
      colourMode = 2;
      }

     switch(results.value){
      case 0xFF7A85: //Keypad button 3
      redValue = 0;
      greenValue = 0;
      blueValue = 255;
      RGB_colour(0, 0, blueValue);
      colourMode = 3;
      }

     switch(results.value){
      case 0xFFA25D: //Keypad button power
      RGB_colour(0, 0, 0);
      colourMode = 4;
      }

     switch(results.value){
      case 0xFFE01F: //Keypad button down
      RGB_Brightness_Down(redValue, greenValue, blueValue);
      }

    irrecv.resume();
  }
}

void RGB_colour(int redValue, int greenValue, int blueValue){
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
  }

int RGB_Brightness_Down(int redValue, int greenValue, int blueValue){
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);

  switch(colourMode){
    case 0:
    if (colourMode == 1 && redValue <=255){
      redValue = redValue - 10;
      return(redValue);
    }
  }
}

A switch statement with one case does not make sense. Put all of your cases under ONE switch statement.

      RGB_Brightness_Down(redValue, greenValue, blueValue);

So, why does RGB_Brightness_Down() need to return a value?

  switch(colourMode){
    case 0:
    if (colourMode == 1 && redValue <=255){
      redValue = redValue - 10;
      return(redValue);
    }

The only way that the if statement will be evaluated is if colourMode equals 0. The odds of it then equaling 1 are nearly nonexistent.

Here’s my advice.

Combine your switch cases into one switch statement and confirm it still works.

  if (irrecv.decode(&results)) {

    switch (results.value) {
      case 0xFF30CF: //Keypad button 1
        redValue = 255;
        greenValue = 0;
        blueValue = 0;
        RGB_colour(redValue, 0, 0);
        colourMode = 1;
        break;

      case 0xFF18E7: //Keypad button 2
        redValue = 0;
        greenValue = 255;
        blueValue = 0;
        RGB_colour(0, greenValue, 0);
        colourMode = 2;
        break;

      case 0xFF7A85: //Keypad button 3
        redValue = 0;
        greenValue = 0;
        blueValue = 255;
        RGB_colour(0, 0, blueValue);
        colourMode = 3;
        break;

      case 0xFFA25D: //Keypad button power
        RGB_colour(0, 0, 0);
        colourMode = 4;
        break;

      default:
        break;

    }

  }

Put your RGB values into an array.

int redValue = 255;
int greenValue = 255;
int blueValue = 255;

int rgbValues[] = {255, 255, 255};

Now modify your switch cases to use your new array, for example

      case 0xFF30CF: //Keypad button 1
        rgbValues[0] = 255;
        rgbValues[1] = 0;
        rgbValues[2] = 0;
        colourMode = 1;
        break;

Add your call to RGB_colour() after the switch statement but inside the if statement confirming you got something from the IR and confirm it still works.

    RGB_colour(rgbValues[0], rgbValues[1], rgbValues[2]);

Now it should be easy to add your up/down cases, for example

      case 0xFFE01F: //Keypad button down

        // dim led
        rgbValues[colourMode - 1] = rgbValues[colourMode - 1] - 10;

        // don't let value go below zero
        if ( rgbValues[colourMode - 1] < 0 ) rgbValues[colourMode - 1] = 0;

        break;

Hi! really appreciate your time trying to help me out. I have adjusted my code to use an array and can clearly see it was the better option. However, I still cannot seem to be able to dim the light. I’ll post my code below.

#include <IRremote.h>

  const int RECV_PIN = 7;
  IRrecv irrecv(RECV_PIN);
  decode_results results;
  
  const int redPin = 11;
  const int greenPin = 10;
  const int bluePin = 6;

  static int rgbValues[] = {255, 255, 255};
  
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  irrecv.enableIRIn();
  irrecv.blink13(true);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (irrecv.decode(&results)){

    switch(results.value){
      
      case 0xFF30CF: //Keypad button 1
      rgbValues[0] = 255;
      rgbValues[1] = 0;
      rgbValues[2] = 0;
      break;

      case 0xFF18E7: //Keypad button 2
      rgbValues[0] = 0;
      rgbValues[1] = 255;
      rgbValues[2] = 0;
      break;


      case 0xFF7A85: //Keypad button 3
      rgbValues[0] = 0;
      rgbValues[1] = 0;
      rgbValues[2] = 255;
      break;


      case 0xFF6897: //Keypad button 0
      rgbValues[0] = 0;
      rgbValues[1] = 0;
      rgbValues[2] = 0;
      break;

      case 0xFF10EF: //Keypad button 4
      rgbValues[0] = 255;
      rgbValues[1] = 255;
      rgbValues[2] = 255;
      break;

      case 0xFF02FD: //Keypad button play/pause
      Serial.print("Red value is "); Serial.println(rgbValues[0]);
      Serial.print("Green value is "); Serial.println(rgbValues[1]);
      Serial.print("Blue value is "); Serial.println(rgbValues[2]);
      break;
      

      case 0xFFE01F: //Keypad button down
      if(rgbValues[0] >= 10){
        rgbValues[0] = rgbValues[0] - 10;
        }
      else{
        rgbValues[0] = rgbValues[0] * 1;
        }
        
      if(rgbValues[1] >= 10){
        rgbValues[1] = rgbValues[1] - 10;
        }
      else{
        rgbValues[1] = rgbValues[1] * 1;
        }
        
      if(rgbValues[2] >= 10){
        rgbValues[2] = rgbValues[2] - 10;
        }
      else{
        rgbValues[2] = rgbValues[2] * 1;
        }
        
      Serial.print("Red value is "); Serial.println(rgbValues[0]);
      Serial.print("Green value is "); Serial.println(rgbValues[1]);
      Serial.print("Blue value is "); Serial.println(rgbValues[2]);
      break;
    }

     RGB_colour(rgbValues[0], rgbValues[1], rgbValues[2]);
     
    irrecv.resume();
  }
}

void RGB_colour(int redValue, int greenValue, int blueValue){
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
  }

I can confirm that when I switch through the different colours and print the values on screen they do change to the specified value. I can flick between them as much as I want. When i press 0 to change the RGB values to 0,0,0 and try to dim then the values stay at 0 as I wanted and I can flick back to another colour.

However, when I try to dim with a value above 10 then the specified amount is taken off the RGB value, it is printed to the screen but the actual RGB light does not change colour and I cannot change the colour back after pressing the dim button. I have to reset the board to be able to flip between the colours again.

Thanks again if anyone is able to help me out.

      else{
        rgbValues[0] = rgbValues[0] * 1;
        }

The statement in the body of the else statement is pointless. If you don't have anything to do, when the of statement evaluates to false, leave out the else statement. Don't just point something useless in the body.

Put your Serial.print() statements, once, after the end of switch statement. Or, make sure that there are print statements in EVERY case. Printing only in some cases is VERY confusing.

PaulS:       else{         rgbValues[0] = rgbValues[0] * 1;         }

The statement in the body of the else statement is pointless. If you don't have anything to do, when the of statement evaluates to false, leave out the else statement. Don't just point something useless in the body.

Put your Serial.print() statements, once, after the end of switch statement. Or, make sure that there are print statements in EVERY case. Printing only in some cases is VERY confusing.

noted. However this has no effect on the problem im having with the LED being stuck. Appreciate the advice nonetheless.

I would also like to say that I CAN dim the blue and green LED's and swap back to another colour. The only issue im having is dimming a light where the redValue is higher than 0. When I do it turns the LED completely off and i cannot select another colour without resetting the board.

However this has no effect on the problem im having with the LED being stuck.

You need to show us some proof that the code (LEDs can't get stuck) is not performing correctly.

That would involve printing the color values EVERY time any of them is changed, and printing something that indicates which remote button was pressed. Hand-waving only works if you are holding semaphore flags AND we can see you. Assuming, of course, that you know how to hold them and we know how to read them.

So much simpler to post some text from the Serial Monitor application.

Maybe a conflict with the IR library? Try putting the red LED on a different PMW pin, like 9. If that doesn't work please post which Arduino you are using the the text in the serial monitor.