AVRdudes:
I uploaded you sketch and it only stays on one colour, a pale green, it incrementally gets brighter then stays bright. This has opened my mind to other options for documentation, thank you. It seems this is a difficult project.
There was one bug in the second while loop; it should be
while (brRed > endRed || brGreen > endGreen || brBlue > endBlue)
{
}
Not sure how it affected the code.
The other issue with the code is that it e.g. counts red up to 100, green to 50 and blue to 100. Next it assigns new endvalues for the decrement; if one or more of those new values are higher than the current values, the decrement for the specific color does nothing. Similar for the increment (if the new values are lower then the current ones).
The below code contains a modified loop (fixed the bug) and displays the RGB values (both current value and end value, the latter between brackets) on the serial port to make clear what happens. I hope it helps to understand. I have also renamed pin1, pin2 and pin3 to more sensible names.
int pinRed = 9;
int pinGreen = 10;
int pinBlue = 11;
void setup() {
pinMode(pinRed, OUTPUT);
pinMode(pinGreen, OUTPUT);
pinMode(pinBlue, OUTPUT);
Serial.begin(9600);
}
// desired brightness for the colors
int endRed = 0;
int endGreen = 0;
int endBlue = 0;
// current brightness of the colors
int brRed = 0;
int brGreen = 0;
int brBlue = 0;
void loop()
{
char buffer[50];
// assign an endvalue for the three colors
endRed = random(0, 255);
endGreen = random(0, 255);
endBlue = random(0, 255);
// while the endvalues are not reached, increase brightness of each color
while (brRed < endRed || brGreen < endGreen || brBlue < endBlue)
{
if (brRed < endRed) brRed++;
if (brGreen < endGreen) brGreen++;
if (brBlue < endBlue) brBlue++;
analogWrite(pinRed, brRed);
analogWrite(pinGreen, brGreen);
analogWrite(pinBlue, brBlue);
snprintf(buffer, sizeof(buffer), "inc: r = %03d (%03d), g = %03d (%03d), b = %03d (%03d)", brRed, endRed, brGreen, endGreen, brBlue, endBlue);
Serial.println(buffer);
delay(250);
}
// assign a new endvalue for the three colors
endRed = random(0, 255);
endGreen = random(0, 255);
endBlue = random(0, 255);
// while the endvalues are not reached, decrease brightness of each color
while (brRed > endRed || brGreen > endGreen || brBlue > endBlue)
{
if (brRed > endRed) brRed--;
if (brGreen > endGreen) brGreen--;
if (brBlue > endBlue) brBlue--;
analogWrite(pinRed, brRed);
analogWrite(pinGreen, brGreen);
analogWrite(pinBlue, brBlue);
snprintf(buffer, sizeof(buffer), "dec: r = %03d (%03d), g = %03d (%03d), b = %03d (%03d)", brRed, endRed, brGreen, endGreen, brBlue, endBlue);
Serial.println(buffer);
delay(250);
}
}