Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// 2009 Kenneth Finnegan
// kennethfinnegan.blogspot.com
// Pins. Must be PWM pins.
#define REDLED 9
#define BLUELED 10
#define GREENLED 11
// Can't be higher than 255
#define MAXBRIGHT 200
// s[tate] t[arget] colors
byte s[3] = {0}, t[3] = {0};
void setup() {
// Make the random MORE RANDOM!
randomSeed(analogRead(0));
// Initialization wiring check
// You should see RED, BLUE, then GREEN.
// If you don't, then you ballsed something up.
analogWrite(REDLED, MAXBRIGHT);
delay(1000);
analogWrite(REDLED, 0);
analogWrite(BLUELED, MAXBRIGHT);
delay(1000);
analogWrite(BLUELED, 0);
analogWrite(GREENLED, MAXBRIGHT);
delay(1000);
analogWrite(GREENLED, 0);
}
void loop() {
byte i, offset;
// Select the next target color
// Start from a random one of the three colors to prevent
// the cycle from being red biased.
offset = random(3);
t[offset] = random(MAXBRIGHT);
t[(offset+1)%3] = random(MAXBRIGHT - t[offset]);
t[(offset+2)%3] = MAXBRIGHT - t[offset] - t[(offset+1)%3];
// Slowly drift to the new target color 1 at a time until
// it has been reached
while(s[0]!=t[0] || s[1]!=t[1] || s[2] != t[2]) {
for (i = 0; i<3; i++) {
if (s[i] > t[i]) {
s[i] = s[i] - 1;
} else if (s[i] < t[i]) {
s[i] = s[i] + 1;
}
analogWrite(REDLED, s[0]);
analogWrite(BLUELED, s[1]);
analogWrite(GREENLED, s[2]);
delay(10);
}
}
analogWrite(REDLED, s[0]);
analogWrite(BLUELED, s[1]);
analogWrite(GREENLED, s[2]);
// Let the viewer enjoy the new color before
// selecting the next target color.
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment