How to control RGB LED dimming for same Hue

Hello all,
Here I have managed to control LED Hue as well as LED saturation but now i would like to add LED brightness or say gamma correction, Here i am not using digital addressable RGB LED i am using just RGB led without any logic gates inside simple R-G-B (3 PWM) from my esp-8266 controller, Here i have done RGB to HSV and then again HSV to RGB to control and to extract brightness from HSV, But unable to manage it wisely , So kindly help me to solve this problem.
Here is my Code:

``````void RGBtoHSV(double r, double g, double b, double light){

Serial.println("r ="+String(r)+ "g ="+String(g)+ "b ="+String(b));

// R, G, B values are divided by 255
// to change the range from 0..255 to 0..1
r = r / 255.0;
g = g / 255.0;
b = b / 255.0;

// h, s, v = hue, saturation, value
double cmax = max(r, max(g, b)); // maximum of r, g, b
double cmin = min(r, min(g, b)); // minimum of r, g, b
double diff = cmax - cmin; // diff of cmax and cmin.
double h = -1, s = -1;

// if cmax and cmax are equal then h = 0
if (cmax == cmin)
h = 0;

// if cmax equal r then compute h
else if (cmax == r)
h = abs(((int)(60.0 * ((g - b) / diff) + 360) %360));

// if cmax equal g then compute h
else if (cmax == g)
h = abs(((int)(60.0 * ((b - r) / diff) + 120) %360));

// if cmax equal b then compute h
else if (cmax == b)
h = abs(((int)(60.0 * ((r - g) / diff) + 240) %360));

// if cmax equal zero
if (cmax == 0)
s = 0;
else
s = (diff / cmax) * 100;

// compute v
double v = cmax * 100;
Serial.println("(" + String(h) + " " + String(s) + " " + String(v) + ")");
HSV_to_RGB(h, s, light);

}

void HSV_to_RGB(double h, double s, double v)
{

double f,p,q,t;
int i,r,g,b;

// h = max((float)0.0, min((float)360.0, h));
// s = max((float)0.0, min((float)100.0, s));
// v = max((float)0.0, min((float)100.0, v));

s /= 100;
v /= 100;

if(s == 0) {
// Achromatic (grey)
r = g = b = abs(v*255);
return;
}

h /= 60; // sector 0 to 5
i = floor(h);
f = h - i; // factorial part of h
p = v * (1 - s);
q = v * (1 - s * f);
t = v * (1 - s * (1 - f));
switch(i) {
case 0:
r = abs(255*v);
g = abs(255*t);
b = abs(255*p);
break;
case 1:
r = abs(255*q);
g = abs(255*v);
b = abs(255*p);
break;
case 2:
r = abs(255*p);
g = abs(255*v);
b = abs(255*t);
break;
case 3:
r = abs(255*p);
g = abs(255*q);
b = abs(255*v);
break;
case 4:
r = abs(255*t);
g = abs(255*p);
b = abs(255*v);
break;
default: // case 5:
r = abs(255*v);
g = abs(255*p);
b = abs(255*q);
}
Serial.println("r ="+String(r)+ "g ="+String(g)+ "b ="+String(b));

analogWrite(redPin, r);
analogWrite(grnPin, g);
analogWrite(bluPin, b);
}
``````

You want us to help you do something "more wisely"? Can you be more specific?

I think you really need to explain the purpose of this exercise.

He wants to fade in/out without the color going askew in the process.

Yes, but why would you want to do that, and how would you want to control it?

All I see is an obscure and congested piece of code bizarrely using floats and such, quite inappropriate for 8-bit PWM if that is the intent, and no reference to fading - which requires recalculation of the values from the primitives and the new fade factor at each step, rather than attempting to calculate form the current values.

One has to ask just why you would think it necessary to go to all this trouble!

Respected Sir,
I would like to control those RGB LED's via my range slider and also onInput slider which will take active values and would like to fade in/out that same color without changes and sorry for that wisely thing and thank you for your help sir to all .
and yes i am curious to know about the RGB LED controlling i would like to control the way i wanted as a learner sir. I will share more details as i will proceed further with my project.
will update you soon i am controlling RGB LED with range slider on my app.
I think i have sent just a small code which is not precise for this thing. will share more soon sir.

Paul__B:
8-bit PWM

Hey, I think it defaults to 10-bit on esp8266... I wonder if the OP realises that...

It could be made 8-bit with

``````analogWriteRange(255)
``````

but it would be better to change the code above to use 10-bit.

That being my point - if you only have 8-bit PWM - and 10 bit not greatly different - all that convoluted code, particularly the use of floats, is just plain silly.

Yes i am using 8-bit and not using 10-bit by giving resolution of 256 just like you have suggested.
And yes little pain to use float.
So complexity i am facing is recollecting same RGB data which i am giving to this code in RGB to HSV part.

Not really what you want I think, but here is a tip you can use on esp8266 (but not on Uno/Nano/Mega etc)

``````Serial.println("r ="+String(r)+ "g ="+String(g)+ "b ="+String(b));
``````

can be

``````Serial.printf("r =%.2d g =%.2d b =%.2d\n", r, g, b);
``````

roboIOTnoob:
So complexity i am facing is recollecting same RGB data which i am giving to this code in RGB to HSV part.

I don't understand what you mean by that. Can you say that another way?

So RGB white is FFFFFF.

Basically:

What color is EEEEEE? 'white', what happens to the brightness of the led? gets dimmer.

What color is DDDDDD? what, what happens to the brightness?

If the value of R and G and B is reduced by an equal percentage, the color will be the same but the brightness will be reduced.

Wanna get 10% less bright for a color?

Here I produce the color pink with a varying degree of brightness:

``````leds.setPixelColor(position, leds.Color( value, 0, value * .91) ); // pink
``````

Here I produce orange with varying degree of brightness:

``````leds.setPixelColor( position, leds.Color( value, value * .3, 0) ); // orange
``````

By keeping the RGB values in proportion to each other, the color brightness can be changed.

Idahowalker:

``````leds.setPixelColor(position, leds.Color( value, 0, value * .91) ); // pink
``````

Here I produce orange with varying degree of brightness:

``````leds.setPixelColor( position, leds.Color( value, value * .3, 0) ); // orange
``````

By keeping the RGB values in proportion to each other, the color brightness can be changed.

Here i think this code uses library of NEOPixel and FASTLED but here i am not using that RGB LED i am using normal RGB LED which does not require NEOpixel i guess.
Thank you Sir yes as per you told that by varying percentages of these RGB at same level we can achieve brightness control so i will try to implement that way thank you.