I am trying to control 6xLEDs from analog pins 3 to 11 which fade up and down at different rates. I have an existing code which works fine, however it is very long winded. I have attempted to condense the code using an array for fadeAmount and a function when declaring the 6x output pins.
The code will upload (to Arduino Uno) without any errors and begin the phasing up/down, however it will stop running after about 10 seconds with some LEDs left on and some off. I cannot work out why it is doing this, but I expect there is an error in my logic somewhere.
I have included the original (working) code and the condensed code below.
Any help/suggestions would be much appreciated.
EXISTING CODE:
int led_1 = 9;
int led_2 = 10;
int led_3 = 11;
int led_4 = 6;
int led_5 = 5;
int led_6 = 3;
int brightness_1 = 0;
int brightness_2 = 100;
int brightness_3 = 200;
int brightness_4 = 150;
int brightness_5 = 250;
int brightness_6 = 50;
int fadeAmount_1 = 2; // varying fadeAmount
int fadeAmount_2 = 5;
int fadeAmount_3 = 6;
int fadeAmount_4 = 3;
int fadeAmount_5 = 4;
int fadeAmount_6 = 3;
void setup() {
pinMode(led_1, OUTPUT);
pinMode(led_2, OUTPUT);
pinMode(led_3, OUTPUT);
pinMode(led_4, OUTPUT);
pinMode(led_5, OUTPUT);
pinMode(led_6, OUTPUT);
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pins
analogWrite(led_1, brightness_1);
analogWrite(led_2, brightness_2);
analogWrite(led_3, brightness_3);
analogWrite(led_4, brightness_4);
analogWrite(led_5, brightness_5);
analogWrite(led_6, brightness_6);
// change the brightness for next time through the loop:
brightness_1 = brightness_1 + fadeAmount_1;
brightness_2 = brightness_2 + fadeAmount_2;
brightness_3 = brightness_3 + fadeAmount_3;
brightness_4 = brightness_4 + fadeAmount_4;
brightness_5 = brightness_5 + fadeAmount_5;
brightness_6 = brigtness_6 + fadeAmount_6;
// reverse the direction of the fading at the ends of the fade:
if (brightness_1 == 0 || brightness_1 == 255) {
fadeAmount_1 = -fadeAmount_1 ;
}else if (brightness_2 == 0 || brightness_2 == 255){
fadeAmount_2 = -fadeAmount_2;
}else if (brightness_3 == 0 || brightness_3 == 255){
fadeAmount_3 = -fadeAmount_3;
}else if (brightness_4 == 0 || brightness_4 == 255){
fadeAmount_4 = -fadeAMount_4;
}else if (brightness_5 == 0 || brightness_5 == 255){
fadeAmount_5 = -fadeAmount_5;
}else if (brightness_6 == 0 || brightness_6 == 255){
fadeAmount_6 = -fadeAMount_6
}
delay(50);
}
int fadeArray[]={2,5,6,3,4,3};
int Index = 1;
int led = 3;
int brightness = 100;
void setup(){
for (int x = 3;x%11;x++){
pinMode(x, OUTPUT);
}
}
void loop(){
for(int x=3;x%11;x++);
analogWrite(led, brightness);{
led++;
}
brightness = brightness + fadeArray[Index];{
Index ++;
}
if (brightness == 0 || brightness == 255){
fadeArray[Index] = -fadeArray[Index];
Index ++;
}
delay(50);
}