Go Down

Topic: Turn of LEDs after 10 seconds - Code mod (Read 380 times) previous topic - next topic

jeffd

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?

Code: [Select]

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);  
}

PaulS

Quote
Could someone help me modify this code to make all the LEDs turn off (no fade) 10 seconds after the last LED fades on?

With all those delay()s? No. You need to study, understand, and embrace the blink without delay example. Then, you'll KNOW how to do what you want.

jeffd

Hey now... the delays are necessary. The lights are timed to a song that will be played through the waveshield. They need to stay.

goobering

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.

Code: [Select]

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.

Go Up