How to fade in and out multiple Leds

With code below I am able to fade in and out a row of Leds. At this moment the second led starts when the first led is totally faded out. I want to start the second led to fade in when the first led is at the ‘top’ of its power. Does anyone know how to do this?

int Pin5 = 5;       // Led 1
int Pin3 = 3;       // Led 5 
int Pin9 = 9;       // Led 2
int Pin10 = 10;       // Led 3
int Pin11 = 11;       // Led 4      (the 'wave' goes Led: 1-2-3-4-5)

int Value5 = 0;    //Value Led1
int Value3 = 0;    //Value Led5
int Value9 = 0;    //Value Led2
int Value10 = 0;    //Value Led3
int Value11 = 0;    //Value Led4
         
int analog0Pin = 0;      // button to start wave
int val0 = 0;             // variable to store the value read

int threshold = 512;     // threshold for button
int timer = 100;

void setup()  
{
  
  pinMode(analog0Pin, INPUT);
  pinMode(Pin5, OUTPUT);   // sets the digital pin as output
  pinMode(Pin3, OUTPUT);   // sets the digital pin as output
  pinMode(Pin9, OUTPUT);   // sets the digital pin as output
  pinMode(Pin10, OUTPUT);   // sets the digital pin as output
  pinMode(Pin11, OUTPUT);   // sets the digital pin as output      
  
  Serial.begin(9600);          //  setup serial

}


void loop()
{ 
  val0 = analogRead(analog0Pin);    // read the input pin for button
  Serial.println(val0);             // debug value

 if (val0 >= threshold)                // 
 
 { 
  for(Value5 = 0 ; Value5 <= 255; Value5+=5) // fade in (from min to max) Led 1
  { 
    analogWrite(Pin5, Value5);           // sets the value (range from 0 to 255) 
    delay(30);                            // waits for 30 milli seconds to see the dimming effect 
  } 
  for(Value5 = 255; Value5 >=0; Value5-=5)   // fade out (from max to min) 
  { 
    analogWrite(Pin5, Value5); 
    delay(30); 
      } 
  
  
   if (Value5 = 255)                //
 
 { 
  for(Value9 = 0 ; Value9 <= 255; Value9+=5) // fade in (from min to max) Led 2
  { 
    analogWrite(Pin9, Value9);           // sets the value (range from 0 to 255) 
    delay(30);                            // waits for 30 milli seconds to see the dimming effect 
  } 
  for(Value9 = 255; Value9 >=0; Value9-=5)   // fade out (from max to min) 
  { 
    analogWrite(Pin9, Value9); 
    delay(30); 
      } 
  
      
      if (Value9 = 255)                // 
 
 { 
  for(Value10 = 0 ; Value10 <= 255; Value10+=5) // fade in (from min to max) Led 3
  { 
    analogWrite(Pin10, Value10);           // sets the value (range from 0 to 255) 
    delay(30);                            // waits for 30 milli seconds to see the dimming effect 
  } 
  for(Value10 = 255; Value10 >=0; Value10-=5)   // fade out (from max to min) 
  { 
    analogWrite(Pin10, Value10); 
    delay(30); 
      } 
  
 }
 
      if (Value10 = 255)                // 
 
 { 
  for(Value11 = 0 ; Value11 <= 255; Value11+=5) // fade in (from min to max) Led 4
  { 
    analogWrite(Pin11, Value11);           // sets the value (range from 0 to 255) 
    delay(30);                            // waits for 30 milli seconds to see the dimming effect 
  } 
  for(Value11 = 255; Value11 >=0; Value11-=5)   // fade out (from max to min) 
  { 
    analogWrite(Pin11, Value11); 
    delay(30); 
      } 
  
 }
 
       if (Value11 = 255)                // 
 
 { 
  for(Value3 = 0 ; Value3 <= 255; Value3+=5) // fade in (from min to max) Led 5
  { 
    analogWrite(Pin3, Value3);           // sets the value (range from 0 to 255) 
    delay(30);                            // waits for 30 milli seconds to see the dimming effect 
  } 
  for(Value3 = 255; Value3 >=0; Value3-=5)   // fade out (from max to min) 
  { 
    analogWrite(Pin3, Value3); 
    delay(30); 
        } 
  
      }
 
    }

  }
  
}

Here's an algorithm that should achieve what you want. This assumes 1) you want to cycle through each LED exactly once, and 2) each LED (n) will start glowing when LED(n-1) is at its maximum brightness, at which point LED(n-1) will start fading back to complete darkness. All LEDs start completely dark.

0. Set current to first pin. Initialise array of brightness values to all zeros.
1. If current is greater than max pin, stop.
2. Increase brightness of current pin.
3. If current pin is not first pin:
     decrease brightness of previous pin if needed.
4. Set all pins to their brightness levels.
5. If brightness of current is max brightness:
     set current to next pin
6. Goto 1.

It should prove fairly simple to convert this to perform a continuous wave of fading/ glowing.

And here's some pseudo-code which might actually be valid python. Implementation of the pin-setting is left as an exercise.

# Algo step 0
current = 0
brightness = [0] * 5
max_brightness = 255

def loop():
  global current, brightness

  # Algo step 1
  if current == len(brightness):
    return   # Terminal condition has been reached.
  
  # Algo step 2
  brightness[current] += 1
  
  # Algo step 3
  if current > 0:
    if brightness[current-1] > 0:
      brightness[current-1] -= 1

  # Algo step 4.
  # Set your pins here.

  # Algo step 5
  if brightness[current] == max_brightness:
    current += 1


# Call loop() here repeatedly until "finished".

Here’s another way to think about it. You can create a function brightness(t) that says how bright the LED is at time T:

unsigned char brightness (unsigned char time)
{
  if (time < 128)
    return time*2;   // f(t) = 2t for first half of possible time values.
  return 2*(255 - time);  // this will make a "triangle" wave, I think.
}

So that’s a nice periodic function that’ll repeat getting brighter and then dimmer, over and over again, if you feed it something like a truncated (to “unsigned char”) version millis(). You can make this function as complex as you want, even using floating point (real sine waves!) But simple is probably good…

Your other LEDs display the same function, but at different phase shifts from the main sequence:

  led1_brightness = brightness(t);
  led2_brightness = brightness(t+phase);
  led3_brightness = brightness(t + 2*phase);
  // then do the analogWrite()s...

hi,

you might want to have a look at my christmas lights code, they fade in and out randomly, at different speeds

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1196702780/0#0

regards, kurt