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
int oldred, oldgrn, oldblu, r, g, b;
unsigned long fadeTimer, fadeTimeout = 3900; // microseconds = 3.9 milliseconds
void setup() {
Serial.begin(115200);
for (int i = 0; i < 3; i++ ) {
pinMode(pin[i], OUTPUT); // configure r, g, b pins to OUTPUT
}
}
void loop() {
trim();
// fade();
}
void trim() {
int brightness = 1;
int r = map(analogRead(A0), 0, 1023, 255, 0);
int g = map(analogRead(A1), 0, 1023, 255, 0);
int b = map(analogRead(A2), 0, 1023, 255, 0);
r /= brightness;
g /= brightness;
b /= brightness;
setColor(r, g, b);
if (r != oldred || g != oldgrn || b != oldblu) {
Serial.print("red "); pad (r); Serial.print(r);
Serial.print(" grn "); pad (g); Serial.print(g);
Serial.print(" blu "); pad (b); Serial.print(b);
Serial.println();
}
oldred = r; oldgrn = g; oldblu = b;
}
void fade() {
if (micros() - fadeTimer > fadeTimeout) {
fadeTimer = micros();
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);
}
void pad (int val) {
if (val < 100) Serial.print(" ");
if (val < 10) Serial.print(" ");
}
red
grn
blu