Hi all.
After my most recent code writing session I thought I'd share it as it might come in handy to someone hopefully. As I'm a newby to this, if anyone has any suggestions on how to shorten, clean up, simplify, etc. the code, that would be appreciated.
What I wanted to do was fade a large amount of RGB LED's in a controlled fashion (brightness and duration from color to color). I'm not worried about using this as display signage or anything like that (no need to control rows, columns, make text, etc).
Using three PWM channels on the Arduino Uno I'm running three banks of RGB LED's in a 'flowing' fashion. My original plan was to use NPN transistors for control of each leg but 12 LED's at max draw are around a 50 milliamp draw. So I will likely just wire them straight to the output channel for the final revision. I don't have the resistors in the Fritzing drawing but I'm using 200 ohm resistors to each leg of each LED. There are three different color wires coming out of the Arduino, but they don't represent the LED color. Just a way to stay organized.
The basic idea is that each output from the Arduino will rise and fall in it's intensity. One potentiometer is used to control the max brightness and another to modify a delay to slow down the loop. Because the brightness is controlled by limiting the max PWM duty cycle, at minimum brightness all the values are the same for a short time so a separate value is used and then mapped against to ensure the cycle is maintained even if the max brightness goes down then comes back up again.
Thanks all.
// Integer declaration
int LoopCount = 0;
int sensorValueA0;
int sensorValueA1;
int Led3 = 3;
int Led9 = 9;
int Led10 = 10;
int brightness3 = 21;
int brightness9 = 137;
int brightness10 = 254;
int fadeAmount3 = 1;
int fadeAmount9 = 1;
int fadeAmount10 = 1;
int MaxBrightness3;
int MaxBrightness9;
int MaxBrightness10;
// Program Setup. Runs once
void setup() {}
// Looping Routine. Runs forever
void loop() {
// Checks for first Loop
if (LoopCount == 0) sensorValueA0 = analogRead(A0);
sensorValueA0 = map(sensorValueA0, 0, 1023, 0, 100);
// Checks for second loop
// Additional loops between reads for better performance
if (LoopCount == 4) sensorValueA1 = map(analogRead(A1), 0, 1023, 21, 254);
// Increases loop counter
LoopCount = LoopCount + 1;
// Resets loop counter
if (LoopCount == 8) LoopCount = 0;
brightness3 = brightness3 + fadeAmount3;
brightness9 = brightness9 + fadeAmount9;
brightness10 = brightness10 + fadeAmount10;
if (brightness3 == 20 || brightness3 == 255)
fadeAmount3 = -fadeAmount3 ;
if (brightness9 == 20 || brightness9 == 255)
fadeAmount9 = -fadeAmount9 ;
if (brightness10 == 20 || brightness10 == 255)
fadeAmount10 = -fadeAmount10 ;
MaxBrightness3 = map(brightness3, 20, 255, 20, sensorValueA1);
MaxBrightness9 = map(brightness9, 20, 255, 20, sensorValueA1);
MaxBrightness10 = map(brightness10, 20, 255, 20, sensorValueA1);
analogWrite(Led3, MaxBrightness3);
analogWrite(Led9, MaxBrightness9);
analogWrite(Led10, MaxBrightness10);
delay(sensorValueA0);
}