LED RGB Dimming using a pot

Hi
I am new to the arduino. I am trying to get an LED strip connected to 3 pwm pins to dim using a pot connected to an analog input. They will be powered by a mosfet, one for each channel (R,G,B).
The overall program I would like to do is Part 1 - have a push button to control the colours through 13 different colours, Part 2 - Be able to dim any colour using a pot. I have done Part 1 using the code below. I know it still needs some de-bouncing code! but I am not worried about that at the moment. All the below code is in the loop section and RGBPB has been defined as an integer 1 and has been set as an input. R, G and B have all been defined as integers 9, 10 and 11 respectfully and have been set as an output.

//Counter for colour selection
  int i = 1
  if(RGBPB = high) i=i+1;
  if(i=14) i = 1;
   
  if(i=1) R=255 && G=0 && B=0;   //Sets colour Red
  if(i=2) R=255 && G=125 && B=0;   //Sets colour Orange
  if(i=3) R=255 && G=225 && B=0;   //Sets colour Yellow
  if(i=4) R=125 && G=255 && B=0;   //Sets colour Spring Green
  if(i=5) R=0 && G=255 && B=0;   //Sets colour Green
  if(i=6) R=0 && G=255 && B=125;   //Sets colour Turquoise
  if(i=7) R=0 && G=255 && B=255;   //Sets colour Cyan
  if(i=8) R=0 && G=125 && B=255;   //Sets colour Ocean
  if(i=9) R=0 && G=0 && B=255;   //Sets colour Blue
  if(i=10) R=125 && G=0 && B=255;   //Sets colour Violet
  if(i=11) R=255 && G=0 && B=255;   //Sets colour Magenta
  if(i=12) R=255 && G=0 && B=125;   //Sets colour Raspberry
  if(i=13) R=255 && G=255 && B=255;   //Sets colour White
    
  //Writes new scaled value to R,G,B outputs
  analogWrite(R, LEDDimmerR);
  analogWrite(G, LEDDimmerG);
  analogWrite(B, LEDDimmerB);

I still do not have my arduino but from what I have read the above should work. Yes is is missing some code but that is not relevant I think.

However I now would like to be able to dim the LED strip on any given colour (Part 2). The problem I am having is that R,G,B do not all increase and decrease by the same amount to dim a given colour. ie for orange say (255,125,0) a dimmed orange would be (255,190,125) a difference of (0,65,125) which is not linear.
What I need is the formula (if there is one) to enable me to do a map value of the input to a value of 0 to 255 then input this into a formula and then use it as the output for R, G and B.
Hope this makes sense. If any more info would help just let me know.
Any help or guidance would be greatly appreciated.

but from what I have read the above should work.

Sorry that code will not run at all, the syntax is all wrong.
There is no formular and it is not as easy as you think.

Just the if statements? Would this work?

//Sets colour Red 
if(i=1) {  
    R=255 
    G=0 
    B=0
  }

I know it will not be easy but non the less I would like to give it a go. Just a push in the right direction is all I am after. You obviously know a bit on the topic.

@ grantastley

I have a program that does almost what you want. Bear with me until I get home and I'll post it here for you.

Regards
Tobias

grantastley:

//Counter for colour selection

int i = 1
 if(RGBPB = high) i=i+1;
 if(i=14) i = 1;

i is always 14 here

grantastley:

  if(i=1) R=255 && G=0 && B=0;   //Sets colour Red

i is always 1 here, and that's not how you set multiple values

grantastley:

analogWrite(R, LEDDimmerR);

Please read analogWrite() - Arduino Reference for the proper syntax

grantastley:
I still do not have my arduino but from what I have read the above should work.

Not in a million years.

grantastley:
Yes is is missing some code but that is not relevant I think.

You may want to reconsider that thought.

grantastley:
for orange say (255,125,0) a dimmed orange would be (255,190,125)

That's not dimming. That's decreasing the saturation. Sounds like you need to read up on color spaces first.

Hi Tobbera

Am happy to wait. Thank you and look forward to the code :slight_smile:

When using an if statement you always need a double equals sign ==.
Statements end in a colon ;

What you need is HSV colour space, the V is the value or brightness. You change that.

However try and compile the code on your arduino IDE, you do not need an actual arduino for that.

Grumpy_Mike:
When using an if statement you always need a double equals sign ==.
Statements end in a colon ;

Semicolon...

Have updated code. This code now compiles and hopefully should work. All I have done so far is the push button function to change the colour of the RGB led strip.
I still need to do some more research about HSV and adjusting the value V using the pot.

//Set variables
int state=LOW;
int laststate=LOW;
int count=0;
int remainder=0;
int R=0;            //Sets Red LED PWM to 0
int G=0;            //Sets Green LED PWM to 0
int B=0;            //Sets Blue LED PWM to 0

//Set constants, Pin numbers
const int LEDR=9;   //Sets Red LED to PWM pin 9
const int LEDG=10;  //Sets Green LED to PWM pin 10
const int LEDB=11;  //Sets Blue LED to PWM pin 11
const int LEDPB=1;  //Sets LED Push Button to pin 1

void setup(){
 //Set pin types
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(LEDPB, INPUT);
  state = digitalRead(LEDPB);
}

void loop(){
  if (state==HIGH && laststate==LOW){
    count++;
    remainder=count%14;
  }
  //Sets colour to Red
  if(remainder==1){
    R=255;
    G=0;
    B=0;
  }
  //Sets colour to Orange
  if(remainder==2){
    R=255;
    G=125;
    B=0;
  }
  //etc to (remainder==14)
  
  analogWrite(LEDR,R);
  analogWrite(LEDG,G);
  analogWrite(LEDB,B);
  
  laststate=state;
  delay(20);
  state=digitalRead(LEDPB);
}

Any comments or improvements or ideas welcome.

If you want to restrain a count don't do

    count++;
    remainder=count%14;

But

    count++;
    if(count >13) count = 1;

The code is all rather turgid with the same thing over and over. All that repeating could be condensed into three lines if you used arrays to look things up:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html
It would then look like:-

// at the start of your sketch define your look up arrays
byte Rlook[] = { 255, 255, ...... and so on }; // only you know these values
byte Glook[] = { 0, 125, ...... and so on };
byte Blook[] = { 0, 0, ...... and so on };
// then inside your function use this:-
 count++;
    if(count >13) count = 0;
R = Rlook[count];
G = Glook[count];
B = Blook[count];

That will get rid of all those if statements.

I still need to do some more research about HSV

Hi Mike
That makes the code much more condensed and makes a lot of sense.
Thanx for the helpful tips.

Hi KirAsh4

Quote from: grantastley on June 13, 2013, 07:08:47 am

analogWrite(R, LEDDimmerR);

Please read analogWrite() - Arduino Reference for the proper syntax

This is the proper syntax R is defined as pin9 and LEDDimmerR is defined as a value between 0 and 255 depending on conditions. Note code has now changed so this is no longer relevant.

Quote from: grantastley on June 13, 2013, 07:08:47 am

for orange say (255,125,0) a dimmed orange would be (255,190,125)

That's not dimming. That's decreasing the saturation. Sounds like you need to read up on color spaces first.

This is actually adjusting the luminosity not the saturation. You can check this very quickly using the colour picker in a word doc etc go to more colours, custom and type in the first values given in the RGB tab then swap over to the HSL tab take note of all values of HSL then go back to the RGB tab and type in the second lot of values for RGB and go back to the HSL tab and you will see that only L has changed.

As for the rest of your remarks - I am always willing to receive constructive criticism but just pointing out what is wrong with no attempt to try help fix these problems, why even post!

grantastley:
Hi KirAsh4

Quote from: grantastley on June 13, 2013, 07:08:47 am

analogWrite(R, LEDDimmerR);

Please read analogWrite() - Arduino Reference for the proper syntax

This is the proper syntax R is defined as pin9 and LEDDimmerR is defined as a value between 0 and 255 depending on conditions. Note code has now changed so this is no longer relevant.

This may no longer be relevant, however it's still not correct according to your own code. You state here that R is defined at pin9, however in your code, as you quoted it in your very first post has R holding a value from 0 to 255.if(i=1) R=255 && G=0 && B=0;  //Sets colour Red
I don't know of any Arduino (or AVR for that matter) that has 255 io pins.

Quote from: grantastley on June 13, 2013, 07:08:47 am
As for the rest of your remarks - I am always willing to receive constructive criticism but just pointing out what is wrong with no attempt to try help fix these problems, why even post!

No one said you had to accept my reply. At least I pointed out individual errors, which is more than what Mike has said in his first reply. I'm not here to solve all of your mistakes, however I will point out where your errors are which is more than what a lot of others will do.

if(i=1) R=255 && G=0 && B=0;   //Sets colour Red

Hi yes point taken, that is a typo where R should have been LEDDimmerR, etc.

And no I do not expect you to solve all of my mistakes but it does not take long to point out a couple of quick fixes such as == instead of = as you go.
Yes not posting the answer made me do I more research and find the answer myself which is a good way to learn, but still a little help while you are going is very helpful to a arduino newbie such as myself.

So I got the dimmer working for the colour Orange :slight_smile: Here is the void loop part of the code, Colour Red is just using the push button and Orange uses the push button to change colours and the pot(Dimmer) to control the luminance. One colour down only 12 more to go :slight_smile:

void loop(){
  if (state==HIGH && laststate==LOW){
    count++;
    if(count>13) count=1;
  }

  //Sets colour to Red
  if(count==1){
    R=255;
    G=0;
    B=0;
  }
  //Sets colour to Orange and is dimmable(Basic Orange R=255, G=125, B=0)
  if(count==2){
if (Dimmer>512){
R=255;
G=(125+(0.24414*(Dimmer-512)));
B=(0.498047*(Dimmer-512));
}
if (Dimmer<512){
R=(255-(0.498047*(512-Dimmer)));
G=(125-(0.24414*(512-Dimmer)));
B=0;
}
 }
  
  analogWrite(LEDR,R);
  analogWrite(LEDG,G);
  analogWrite(LEDB,B);
  
  laststate=state;
  delay(20);
  state=digitalRead(LEDPB);

Finally finished the code :slight_smile:
13 colour selection by a push button (LEDPB) and all colours dimmable by a potentiometer (Dimmer).

//Set variables
int state=LOW;
int laststate=LOW;
int count=0;
int remainder=0;
int R=0;            //Sets Red LED PWM to 0
int G=0;            //Sets Green LED PWM to 0
int B=0;            //Sets Blue LED PWM to 0
int Dimmer;

//Set constants, Pin numbers
const int LEDR=9;   //Sets Red LED to PWM pin 9
const int LEDG=10;  //Sets Green LED to PWM pin 10
const int LEDB=11;  //Sets Blue LED to PWM pin 11
const int LEDPB=1;  //Sets LED Push Button to pin 1

void setup(){
 //Set pin types
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(LEDPB, INPUT);
  pinMode(Dimmer, INPUT);
  state = digitalRead(LEDPB);
}

void loop(){
  if (state==HIGH && laststate==LOW){
    count++;
    if(count>13) count=1;
  }
  //Sets colour to Orange and is dimmable(Basic Orange R=255, G=125, B=0)
  if(count==1){
 if (Dimmer>512){
R=255;
G=(125+(0.24414*(Dimmer-512)));
B=(0.498047*(Dimmer-512));}
 if (Dimmer<512){
R=(255-(0.498047*(512-Dimmer)));
G=(125-(0.24414*(512-Dimmer)));
B=0;}}
   
   //Sets colour to Yellow and is dimmable(Basic Yellow R=255, G=255, B=0)
  if(count==2){
 if(Dimmer>512){
R=255;
G=255;
B=(0.498047*(Dimmer-512));}
 if(Dimmer<512){
R=(255-(0.498047*(512-Dimmer)));
G=(255-(0.498047*(512-Dimmer)));
B=0;}}
   
   //Sets colour to Spring Green and is dimmable(Basic Spring Green R=125, G=255, B=0)
  if(count==3){
 if(Dimmer>512){
R=(125+(0.24414*(Dimmer-512)));
G=255;
B=(0.498047 * (Dimmer-512));}
 if(Dimmer<512){
R=(125-(0.24414*(512-Dimmer)));
G=(255-(0.498047*(512-Dimmer)));
B=0;}}
 
 //Sets colour to Green and is dimmable(Basic Green R=0, G=255, B=0)
  if(count==4){
 if(Dimmer>512){
R=(0.498047*(Dimmer-512));
G=255;
B=(0.498047*(Dimmer-512));}
 if(Dimmer<512){
R=0;
G=(255-(0.498047*(512-Dimmer)));
B=0;}}

//Sets colour to Turquise and is dimmable(Basic Turquise R=0, G=255, B=125)
  if(count==5){
 if(Dimmer>512){
R=(0.498047*(Dimmer-512));
G=255;
B=(125+(0.24414*(Dimmer-512)));}
if(Dimmer<512){
R=0;
G=(255-(0.498047*(512- Dimmer)));
B=(125-(0.24414*(512- Dimmer)));}}

//Sets colour to Cyan and is dimmable(Basic Cyan R=0, G=255, B=255)
  if(count==6){
 if(Dimmer>512){
R=(0.498047*(Dimmer-512));
G=255;
B=255;}
if(Dimmer<512){
R=0;
G=(255-(0.498047*(512-Dimmer)));
B=(255-(0.498047*(512-Dimmer)));}}

//Sets colour to Ocean and is dimmable(Basic Ocean R=0, G=125, B=255)
  if(count==7){
 if(Dimmer>512){
R=(0.498047*(Dimmer-512));
G=(125+(0.24414*(Dimmer-512)));
B=255;}
if(Dimmer<512){
R=0;
G=(125-(0.24414*(512-Dimmer)));
B=(255-(0.498047*(512-Dimmer)));}}

//Sets colour to Blue and is dimmable(Basic Blue R=0, G=0, B=255)
  if(count==8){
 if(Dimmer>512){
R=(0.498047*(Dimmer-512));
G=(0.498047*(Dimmer-512));
B=255;}
if(Dimmer<512){
R=0;
G=0;
B=(255-(0.498047*(512-Dimmer)));}}

//Sets colour to Violet and is dimmable(Basic Violet R=125, G=0, B=255)
  if(count==9){
 if(Dimmer>512){
R=(125+(0.24414*(Dimmer-512)));
G=(0.498047*(Dimmer-512));
B=255;}
if(Dimmer<512){
R=(125-(0.24414*(512-Dimmer)));
G=0;
B=(255-(0.498047*(512-Dimmer)));}}

//Sets colour to Magenta and is dimmable(Basic Magenta R=255, G=0, B=255)
  if(count==10){
 if(Dimmer>512){
R=255;
G=(0.498047*(Dimmer-512));
B=255;}
if(Dimmer<512){
R=(255-(0.498047*(512-Dimmer)));
G=0;
B=(255-(0.498047*(512-Dimmer)));}}

//Sets colour to Raspberry and is dimmable(Basic Raspberry R=255, G=0, B=125)
  if(count==11){
 if(Dimmer>512){
R=255;
G=(0.498047*(Dimmer-512));
B=(125+(0.24414*(Dimmer-512)));}
if(Dimmer<512){
R=(255-(0.498047*(512-Dimmer)));
G=0;
B=(125-(0.24414*(512-Dimmer)));}}

//Sets colour to Red and is dimmable(Basic Red R=255, G=0, B=0)
  if(count==12){
 if(Dimmer>512){
R=255;
G=(0.498047*(Dimmer-512));
B=(0.498047*(Dimmer-512));}
if(Dimmer<512){
R=(255-(0.498047*(512-Dimmer)));
G=0;
G=0;}}

//Sets colour to White and is dimmable(Basic White R=125, G=125, B=125)
  if(count==13){
 if(Dimmer>512){
R=(125+(0.24414*(Dimmer-512)));
G=(125+(0.24414*(Dimmer-512)));
B=(125+(0.24414*(Dimmer-512)));}
if(Dimmer<512){
R=(125-(0.24414*(512- Dimmer)));
G=(125-(0.24414*(512- Dimmer)));
B=(125-(0.24414*(512- Dimmer)));}}
  
  analogWrite(LEDR,R);
  analogWrite(LEDG,G);
  analogWrite(LEDB,B);
  
  laststate=state;
  delay(20);
  state=digitalRead(LEDPB);
}

Let me know what you think :slight_smile:

Let me know what you think

Does it do what you want?
If so well done! :slight_smile:

Well all the formula for each colour works to dim them properly and the code compiles :slight_smile: still waiting on the arduino uno to arrive though to test it out fully. But I think it should work. Once I get it all I will post a link to a video of it all up and going.
Bellow is the spreed sheet I made to get the formula.

RGB.xlsx (32.3 KB)