The following code is from adafruit to show as an example of controlling a single RGB led.
I have successfully used the same code to control a 5m length of RGB leds... but would like to know how to change it from the current change of colors to only Red-White-Blue colors pattern.
All and any comments welcomed.
// color swirl! connect an RGB LED to the PWM pins as indicated
// in the #defines
// public domain, enjoy! #define REDPIN 5 #define GREENPIN 6 #define BLUEPIN 3 #define FADESPEED 5 // make this higher to slow down
void setup() {
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
}
void loop() {
int r, g, b;
// fade from blue to violet
for (r = 0; r < 256; r++) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
// fade from violet to red
for (b = 255; b > 0; b--) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
// fade from red to yellow
for (g = 0; g < 256; g++) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
// fade from yellow to green
for (r = 255; r > 0; r--) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
// fade from green to teal
for (b = 0; b < 256; b++) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
// fade from teal to blue
for (g = 255; g > 0; g--) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
}
First off you should be placing code in code tags (Item 7 in this readme)
Each for-next loop is either fading a single colour up...
// fade from blue to violet
for (r = 0; r < 256; r++) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
or down...
// fade from violet to red
for (b = 255; b > 0; b--) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
You want to go from RED only to RED,GREEN & BLUE (white) to BLUE only and keep looping.
Starting with RED you will currently have BLUE illuminated (after first pass through loop) so you need to fade up RED and fade down BLUE at the same time. (As your code will need to both fade colours up and down at the same time we will use just one method to simplify things.)
for (r = 0; r < 256; r++) {
analogWrite(REDPIN, r); //Fade up RED
analogWrite(BLUEPIN, 255 - r); //Fade down BLUE
delay(FADESPEED);
}
the next loop will leave RED alone and fade up BLUE & GREEN to make white
for (r = 0; r < 256; r++) {
analogWrite(GREENPIN, r); //Fade up GREEN
analogWrite(BLUEPIN, r); //Fade up BLUE
delay(FADESPEED);
}
the final section will need to now fade down RED & GREEN leaving BLUE alone. This I will leave for you to do.