HSB to RGB help

Can anyone either give me a video source that explains how to program HSB to RGB? Or if someone is willing to help walk me through it?

I have a problem with word processing, kinda like dyslexia, except I can read just fine, but my brain cant hold on to the information. Ive gotten pretty far with arduinos using mainly video sources because thats how I learn the best. However I cannot find any videos that explain HSB to RGB for the life of me. And people I ask get frustrated because when they try to explain it through text, I have alot of difficulty understanding it and they think it should be easy to grasp.

So I present these options if someone is willing to help me:

-Send me a video on youtube or whatever that explains how to program HSB to RGB

-Msg me and we can set up a discord call or something and walk me through it

-or be patient and walk me through it, via msging/posting, like you're talking to a 5th grader that has an associates in science lol

If someone can do something like that for me, you would be a life saver!

There is some context from your other topic that might be useful to those providing assistance. Starting here:

Yes, as Pert was saying, there is more context I should provide:

I have an Arduino Nano and I'm working with an RGB Led Common Cathode, so there is no data pin. I'm trying to create a code that fades one color in brightness back and forth, say Cyan with a brightness going between 100-255 repeatedly.

However, once a switch is pulled, it smoothly transitions into a new color, say hot pink, and then fades in and out again.

I know if I use HSB, I could change the Hue when the switch is toggled, and leave the brightness the way it is. However, I cant figure out how to use HSB to RGB and I'm wanting to learn how to do this from scratch, rather than just copy and paste it; This way, I understand it better, and can apply it, or other attributes to other projects better.

Check out the FastLED library it supports both RGB and HSV color values. I believe there is some code in there somewhere to translate back and forth. Or just use the library!

Check out the FastLED library it supports both RGB and HSV color values. I believe there is some code in there somewhere to translate back and forth. Or just use the library!

From what I can tell, FastLED only works with LEDs that have a data pin. The LEDs I'm using have the four pins, red, ground, green, blue. I cant find anything in the code to let me set each color(R,G,B) to a designated pin on the board.

Do you mean the HSV color system? If so, start here.

Here is the code I tried to write today, I cant get it to work. I dont know what I’m doing wrong.

int B;
int G;
int R;

int RPin=6;
int GPin=5;
int BPin=3;


void setup() {
pinMode(RPin,OUTPUT);
pinMode(GPin,OUTPUT);
pinMode(BPin,OUTPUT);
Serial.begin(9600);
}

void loop() {

setColor(0,1,1);
analogWrite(RPin, R);
analogWrite(GPin, G);
analogWrite(BPin, B); 

}

void setColor(int H, int S, int B){
int C;
int V;
int M;
int X;

C=V*S;
X=C*(1-abs((H/60)%2-1));
M=V-C;

if(H>=0||H<60){
  R=(C+M)*255;
  G=(X+M)*255;
  B=(0+M)*255;
}
else if(60<=H<120){
  R=(X+M)*255;
  G=(C+M)*255;
  B=(0+M)*255;  
}
else if(120<=H<180){
  R=(0+M)*255;
  G=(C+M)*255;
  B=(X+M)*255;  
}
else if(180<=H<240){
  R=(0+M)*255;
  G=(X+M)*255;
  B=(C+M)*255;  
}
else if(240<=H<300){
  R=(X+M)*255;
  G=(0+M)*255;
  B=(C+M)*255;  
}
else if(300<=H<360){
  R=(C+M)*255;
  G=(0+M)*255;
  B=(X+M)*255;  
}
Serial.println(R);
delay(1000);
}

I have a Serial.println(R) at the end to see if anything is even being sent back as an R value and it says R is 0, no matter what I do.

The code is based off of this web page: HSV to RGB conversion | color conversion

MajesticLamb:
Here is the code I tried to write today, I cant get it to work. I dont know what I'm doing wrong.

You're trying to do real number calculation using integers.

You're trying to do real number calculation using integers.

So what do I need to change?

There are working examples of functions that do HSV to RGB conversion. Google will help you find them.

There are working examples of functions that do HSV to RGB conversion. Google will help you find them.

Well, if I found them, I wouldnt be on here, would I?

Sorry this is so hard for you! Take the first hit below:
https://www.google.com/search?q=c%2Fc%2B%2B+hsv+to+rgb+function

Sorry this is so hard for you!

Stop being an ass dude. I'm trying to learn how to program it from scratch. But you wouldnt know that because it seems you dont read as well as I do.

Then start by reading reply #7, and then do some more reading to learn the difference between floating point (real) and integer calculations.

jremington, stop replying to this thread, you are not helping.
Thanks

You are welcome.

Time wasting reports will not be tolerated too well.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

Bob.

int B;
int G;
int R;
int RPin=6;
int GPin=5;
int BPin=3;


void setup() {
pinMode(RPin,OUTPUT);
pinMode(GPin,OUTPUT);
pinMode(BPin,OUTPUT);
Serial.begin(9600);
}

void loop() {
analogWrite(RPin, R);
analogWrite(GPin, G);
analogWrite(BPin, B); 

setColor(60,1,1);

delay(1);
}

void setColor(int H, float S, float V){
//When 0 ≤ H < 360, 0 ≤ S ≤ 1 and 0 ≤ V ≤ 1:
float C;
float M;
float X;

C=V*S;
X=C*(1-abs((H/60)%2-1));
M=V-C;

if(H>=0&&H<60){
  R=(C+M)*255;
  G=(X+M)*255;
  B=(0+M)*255;
}
else if(H>=60&&H<120){
  R=(X+M)*255;
  G=(C+M)*255;
  B=(0+M)*255;  
}
else if(H>=120&&H<180){
  R=(0+M)*255;
  G=(C+M)*255;
  B=(X+M)*255;  
}
else if(H>=180&&H<240){
  R=(0+M)*255;
  G=(X+M)*255;
  B=(C+M)*255;  
}
else if(H>=240&&H<300){
  R=(X+M)*255;
  G=(0+M)*255;
  B=(C+M)*255;  
}
else if(H>=300&&H<360){
  R=(C+M)*255;
  G=(0+M)*255;
  B=(X+M)*255;  
}

}

Still cant get it to work. its better, but I’m getting frustrated

What is not working?

You calculate the same value each time through loop() since you have the same input each time through loop()

What is not working?

My bad, I should have specified. However, I tweeked the code a bit already.

int B;
int G;
int R;
int Y;

int RPin=6;
int GPin=5;
int BPin=3;


void setup() {
pinMode(RPin,OUTPUT);
pinMode(GPin,OUTPUT);
pinMode(BPin,OUTPUT);
Serial.begin(9600);
}

void loop() {
analogWrite(RPin, R);
analogWrite(GPin, G);
analogWrite(BPin, B);
setColor(180,1,.5);
delay(10);

}

void setColor(int H, float S, float V){
//When 0 ≤ H < 360, 0 ≤ S ≤ 1 and 0 ≤ V ≤ 1:
float C;
float M;
float X;

C=(1-abs(2*V-1))*S;
X=C*(1-abs((H/60)%2-1));
M=V-C/2;

if(H>=0&&H<60){
  R=(C+M)*255;
  G=(X+M)*255;
  B=(0+M)*255;
}
else if(H>=60&&H<120){
  R=(X+M)*255;
  G=(C+M)*255;
  B=(0+M)*255;  
}
else if(H>=120&&H<180){
  R=(0+M)*255;
  G=(C+M)*255;
  B=(X+M)*255;  
}
else if(H>=180&&H<240){
  R=(0+M)*255;
  G=(X+M)*255;
  B=(C+M)*255;  
}
else if(H>=240&&H<300){
  R=(X+M)*255;
  G=(0+M)*255;
  B=(C+M)*255;  
}
else if(H>=300&&H<360){
  R=(C+M)*255;
  G=(0+M)*255;
  B=(X+M)*255;  
}
Serial.print(R );
Serial.print(G );
Serial.print(B );
Serial.println();
delay(1000);
}

This is my current code. The only thing I changed were some of the equation at the beginning of setColor.

The colors seem to work some of the time. Cyan(180) comes out as cyan, Red(0) comes out as red. Yellow doesnt look yellow at all however, and I’m not sure if thats something wrong with the code or just my LED.

However, if I replace the Hue value with something like “Y” (setColor(Y,1,.5):wink: and make Y=Y+1;, the LED doesnt cycle through the colors like I’ve been able to do with the brightness. Instead the color stays Red and then after a few seconds with instantly change to green.

EDIT: I left the circuit on while I ate and saw it switch from red, to green, to cyan, to blue, then magenta. So it seems to be going through the colors, but has no intermediate colors; making it just snap to the next color.