Help Needed, Problems by programming

I spent a few hours bz looking for a code that will turn on the rgb using html colors
I found this on adafruit :

int redPin = 2;
int greenPin = 3;
int bluePin = 4;

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

void loop()
{
String color = "4b0088";
setColor(0x4b , 0x00 , 0x88);
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

As you can see there is ¨stringcolor¨and i want that arduino will read this stringcolor for example ¨4B0088¨and break it into pieces ant those piceces should be seted in the line under like 0x4B 0x00 0x88
I am begginer by programming but i would like to make my apprenticeship in this content
Thx for help

char color[] = "4b0088";
long val = strtol(color, NULL, 16):
byte r, g, b;
r = val >> 16;
g = val >> 8;
b = val;

Not tested. >>16 is the same as divide by 65536. The further trick is that the variables are bytes and hence the result will be truncated.

Acctually it does not work for me.. the code looks like this
int redPin = 2;
int greenPin = 3;
int bluePin = 4;

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

void loop()
{
char color[] = "4b0088";
long val = strtol(color, NULL, 16);
byte r, g, b;
r = val >> 16;
g = val >> 8;
b = val;
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
but the led is not turned on... Maybe i made a mistake, it can be possible, as i said, i im using arduino since yesterday morning

Maybe it does not work for me because i am not using the original arduino... Rf duino is the platform i use... I got it from my employer to learn how to programm
Some of codes made for arduino dont work for me

You don't call setColor() after settling r, g and b.

it is working perfectly right now, thank you :slight_smile:

Dym3k99:
it is working perfectly right now, thank you :slight_smile:

Pleasure.

Do you understand what it is doing?

Actually, no :frowning:
And I got new ex... I have to do the same, but something with serial... I dont understand what do they mean

I dont understand what do they mean

It means that they now expect you to read the color data from the serial port.

Fortunately, other people have already been down that path.
Serial input basics - updated