Pages: [1]   Go Down
Author Topic: Turn of LEDs after 10 seconds - Code mod  (Read 364 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Pages: [1]   Go Up
Jump to: