Hello everyone, I’m trying to create an lighting effect with my arduino but I’m running into a bit of a problem. I have 13 LEDs hooked up in a row on my breadboard, connected to pins 1-13. The first led in the row is hooked to pin 11, and the last led is hooked to pin 2, which I understand to both be PWM pins. The effect I’m going for is for the first LED (hooked to pin 11) to start out at it’s brightest, fade a bit, then the leds between the first and the last to turn on and off in order until they reach the last LED which brightens a bit, and so on, and so on until the first LED is off, and the last LED is at it’s brightest, then it reverses going from last to first.
Whats happening instead is it works one way (from first LED to last), then when the order reverses the first LED just blinks on and off instead of fading up its brightness.
I’m new to C++ programming, so I’m wondering if I’ve messed up something in my code. Can someone take a look and let me know if they see anything that would cause this behavior?
int leds[]= {13, 12, 10, 9, 8, 7, 6, 5, 4, 2, 1};
int digLeds = 11;
int timer = 30;
int fadeLEDfront = 11;
int fadeLEDback = 3;
void setup() {
for(int j = 0; j < digLeds; j++){
pinMode(leds[j], OUTPUT);
}
analogWrite(fadeLEDfront, 255);
analogWrite(fadeLEDback, 0);
}
void loop() {
// LEDs pulse front to back
for( int fade = 15; fade <= 255; fade += 15 ){
analogWrite(fadeLEDfront, 255 - fade);
for( int i = 0; i < digLeds; i++){
digitalWrite(leds[i], HIGH);
delay(timer);
digitalWrite(leds[i - 1], LOW);
delay(timer);
}
analogWrite(fadeLEDback, fade);
digitalWrite(leds[digLeds - 1], LOW);
delay(timer*3);
}
randomLightShow();
//LEDs pulse back to front
for( int fade2 = 15; fade2 <= 255; fade2 += 15 ){
analogWrite(fadeLEDback, 255 - fade2);
for( int i = digLeds; i >= 0; i--){
digitalWrite(leds[i], HIGH);
delay(timer);
digitalWrite(leds[i + 1], LOW);
delay(timer);
}
analogWrite(fadeLEDfront, fade2);
digitalWrite(leds[0], LOW);
delay(timer*3);
}
randomLightShow();
ledsOff();
}
void ledsOff(){
for( int n = 0; n < digLeds; n++ ){
digitalWrite(leds[n], LOW);
}
}
//lights up LEDs randomly for random length of time
void randomLightShow(){
int ranCycles = random(10,50);
for( int r = 0; r < ranCycles; r++ ){
int ranLength = random(1, digLeds);
int ranArr[ranLength];
ledsOff();
//pick random LEDs
for(int b = 0; b < ranLength -1; b++ ){
int ranLED = random(0, digLeds);
ranArr[b] = leds[ranLED];
}
for (int j = 0; j < ranLength -1; j++) {
digitalWrite(ranArr[j], HIGH);
}
delay(timer);
ledsOff();
}
}