Cross Fading LEDs

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
fredk1
 
Posts: 10
Joined: Sat Feb 16, 2013 4:45 pm

Cross Fading LEDs

Post by fredk1 »

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

#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 8:57 am, edited 1 time in total.
Reason: please use [CODE] tags

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Cross Fading LEDs

Post by adafruit_support_rick »

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

  
  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

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

fredk1
 
Posts: 10
Joined: Sat Feb 16, 2013 4:45 pm

Re: Cross Fading LEDs

Post by fredk1 »

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.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Cross Fading LEDs

Post by adafruit_support_bill »

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

Code: Select all

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

  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

  analogWrite(LED, n);
causes a flash.

fredk1
 
Posts: 10
Joined: Sat Feb 16, 2013 4:45 pm

Re: Cross Fading LEDs

Post by fredk1 »

Thanks! I really appreciate it!

fredk1
 
Posts: 10
Joined: Sat Feb 16, 2013 4:45 pm

Re: Cross Fading LEDs

Post by fredk1 »

So maybe something a little more like this?

Code: Select all

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

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Cross Fading LEDs

Post by adafruit_support_bill »

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

Code: Select all

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

fredk1
 
Posts: 10
Joined: Sat Feb 16, 2013 4:45 pm

Re: Cross Fading LEDs

Post by fredk1 »

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 4:45 pm

Re: Cross Fading LEDs

Post by fredk1 »

Just ran the code. My solution was functional but still jumpy. Yours was silky smooth. Thanks again.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”