# Adding A Brightness Pot

I wrote some simple code... I want to add a 4th pot for controlling the overall birghtness of the led...

So I add a pot to analog pin 3, and it changes the total brightness of the lights... I dont wanna sound like im lazy, i just dont know how... im only 11...

Here is the original code... please add the lines of code for the brightnss pot... it would be hooked to analog pin 3.

Original Code:

int RledPin = 11;
int GledPin = 10;
int BledPin = 9;
int RpotPin = 0;
int GpotPin = 1;
int BpotPin = 2;
int Rval;
int Gval;
int Bval;

void setup()
{
pinMode(RledPin, OUTPUT);
pinMode(GledPin, OUTPUT);
pinMode(BledPin, OUTPUT);
}

void loop()
{
Rval = map(Rval, 0, 1023, 0, 255);
Gval = map(Gval, 0, 1023, 0, 255);
Bval = map(Bval, 0, 1023, 0, 255);
analogWrite(RledPin, Rval);
analogWrite(GledPin, Gval);
analogWrite(BledPin, Bval);
delay(10);
}

You could add in a pot, get it to read that value, map it to a lower value and then take that away from the R,G&B values before the PWM is done.

I presume you can add a pot, define that, do an analog read, map the value, then take that away from the other values.

If you wrote (or helped to write) the current code then it shouldn't be too difficult.

Congrats for learning arduino at 11. I wish I had learnt coding at 11! ;)

Mowcius

self written and taught... :)

i know how to analogRead and map, but I dont know where it would go...

{
Rval = map(Rval, 0, 1023, 0, 255);
Gval = map(Gval, 0, 1023, 0, 255);
Bval = map(Bval, 0, 1023, 0, 255);

//Put it here.
//analogread the brightness pot
//then map the value 0-255
//then Rval = Rval-brightnessval
//Gval = Gval-brightnessval
//Bval = Bval-brightnessval

analogWrite(RledPin, Rval);
analogWrite(GledPin, Gval);
analogWrite(BledPin, Bval);
delay(10);
}

Mowcius :)

So i wrote it... and it kinda bugs out...

int RledPin = 11;
int GledPin = 10;
int BledPin = 9;
int RpotPin = 0;
int GpotPin = 1;
int BpotPin = 2;
int BrightPin = 3;
int Rval;
int Gval;
int Bval;
int Brightval;

void setup()
{
pinMode(RledPin, OUTPUT);
pinMode(GledPin, OUTPUT);
pinMode(BledPin, OUTPUT);
}

void loop()
{
Rval = map(Rval, 0, 1023, 0, 255);
Gval = map(Gval, 0, 1023, 0, 255);
Bval = map(Bval, 0, 1023, 0, 255);

Brightval = analogRead(BrightPin); //analogread the brightness pot
Brightval = map(Brightval, 0, 1023, 0, 255);//then map the value 0-255
Rval = Rval-Brightval;//then Rval = Rval-brightnessval
Gval = Gval-Brightval;//Gval = Gval-brightnessval
Bval = Bval-Brightval;//Bval = Bval-brightnessval

analogWrite(RledPin, Rval);
analogWrite(GledPin, Gval);
analogWrite(BledPin, Bval);
delay(10);
}

Give this a go. A few changes made. The Brightness pot in this code works as a percentage of overall brightness. You also had not defined the four pins for the pots and input. I have tested this on my bread board and it works fine.

int RledPin = 11;
int GledPin = 10;
int BledPin = 9;
int RpotPin = 0;
int GpotPin = 1;
int BpotPin = 2;
int BRIpotPin = 3;
int Rval;
int Gval;
int Bval;
int BRIval;

void setup()
{
pinMode(RledPin, OUTPUT);
pinMode(GledPin, OUTPUT);
pinMode(BledPin, OUTPUT);
pinMode(RpotPin, INPUT);
pinMode(GpotPin, INPUT);
pinMode(BpotPin, INPUT);
pinMode(BRIpotPin, INPUT);
}

void loop()
{
BRIval = map(BRIval, 0, 1023, 0, 255);
Rval = map(Rval, 0, 1023, 0, 255);
Rval = Rval * (BRIval/255);
Gval = map(Gval, 0, 1023, 0, 255);
Gval = Gval * (BRIval/255);
Bval = map(Bval, 0, 1023, 0, 255);
Bval = Bval * (BRIval/255);

analogWrite(RledPin, Rval);
analogWrite(GledPin, Gval);
analogWrite(BledPin, Bval);
delay(10);
}

i tried meyhem's code, and the led only turns on if the brigthness pot is at the full level.

How do you have your pots wired up?
One end needs to go to 5V, one end needs to go to gnd, and the middle wire on the pot needs to go to the Auruino PWM pin.

i think you mean middle to analog in...and yes, thats how it's wired...

i tried meyhem's code, and the led only turns on if the brigthness pot is at the full level.

Loose the parenthesis, and it should work :)

Parenthesis have a higher precedence than anything, so anything inside parenthesis gets calculated first, and then the rest of the equation from left to right, and according to http://www.intap.net/~drw/cpp/cpp03_05.htm]operator precedence (Notice that, for instance, multiplication and division have the same precedence, so this will be determined by left-to-right sequence of variables. Like if you loose the parenthesis in your brightness formula).

Because all values are integers, and if you divide an integer by a bigger integer (like 250/255, for example), you always get zero inside the parenthesis. Unless, as you say, the pot is at full value, and you get 255/255 = 1.

Rval = Rval * [glow]([/glow]BRIval/255[glow])[/glow];

If you loose the parenthesis, it will (again) calculate the values from left to right. First Rval * BRIval. Since the maximum value is 255 for both, and they are stored in an integer (16 bit), it should hold the hole range (but - they should be unsigned integers for that! - unsigned int Rval, etc). Then this result is divided by 255.

Rval = Rval * BRIval / 255;

Alternatively, add a dot zero behind the constant 255, to make it a "float" datatype (a number with decimals), and it will convert the result inside the parenthesis to a float also (and not discard decimals like an integer does). The result would also be a float, but since the variables getting the results are integers, they will probably be automatically "casted" to integers.

Rval = Rval * (BRIval/[glow]255.0[/glow]);

To make sure, you could cast it to an integer specifically:

Rval = [glow](int)[/glow] [glow]([/glow]Rval * (BRIval/255.0)[glow])[/glow];

or use the int() function:

Rval =[glow]int([/glow]Rval * (BRIval/255.0)[glow])[/glow];

EDIT: some corrections and changes, hopefully to the better.

you guys are genious's...

heres the working code:

int RledPin = 11;
int GledPin = 10;
int BledPin = 9;
int RpotPin = 0;
int GpotPin = 1;
int BpotPin = 2;
int BrightPotPin = 3;
int Rval;
int Gval;
int Bval;
int Brightval;

void setup()
{
pinMode(RledPin, OUTPUT);
pinMode(GledPin, OUTPUT);
pinMode(BledPin, OUTPUT);
pinMode(RpotPin, INPUT);
pinMode(GpotPin, INPUT);
pinMode(BpotPin, INPUT);
pinMode(BrightPotPin, INPUT);
}

void loop()
{
Brightval = map(Brightval, 0, 1023, 0, 255);

Rval = map(Rval, 0, 1023, 0, 255);
Rval = Rval * (Brightval/255.0);
Gval = map(Gval, 0, 1023, 0, 255);
Gval = Gval * (Brightval/255.0);
Bval = map(Bval, 0, 1023, 0, 255);
Bval = Bval * (Brightval/255.0);

analogWrite(RledPin, Rval);
analogWrite(GledPin, Gval);
analogWrite(BledPin, Bval);
delay(10);
}

Alright, cool that you got it working!

I didn’t see your new post there, or else I might not have edited my old one. Anyway, nice work! I’m impressed that you dabble with microcontrollers at 11!

thanks again for helping, and thanks lol about me being 11...

i am almost 12... in about a month... i just hope i get better at this...

i just hope i get better at this...

I have no doubt you will :)

I was just thinking, in case you want to adjust the brightness from completely dark to white: You can try to add this in there before the analogWrites(). Also remove the lines with (Brightval/255.0) in them.

// Normalize brightness and colors to the range 0-1
float brightness = Brightval/255.0;
float red = Rval/255.0;
float green = Gval/255.0;
float blue = Bval/255.0;
if (brightness > 0.5)
{
// potmeter is above half (brighter)
float lum = (2 * brightness) - 1;
red   += (lum * (1-red));
green += (lum * (1-green));
blue  += (lum * (1-blue));
} else {
// potmeter equal to or below half (darker)
float lum = 2 * brightness;
red = red * lum;
green = green * lum;
blue = blue * lum;
}
// Scale up the color levels to 0-255 range again
Rval = red * 255;
Gval = green * 255;
Bval = blue * 255;

This way, when the brightness potentiometer is at exactly half position (if it is a linear potmeter), the colors are unchanged. If it is above half, it will be more white, and if it is below half, darker.

it flickers when you turn the brightness pot down low... how can i fix that?

int RledPin = 11;
int GledPin = 10;
int BledPin = 9;
int RpotPin = 0;
int GpotPin = 1;
int BpotPin = 2;
int BrightPotPin = 3;
int Rval;
int Gval;
int Bval;
int Brightval;

void setup()
{
pinMode(RledPin, OUTPUT);
pinMode(GledPin, OUTPUT);
pinMode(BledPin, OUTPUT);
pinMode(RpotPin, INPUT);
pinMode(GpotPin, INPUT);
pinMode(BpotPin, INPUT);
pinMode(BrightPotPin, INPUT);
}

void loop()
{
Brightval = map(Brightval, 0, 1023, 0, 255);

Rval = map(Rval, 0, 1023, 0, 255);
Rval = Rval * (Brightval/255.0);
Gval = map(Gval, 0, 1023, 0, 255);
Gval = Gval * (Brightval/255.0);
Bval = map(Bval, 0, 1023, 0, 255);
Bval = Bval * (Brightval/255.0);

analogWrite(RledPin, Rval);
analogWrite(GledPin, Gval);
analogWrite(BledPin, Bval);
delay(10);
}

@Jeremy When mapping a analog in to a PWM output, there are 1024 possible inputs (0 to 1023) and 256 possible outputs (0 to 255). Far faster then using the map function is to just divide the analogRead value by 4.

The difference in speed doesn't make any difference in your sketch, but it might in a different sketch. Just something to keep in mind.

any way to fix the flickering?

If I read this right, you are using 4 pots to control the color of one RGB LED. I'm not sure that I understand why. Most people do it with just 3.

I'd add some Serial.print statements and intermediate variables. Capture the pot readings in variables that are not the same ones used as the output from the map functions (none of which you really need). After setting all 4 led values, use Serial.print to see the values that were read from the pots.

My guess is that the values are not steady. They move up and down small amounts even when not being touched. At small values, the light output from the LED is not enough to fool the eye into thinking that the LED is on all the time. So, it appears to flicker.

The delay, in this sketch, is unnecessary, and contributing to the flicker.

If I read this right, you are using 4 pots to control the color of one RGB LED. I’m not sure that I understand why. Most people do it with just 3.

Cos he wants to have a max brightness pot. I wasn’t going to ask why though…

Mowcius

i dont understand… i removed the delay and still have flicker…