Skip to content

Instantly share code, notes, and snippets.

@jimsynz
Created January 5, 2011 20:59
Show Gist options
  • Star 68 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save jimsynz/766994 to your computer and use it in GitHub Desktop.
Save jimsynz/766994 to your computer and use it in GitHub Desktop.
Arduino sketch to cycle an RGB LED through the colour spectrum.
const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;
void setup() {
// Start off with the LED off.
setColourRgb(0,0,0);
}
void loop() {
unsigned int rgbColour[3];
// Start off with red.
rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;
// Choose the colours to increment and decrement.
for (int decColour = 0; decColour < 3; decColour += 1) {
int incColour = decColour == 2 ? 0 : decColour + 1;
// cross-fade the two colours.
for(int i = 0; i < 255; i += 1) {
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
delay(5);
}
}
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
@dreamer1048576
Copy link

@jdimpson

Otherwise the LED doesn't ever hit a pure red output.

??? WHY ???

 for (int decColour = 0; decColour < 3; decColour += 1) {
 // When `decColour ==2`
   int incColour = decColour == 2 ? 0 : decColour + 1;
   //  `incColour = 0` at this moment 

After finish the i loop ,
rgbColour[0]=255; and rgbColour[1]=rgbColour[2]=0; ... back to pure red again...
right ?

@dreamer1048576
Copy link

Another kinds of colour cycle
it might ignore darker shade problem

but it's total different spectrum with original

void loop() {
  unsigned int rgbColour[3];

  // Start off with red.
  rgbColour[0] = 255;
  rgbColour[1] = 255;    //// Different place !
  rgbColour[2] = 0;  

  // Choose the colours to increment and decrement.
  for (int decColour = 0; decColour < 3; decColour += 1) {
    int incColour = (0==decColour ) ? 2 : decColour - 1;    //// Different place !

    // cross-fade the two colours.
    for(int i = 0; i < 255; i += 1) {
      rgbColour[decColour] -= 1;
      rgbColour[incColour] += 1;
      
      setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
      delay(5);
    }
  }
}

@dreamer1048576
Copy link

for states :
255, 0, 0
255, 255, 0
0, 255, 0
0, 255, 255
0, 0, 255
255, 0, 255
255, 0, 0

void loop() {
  unsigned int rgbColour[3];

  // Start off with red.
  rgbColour[0] = 255;
  rgbColour[1] = 0; 
  rgbColour[2] = 0;  
  int decColour =0;
  int incColour =1;

void loop() {
			if (255<=rgbColour[incColour]) {
				if (0>=rgbColour[decColour]) {
					decColour++;	decColour %= 3;
					incColour++;	incColour %= 3;
				} else 	// if (0==rgbColour[decColour]) {
					rgbColour[decColour] -= 15;  // --
			} else	// if (255==rgbColour[incColour]) {
				rgbColour[incColour] += 15;  // ++
      
        setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
        delay(5);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment