LED fading sketch explanation?

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. :stuck_out_tongue: 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! :slight_smile:

First, does the code below do anything?

it is an assignment and it gives the variable DISPLAY_TIME the value 100.
Probably it is meant as a const as it is written in capitals (typical C style)
const int DISPLAY_TIME = 100; would be better,
However the variable is nowhere used (probably intended to be in the inner loop 0..768)

Second, how does showRGB(x); tells the arduino to light the LED? Is 'x' the same as 'int colour'?

This is a function call. The processor will execute the showRGB() code and after that is doen it will execute the next statement.
The showRGB is called with the parameter x, from which the value will be assignet to the variable colour of the function showRGB().
So it is a copy of the variable.

And lastly, how does the redIntensity, greenIntensity and blueIntensity work if they have not been defined? (Or have they??)

They are calculated from the variable colour.

Advice: buy a standard C++ book to get basic understanding of the C/C++ language.
Or go through the arduino tutorial section to get the basics right