Programming some fading LEDs!

Hey guys, I need some help coding a project I'm working on.

I need a row of LEDs (let’s say 5) to fade up to full brightness one by one. I would like them to fade up slowely so that it takes about 2 seconds to reach full brightness. The problem is, I don’t want the next LED in line to begin fading on once the previous LED has finished, I would like the process to start slightly before that to achieve a more natural and not so staggered look. Let’s say the fade process for LED 2 begins as LED 1 reaches 10% and LED 3 begins when LED 2 reaches 10% and so on... all also attach a picture that will hopefully help depict what I'm trying to do. When the last LED reaches full brightness and stay there for 5 seconds I would like all LEDs to fade off. Any help would be appreciated!

 int LedPin1 = 27;
 int LedPin2 = 0;
 int LedPin3 = 1;
 int LedPin4 = 14;
 int LedPin5 = 15;
 

int DelayVal = 60;    // how long the fade takes





void setup()  { 
  //sets pins as outputs
  pinMode(LedPin1, OUTPUT);
  pinMode(LedPin2, OUTPUT);
  pinMode(LedPin3, OUTPUT);
  pinMode(LedPin4, OUTPUT);
  pinMode(LedPin5, OUTPUT);
} 

void loop()  { 
  
  for(int fadeValue = 0 ; fadeValue <= 25; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin1, fadeValue);         
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  for(int fadeValue = 26 ; fadeValue <= 50; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin1, fadeValue);  
    analogWrite(LedPin2, fadeValue-25);    
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  for(int fadeValue = 51 ; fadeValue <= 75; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin1, fadeValue);
    analogWrite(LedPin2, fadeValue-25);  
    analogWrite(LedPin3, fadeValue-50);     
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  for(int fadeValue = 76 ; fadeValue <= 100; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin1, fadeValue);
    analogWrite(LedPin2, fadeValue-25);  
    analogWrite(LedPin3, fadeValue-50);  
    analogWrite(LedPin4, fadeValue-75);    
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  for(int fadeValue = 101 ; fadeValue <= 125; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin1, fadeValue); 
    analogWrite(LedPin2, fadeValue-25);  
    analogWrite(LedPin3, fadeValue-50);  
    analogWrite(LedPin4, fadeValue-75);
    analogWrite(LedPin5, fadeValue-100);    
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  for(int fadeValue = 126 ; fadeValue <= 150; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin1, fadeValue); 
    analogWrite(LedPin2, fadeValue-25);  
    analogWrite(LedPin3, fadeValue-50);  
    analogWrite(LedPin4, fadeValue-75);
    analogWrite(LedPin5, fadeValue-100);   
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  for(int fadeValue = 151 ; fadeValue <= 175; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin1, fadeValue);   
    analogWrite(LedPin2, fadeValue-25);  
    analogWrite(LedPin3, fadeValue-50);  
    analogWrite(LedPin4, fadeValue-75);
    analogWrite(LedPin5, fadeValue-100);      
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  for(int fadeValue = 176 ; fadeValue <= 200; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin1, fadeValue); 
    analogWrite(LedPin2, fadeValue-25);  
    analogWrite(LedPin3, fadeValue-50);  
    analogWrite(LedPin4, fadeValue-75);
    analogWrite(LedPin5, fadeValue-100);      
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  for(int fadeValue = 201 ; fadeValue <= 225; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin1, fadeValue);   
    analogWrite(LedPin2, fadeValue-25);  
    analogWrite(LedPin3, fadeValue-50);  
    analogWrite(LedPin4, fadeValue-75);
    analogWrite(LedPin5, fadeValue-100);      
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  for(int fadeValue = 226 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin1, fadeValue);  
    analogWrite(LedPin2, fadeValue-25);  
    analogWrite(LedPin3, fadeValue-50);  
    analogWrite(LedPin4, fadeValue-75);
    analogWrite(LedPin5, fadeValue-100);      
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  for(int fadeValue = 226 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin2, fadeValue);  
    analogWrite(LedPin3, fadeValue-25);  
    analogWrite(LedPin4, fadeValue-50);  
    analogWrite(LedPin5, fadeValue-75);    
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  for(int fadeValue = 226 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin3, fadeValue);  
    analogWrite(LedPin4, fadeValue-25);  
    analogWrite(LedPin5, fadeValue-50);  
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  for(int fadeValue = 226 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin4, fadeValue);  
    analogWrite(LedPin5, fadeValue-25);  
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 

  for(int fadeValue = 226 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin5, fadeValue);  
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 

  
  
    for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255) fading in
    analogWrite(LedPin1, fadeValue);  
    analogWrite(LedPin2, fadeValue);  
    analogWrite(LedPin3, fadeValue);  
    analogWrite(LedPin4, fadeValue);
    analogWrite(LedPin5, fadeValue);      
    // wait for x milliseconds to see the dimming effect    
    delay(DelayVal);                            
  } 
  
}

Different way of skinning the same cat. This is based on an UNO, with PWM on pins 3, 5, 6, 9, and 10:

byte myPins[] = {3, 5, 6, 9, 10};
byte myValues[] = {0, 0, 0, 0, 0};
int p = 0;

void setup() {
  for (int pin = 0; pin < sizeof(myPins); pin++) {
    pinMode(myPins[pin], OUTPUT);
  }
}

void loop() {
  myValues[p]++;
  if (myValues[p] > 25) {
    p++;
    if (p > 4) p = 0;
  }
  analogWrite(myPins[p], myValues[p]);
  delay(2);
  if (myValues[p] == 255) {
    // We reached full intensity on all LEDs, pause for 5 seconds
    delay(5000);
    while (myValues[p] > 0) {
      myValues[p]--;
      for (int pin = 0; pin < sizeof(myPins); pin++) {
        analogWrite(myPins[pin], myValues[p]);
        delay(1);
      }
    }
    // All LEDs are off, reset values array
    for (int p = 0; p <sizeof(myPins); p++) {
      myValues[p] = 0;
    }
    // Wait 5 seconds before looping back
    delay(5000);
  }
}

Thank you for your post, it seems to be working but Im having 2 small issues:

  1. For some reason there seems to be a good deal of flickering on each LED as they fade up. I've only got one LED per pin so I'm not sure why this is happeneing. I really need this to be as smooth as possible (its for an art project). I did a project once using sine waves to allow LEDs to fade on pins that arent PWM and it was very smooth, maybe something like that would be better?

  2. All the LEDs fade out at the end but I really need them to stay on.

Any help with these would be appreaciated!

Have a look at my blinkenlight experiments. Especially at Knight Rider | Blinkenlight and then to get rid of the flicker: Removing Flicker | Blinkenlight

jeffd:

  1. For some reason there seems to be a good deal of flickering on each LED as they fade up. I've only got one LED per pin so I'm not sure why this is happeneing. I really need this to be as smooth as possible (its for an art project). I did a project once using sine waves to allow LEDs to fade on pins that arent PWM and it was very smooth, maybe something like that would be better?

You don't say which code you're running. When I run mine, I have zero flicker. It's a smooth transition both ways.

jeffd:
2) All the LEDs fade out at the end but I really need them to stay on.

That's not what you said in your original post:

jeffd:
When the last LED reaches full brightness and stay there for 5 seconds I would like all LEDs to fade off.