Cross Fading LEDs
Moderators: adafruit_support_bill, adafruit

Cross Fading LEDs

by fredk1 on Tue Mar 05, 2013 11:00 pm

I'm very new to this so please excuse such a basic question.... I'm trying to cross fade a RGB LED using 3 PWM pins. I am using a "for" statement within a for statement and it cross fades smoothly within the loop. But when it jumps back to the top of the loop I get a visual pulse in the LED. Any suggestions? Thanks for the help. Here's the code:

Code: Select all | TOGGLE FULL SIZE
#define LED 9 //the pin for the LED
#define LED2 10 //the pin for the LED
#define LED3 11//THe last pin
int n = 0; //counting up and down
int i = 0; //we'll use this to count up and down
int w = 0;
void setup() {
  pinMode(LED, OUTPUT); //tell a LED is an output
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
}

void loop() {
 
  for(n = 255; n > 1; n--) { //loop from 0 to 254 (fade out)
  analogWrite(LED, n); //set the LED brightness
  for(i = 0; i < 255; i++);//Fade in
  analogWrite(LED2, i);
  delay (10);
  }
 
  for(i = 255; i > 1; i--) { //loop from 0 to 254 (fade out)
  analogWrite(LED2, i); //set the LED brightness
  for(w = 0; w < 255; w++);//Fade in
  analogWrite(LED3, w);
  delay (10);
  }
 
  for(w = 255; w > 1; w--) { //loop from 0 to 254 (fade out)
  analogWrite(LED2, w); //set the LED brightness
  for(n = 0; n < 255; n++); //Fade in
  analogWrite(LED, n);
  delay (10);
  }

}
Last edited by adafruit_support_rick on Wed Mar 06, 2013 7:57 am, edited 1 time in total.
Reason: please use [CODE] tags
fredk1
 
Posts: 10
Joined: Sat Feb 16, 2013 3:45 pm

Re: Cross Fading LEDs

by adafruit_support_rick on Wed Mar 06, 2013 8:02 am

You reset the LED in the inner loop from 255 back to zero each time through the output loop. Is this the pulse you're referring to?
Code: Select all | TOGGLE FULL SIZE
 
  for(n = 255; n > 1; n--) { //loop from 0 to 254 (fade out)
    analogWrite(LED, n); //set the LED brightness
    for(i = 0; i < 255; i++);//Fade in    //RESET LED2 TO 0 AND FADE IN
      analogWrite(LED2, i);
  delay (10);
  }


Maybe you want something more like this?
Code: Select all | TOGGLE FULL SIZE
 
  for(n = 255; n > 1; n--) { //loop from 0 to 254 (fade out)
    analogWrite(LED, n);             //set the LED brightness
    analogWrite(LED2, (255-n));  //set LED2 brightness as the complement of LED
    delay (10);
  }
User avatar
adafruit_support_rick
 
Posts: 11594
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Cross Fading LEDs

by fredk1 on Wed Mar 06, 2013 10:10 am

Thanks for helping me with the "code" tab.

As to your comment, I start LED (blue) at full brightness and then dim it down at the beginning of the loop. As it fades down, LED2 (green) fades in. Visually this is a smooth transition, as is the fade down of LED2 (green) and the fade up of LED3 (red). The LED3 fade down is also smooth as is the fade back up of LED (blue) at the end of the loop. At the end of the loop, LED should be back at 255, full brightness. The visual pulse happens when it goes back to the beginning of the loop, blue pulses on and off again. So I guess my question is, when the sketch runs, does it loop through the "loop" only, or does it loop through the entire sketch? (Then I would be resetting LED to 0.) Could it also be just a poor use of the "for" condition? Any help would be greatly appreciated. This is the first step in a larger project.

Thanks.
fredk1
 
Posts: 10
Joined: Sat Feb 16, 2013 3:45 pm

Re: Cross Fading LEDs

by adafruit_support_bill on Wed Mar 06, 2013 10:23 am

You have a nested 'for' loop, but the inner loop is empty, all it does is count up to 255:

Code: Select all | TOGGLE FULL SIZE
for(w = 255; w > 1; w--) { //loop from 0 to 254 (fade out)
  analogWrite(LED2, w); //set the LED brightness
  for(n = 0; n < 255; n++); //Fade in
  analogWrite(LED, n);
  delay (10);
  }


The scope of this loop:
Code: Select all | TOGGLE FULL SIZE
  for(n = 0; n < 255; n++); //Fade in

is bounded by the semicolon. So it does no fading. It just leaves 'n' at 255, so this line:
Code: Select all | TOGGLE FULL SIZE
  analogWrite(LED, n);

causes a flash.
User avatar
adafruit_support_bill
 
Posts: 29110
Joined: Sat Feb 07, 2009 9:11 am

Re: Cross Fading LEDs

by fredk1 on Wed Mar 06, 2013 10:37 am

Thanks! I really appreciate it!
fredk1
 
Posts: 10
Joined: Sat Feb 16, 2013 3:45 pm

Re: Cross Fading LEDs

by fredk1 on Wed Mar 06, 2013 2:34 pm

So maybe something a little more like this?
Code: Select all | TOGGLE FULL SIZE
  for(w = 255; w > 1; w--) { //loop from 0 to 254 (fade out)
  analogWrite(LED_red, w); //set the LED brightness
  for(n = 0; n < 255; n++){
  analogWrite(LED_blue, n);
  }
  delay (10);
  }
fredk1
 
Posts: 10
Joined: Sat Feb 16, 2013 3:45 pm

Re: Cross Fading LEDs

by adafruit_support_bill on Wed Mar 06, 2013 4:32 pm

I don't think you even need the nested loop. For a cross fade, I think you want something like:

Code: Select all | TOGGLE FULL SIZE
  for(i = 0; i < 256; i++)
  {
    analogWrite(LED_red, 255-i);          //red led fades out
    analogWrite(LED_blue, i);  // blue led fades in
    delay (10);
  }
User avatar
adafruit_support_bill
 
Posts: 29110
Joined: Sat Feb 07, 2009 9:11 am

Re: Cross Fading LEDs

by fredk1 on Wed Mar 06, 2013 4:57 pm

Cool. And I really appreciate the help. If you can't tell, I'm learning to code at the same time as learning Arduino.
fredk1
 
Posts: 10
Joined: Sat Feb 16, 2013 3:45 pm

Re: Cross Fading LEDs

by fredk1 on Wed Mar 06, 2013 8:24 pm

Just ran the code. My solution was functional but still jumpy. Yours was silky smooth. Thanks again.
fredk1
 
Posts: 10
Joined: Sat Feb 16, 2013 3:45 pm