This is a bit of code written to fade up LEDs one by one and then all fade down together. Could someone help me modify this code to make all the LEDs turn off (no fade) 10 seconds after the last LED fades on?
int leds[] = {13,11,10,9,6,5,3};
int num = 7;
void init_leds(){
for(int i = 0; i<num; i++){
digitalWrite(leds[i],LOW);
pinMode(leds[i], OUTPUT);
delay(200);
}
}
void FadeOutLeds(){
for(int i = 155;i>0;i--){
for(int j = 0;j<num;j++){
analogWrite(leds[j],i);
}
delay(80);
}
}
void FadeInLed(int theLed){
for(int i = 0;i<256;i++){
analogWrite(theLed,i);
delay(20);
}
}
void setup() {
// initialize the digital pins as an output.
init_leds();
}
// the loop routine runs over and over again forever:
void loop() {
for(int i = 0; i<num;i++){
FadeInLed(leds[i]);
}
delay(5000); // wait for 5 seconds
FadeOutLeds();
delay(5000);
}
It was an unhelpful answer, and not one that I think would necessarily solve the problem. Although you probably should take a look at the alternative to delay as suggested, as it offers a few advantages in the long run.
I think that simply removing the outer for loop from FadeOutLeds and setting the analogWrite value to 0 should work. You might also want to try tuning the 80ms delay after each turn-off to get it as low as possible. I can make 50ms for transitions on mine before things go bork.
for(int j = 0;j<num;j++){
analogWrite(leds[j],0); //write value of '0' for brightness(?)
}
delay(80); //play with this, low as possible for faster 'off'.
You're likely to see them cycle off sequentially rather than all instantaneously flip off together with that though.