Hi! I'm new the this Arduino programming so I hope that you can help me understand certain parts of the code. The full sketch is below and it works but I do not understand how.
This sketch makes an LED fades between red, green and blue.
const int R = 9;
const int G = 10;
const int B = 11;
int DISPLAY_TIME = 100;
void setup(){
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
}
void loop(){
showSpectrum();
}
void showSpectrum(){
int x;
for(x = 0; x < 768; x++)
{showRGB(x);
delay(10);}
}
void showRGB(int colour){
int redIntensity;
int greenIntensity;
int blueIntensity;
if (colour <= 255)
{
redIntensity = 255 - colour;
greenIntensity = colour;
blueIntensity = 0;
}
else if (colour <= 511)
{
redIntensity = 0;
greenIntensity = 255 - (colour - 256);
blueIntensity = (colour -256);
}
else
{
redIntensity = (colour - 512);
greenIntensity = 0;
blueIntensity = 255 - (colour - 512);
}
analogWrite(R, redIntensity);
analogWrite(G, greenIntensity);
analogWrite(B, blueIntensity);
}
First, does the code below do anything?
int DISPLAY_TIME = 100;
Second, how does showRGB(x); tells the arduino to light the LED? Is 'x' the same as 'int colour'?
{showRGB(x);
delay(10);}
And lastly, how does the redIntensity, greenIntensity and blueIntensity work if they have not been defined? (Or have they??)
void showRGB(int colour){
int redIntensity;
int greenIntensity;
int blueIntensity;
if (colour <= 255)
{
redIntensity = 255 - colour;
greenIntensity = colour;
blueIntensity = 0;
}
else if (colour <= 511)
{
redIntensity = 0;
greenIntensity = 255 - (colour - 256);
blueIntensity = (colour -256);
}
else
{
redIntensity = (colour - 512);
greenIntensity = 0;
blueIntensity = 255 - (colour - 512);
}
Please help me clarify my doubts. Thanks! ![]()