int pins[] = {11, 10, 9}; // an array of pins! This is similar to int pin0 = 11; int pin1=10; int pin2=9;
long values[3]; // make an array of values but don't give them a value just yet
long current_values[3]; // make an array of current values, but don't give them a value yet
int short_delay; // time between transition
void setup(){
randomSeed(analogRead(0)); // get some unpredictable value to start off our random number generator
// otherwise, we'd get the same random numbers each time (boring!)
for (int i=0; i <3; i++) { // pins 0 to less than 3
values[i] = random(255);// pick a random number between 0 and 255 for a pin (red, green, or blue)
current_values[i] = values[i]; // make our current value the same
analogWrite(pins[i], current_values[i]); // set the LED to our initial choice
values[i] = random(255); // pick a new random number for our next color
}
}
void loop(){
short_delay = random(3, 9);
for (int i=0; i <3; i++) {
if (values[i] > current_values[i]){ // if our new color is a larger number than our current color ...
current_values[i]++; // get just a little bit closer to the new color by adding 1
analogWrite(pins[i], current_values[i]); // set the LED to the new current color
delay(short_delay); // wait a little bit
}
if (values[i] < current_values[i]){ // if our new color is a smaller number than our current color ...
current_values[i]--; // get just a little bit closer to the new color by subtracting 1
analogWrite(pins[i], current_values[i]); // set the LED to the new current color
delay(short_delay); // wait a little bit
}
if (values[i] == current_values[i]){ // if our new color and our current color are the same ...
analogWrite(pins[i], current_values[i]); // make sure the LED is set to the new color
values[i] = random(255); // pick a new color
}
}
}