Cross Fade LED's

What I’m Trying To Do: Have two LED’s fade from one to another, with one LED fading out as the other is fading in.

What I’ve Done: One LED fades out, and as soon as it is finished fading, the other fades in. I copied a snippet of the code I’m currently using below… basically just taken from one of the examples on this site.

for (RedBrightness = 255 ; RedBrightness >= 0 ; RedBrightness-=5) {
analogWrite(RedPin, RedBrightness);
delay(30);
}
for (OrangeBrightness = 0 ; OrangeBrightness <= 255 ; OrangeBrightness+=5) {
analogWrite(OrangePin, OrangeBrightness);
delay(30);
}

I’m sure there is an easy solution to this… I just don’t know how to make Arduino do two things at once. My end goal for this is a line of LED’s (as many as I have PWM outputs for) that fade, smoothly, one in to the next.

Any Help?

Here is a simple idea to start with…

for(brightness=0;brightness<=255;brightness+=5) {
    //Fade out red led value counts down from 255 to 0
    analogWrite(RedPin,255-brightness);
    //Fade in the green one value counts up
    analogWrite(OrangePin,brightness);
    delay(30);
}

Eberhard

I’ve done something similar with a row of LEDs, along these lines:

#define FADEINCR 5 // step size; using 5 because that's what the original post uses
//Use chars to keep track of fade direction, because it is the smallest signed data type
char RedDirection = 1; // Red starts fading up
char OrangeDirection = 0; //Orange starts doing nothing
int RedBrightness = 0; // This starts red and orange off
int OrangeBrightness = 0;

void loop() {
    analogWrite(RedPin, RedBrightness);
    analogWrite(OrangePin, OrangeBrightness);
    delay(30);
    RedBrightness += RedDirection*FADEINCR;
    OrangeBrightness += OrangeDirection*FADEINCR;
    if(RedBrightness > 255){ //If we've hit maximum brightness...
        RedBrightness = 255;
        RedDirection = -1; // ...switch to fading down...
        OrangeDirection = 1; // ...and switch orange to fading up.
    } else if(RedBrightness <= 0) { // If we're off...
        RedBrightness = 0;
        RedDirection = 0; // ...stop fading.
    }
    // And again for Orange:
    if(OrangeBrightness > 255){ //If we've hit maximum brightness...
        OrangeBrightness = 255;
        OrangeDirection = -1; // ...switch to fading down...
        RedDirection = 1; // ...and switch red to fading up.
    } else if(OrangeBrightness <= 0) { // If we're off...
        OrangeBrightness = 0;
        OrangeDirection = 0; // ...stop fading.
    }
}

Obviously, this won’t run, it’s just an outline. There are definitely ways to tidy this code up and optimize, but I left it this way for readability. I would also use an array for direction and for brightness, adjusted in a for loop, rather than having separate red and orange variables. If you need help adapting it, I can post the full code, but I figured you might want to figure it out on your own. :slight_smile:

You may also wish to look at http://www.arduino.cc/playground/Main/PWMallPins It has randomly fading LED's on every pin and implements a PWM loop. If you set the values for fade speed of each LED to be the same, I think it will do what you want if you start the values off with a jog. It has a big lookup table though that you might not want hogging all your program space.

You could always pare that down and do linear interpolation, if you want to go through the fuss.

have fun

pb

Well... So far... the elegant first solution is working pretty well! That's exactly the kind of stupid simple solution I hoped existed.

Hopefully it works out for my full application because the code is so concise it leaves me a lot of room.

Thanks for the help.