Controlling brightness RGB with method

So I'm working on a little project and want to control the brightness for the RGB led. I tried to do it the way you do it if you don't have a method for the color setting, but that didn't work. So I'm kind of lost where to start since I can't find anything about it.

int RedLight = 11;
int GreenLight = 10;
int BlueLight = 9;


void setup() {
  pinMode(RedLight, OUTPUT) ;
  pinMode(GreenLight, OUTPUT) ;
  pinMode(BlueLight, OUTPUT) ;
}

void loop() {
  //brightness scale from 0 - 255
colorSetting(255,0,0); // want brightness 255
delay(2000);
colorSetting(255,0,0); //want brightness 100

}

void colorSetting(int red, int green, int blue) //writes an analog value to a pin
{ // making use of common anode, so have to substract the color value from 255 in order to set the rgb color
  analogWrite( RedLight, 255 - red ) ;
  analogWrite( GreenLight, 255 - green ) ;
  analogWrite( BlueLight, 255 - blue ) ;
}

Post your attempt that didn't work and explain what you mean by "didn't work".

I managed to figure it out myself. For anyone who will come across this topic to look for help, this is how i fixed it.

int RedLight = 11
int GreenLight = 10;
int BlueLight = 9;

void setup() {
  pinMode(RedLight, OUTPUT) ;
  pinMode(GreenLight, OUTPUT) ;
  pinMode(BlueLight, OUTPUT) ;
}

void loop() {
colorSetting(255,0,0);
analogWrite(RedLight, 0);
delay(2000);
colorSetting(255,0,0);
analogWrite(RedLight, 100);
}
void colorSetting(int red, int green, int blue) //writes an analog value to a pin
{ // making use of common anode, so have to substract the color value from 255 in order to set the rgb color
  analogWrite( RedLight, 255 - red ) ;
  analogWrite( GreenLight, 255 - green ) ;
  analogWrite( BlueLight, 255 - blue ) ;
}
 colorSetting(255,0,0); // want brightness 255

You got it.

colorSetting(255,0,0); //want brightness 100

Why not try

colorSetting(100,0,0); //want brightness 100

Edit

I managed to figure it out myself.

No you didn’t. Not with that code. You must have used something else cos that is just silly.
You need a delay at the last thing in the loop function and remove the unnecessary analogue write to pin 0.

Edit 2 I see you have removed the unnecessary function call and replaced it with a redundant one. After I made that comment.

Try this experiment.

Send 1,0,0 to the RGB LED, what color do you get?

Send 50,0,0 to the RGB LED, what color do you get?

Send 150,0,0 to the RGB LED, what color do you get?

Send 200,0,0 to the RGB LED, what color do you get?

Send 255,0,0 to the RGB LED, what color do you get?

I am going to bet that in all cases you get a red colored LED. Which leads to the question what did changing the red value do? Did it make the LED get brighter when the R number was increased? Getting the feeling that the brightness of the LED is proportional to the value sent to the LED?

Ok just to underlying things your loop function should read

 void loop() {
  //brightness scale from 0 - 255
colorSetting(255,0,0); // want brightness 255
delay(2000);
colorSetting(100,0,0); //want brightness 100
delay(2000);

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.