int pin[] = {3, 5, 6}; // r, g, b array of PWM pins
int stage = 0, stages = 6; // stages of each color
int minimum = 0, maximum = 255, increas, decreas; // counters - misspelled for word length
unsigned long fadeTimer, fadeTimeout = 3900; // microseconds = 3.9 milliseconds
void setup() {
for (int i = 0; i < 3; i++ ) {
pinMode(pin[i], OUTPUT); // configure r, g, b pins to OUTPUT
}
}
void loop() {
if (micros() - fadeTimer > fadeTimeout) {
fadeTimer = micros();
fade();
}
}
void fade() {
if (increas++ == maximum) { // increment step until counter is max
increas = minimum; // reset step counter
if (stage++ == stages) // increment stage until stage is max
stage = 1; // reset stage
}
decreas = maximum - increas; // invert the counter to decrease values
switch (stage) {
case 0: setColor(increas, minimum, minimum); break; // blk (0,0,0) to red (1,0,0) - blk one time only
case 1: setColor(maximum, increas, minimum); break; // red (1,0,0) to yel (1,1,0)
case 2: setColor(decreas, maximum, minimum); break; // yel (1,1,0) to grn (0,1,0)
case 3: setColor(minimum, maximum, increas); break; // grn (0,1,0) to cyn (0,1,1)
case 4: setColor(minimum, decreas, maximum); break; // cyn (0,1,1) to blu (0,0,1)
case 5: setColor(increas, minimum, maximum); break; // blu (0,0,1) to mag (1,0,1)
case 6: setColor(maximum, minimum, decreas); break; // mag (1,0,1) to red (1,0,0)
default: break;
}
}
void setColor(int val1, int val2, int val3) {
analogWrite(pin[0], val1);
analogWrite(pin[1], val2);
analogWrite(pin[2], val3);
}