Skip to content

Instantly share code, notes, and snippets.

@dahngeek
Forked from jimsynz/rgb_spectrum.c
Last active August 29, 2015 14:27
Show Gist options
  • Save dahngeek/b84b71f4f79a1fb7b04b to your computer and use it in GitHub Desktop.
Save dahngeek/b84b71f4f79a1fb7b04b to your computer and use it in GitHub Desktop.
Arduino sketch to cycle an RGB LED through the colour spectrum.
const int redPin = 8;
const int greenPin = 9;
const int bluePin = 10;
#define LDR 0
void setup() {
// Start off with the LED off.
setColourRgb(0,0,0);
Serial.begin(9600);
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 loop() {
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
Serial.print("rgb(");
Serial.print( red );
Serial.print(",");
Serial.print( green );
Serial.print(",");
Serial.print( blue );
Serial.print(") -");
Serial.print( analogRead( LDR ) );
Serial.print("-");
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment