Fading at different times

Hello everyone. I'm trying to figure out how to Fade an Led in and out. Then have a second begin fading in before the first one is completely faded out. Maybe it's not possible. I started by fading the LED 3 in and out. No problems. When I add the one on Pin 5 the Pin 5 LED does nothing and he LED on 3 doesn't fade out properly.
Any help appreciated

[code]
int led3 = 3; 
int led5 = 5;         
int brightness = 0;    // how bright the LED is
int fadeAmount = 3;    // how many points to fade the LED by
unsigned long thisMillis;
unsigned long previousFadeMillis;
int waitInterval = 1000;
void setup() {

  pinMode(led3, OUTPUT);
  pinMode(led5, OUTPUT);
}

void loop() {

  fade_3();
  fade_5();

}
  void fade_3() {

    analogWrite(led3, brightness);               // set the brightness of pin 3

    brightness = brightness + fadeAmount;    // change the brightness for next  loop:

    if (brightness == 0 || brightness == 255) {      // reverse the direction of  fade
    }
    delay(25);           
    
  }
    
    void fade_5 () {
    
    if (thisMillis - previousFadeMillis >= waitInterval) {     //wait one second

  analogWrite(led5, brightness);               // set the brightness of pin 5

    brightness = brightness + fadeAmount;    // change the brightness for next  loop:

    if (brightness == 0 || brightness == 255) {      // reverse the direction of fade
      
      delay(25);
  }
    }
    }

[/code]
.

Hello everyone. I'm trying to figure out how to Fade an Led in and out. Then have a second begin fading in before the first one is completely faded out. Maybe it's not possible. I started by fading the LED 3 in and out. No problems. When I add the one on Pin 5 the Pin 5 LED does nothing and he LED on 3 doesn't fade out properly.
Any help appreciated

int led3 = 3;
int led5 = 5;         
int brightness = 0;    // how bright the LED is
int fadeAmount = 3;    // how many points to fade the LED by
unsigned long thisMillis;
unsigned long previousFadeMillis;
int waitInterval = 1000;
void setup() {

  pinMode(led3, OUTPUT);
  pinMode(led5, OUTPUT);
}

void loop() {

  fade_3();
  fade_5();

}
  void fade_3() {

    analogWrite(led3, brightness);               // set the brightness of pin 3

    brightness = brightness + fadeAmount;    // change the brightness for next  loop:

    if (brightness == 0 || brightness == 255) {      // reverse the direction of  fade
    }
    delay(25);           
   
  }
   
    void fade_5 () {
   
    if (thisMillis - previousFadeMillis >= waitInterval) {     //wait one second

  analogWrite(led5, brightness);               // set the brightness of pin 5

    brightness = brightness + fadeAmount;    // change the brightness for next  loop:

    if (brightness == 0 || brightness == 255) {      // reverse the direction of fade
     
      delay(25);
  }
    }
    }

Well, this statement does nothing:

    if (brightness == 0 || brightness == 255) {      // reverse the direction of  fade
    }

Sorry, wrong forum

? I'll begin again

See How to fade LED in a time period without using delay()

IoT_hobbyist:
See How to fade LED in a time period without using delay()

Thanks again, But now I need to learn about MAP :slight_smile: see my icon

There was a post on this what? A week ago? Two weeks?

-jim lee

Ha! time flies! Back in the beginning of January..

Here's the example. Does as many LEDs as you have outputs for.

Good luck understanding it.

#include <multiMap.h>
#include <timeObj.h>
#include <idlers.h>


#define  PROFILE_MS  800   // Total time that the profile spans.
#define  CHANGE_MS   4     // How much time between recalculating new brightness values.
#define  LED_PIN1    3     // Need pins that have analog out.
#define  LED_PIN2    9
#define  LED_PIN3    10

// Create a class that takes a  time,value profile and plays it as a PWM.
// And, runs in the background.


class mappedAnalog   : public idler {

   public:
               mappedAnalog(multiMap* inMap,int inPin,float inChange,float totalMs);
   virtual     ~mappedAnalog(void);

               void  start(float delayMs=0);
   virtual     void  idle(void);

               multiMap*   ourProfile;
               int         pin;
               int         frameMs;
               float       changeMs;
               float       profileMs;
               timeObj     changeTimer;
               bool        delaying;
};


mappedAnalog::mappedAnalog(multiMap* inMap,int inPin,float inChange,float totalMs) {

   ourProfile  = inMap;
   pin         = inPin;
   frameMs     = 0;
   changeMs    = inChange;
   profileMs   = totalMs;
   changeTimer.setTime(changeMs,false);
   delaying    = false;
}


mappedAnalog::~mappedAnalog(void) {  }


void mappedAnalog::start(float delayMs) {

   if (delayMs>0) {
      changeTimer.setTime(delayMs);
      delaying = true;
   }
   hookup();
   pinMode(pin,OUTPUT);
   frameMs = 0;
   changeTimer.start();
}


void mappedAnalog::idle(void) {

   float newBright;
   
   if (delaying) {
      if(changeTimer.ding()) {
         changeTimer.setTime(changeMs);
         changeTimer.start();
         delaying = false;
      }
   } else {
      if (changeTimer.ding()) {
         if (frameMs>=profileMs) {
            frameMs = 0;
         } else {
            frameMs = frameMs + changeMs;
         }
         newBright = ourProfile->map(frameMs);
         analogWrite(pin, round(newBright));
         changeTimer.start();
      }
   }
}




// ********************* Program starts here *********************



multiMap       profile;    // Your time / brightness profile.
mappedAnalog*  LED1;       // An LED
mappedAnalog*  LED2;       // ANother..
mappedAnalog*  LED3;       // Notice these are actually just global pointers to LED objects. Make as many as you need.


void setup(void) {

   profile.addPoint(0,0);                 // Build the pofile by adding points (time in ms, brighness value)
   profile.addPoint(PROFILE_MS/2,255);    // Second point.
   profile.addPoint(PROFILE_MS,0);        // Last point. ( You can actually add as many points as you like. Go crazy! )
   
   LED1 = new mappedAnalog(&profile,LED_PIN1,CHANGE_MS,PROFILE_MS);  // Create first LED object. Address of profile, pin num, frame rate, total time for profile.
   LED1->start();                                                    // Fire uip first LED.
   LED2 = new mappedAnalog(&profile,LED_PIN2,CHANGE_MS,PROFILE_MS);  // Create second LED.
   LED2->start(200);                                                 // Fire it up with a preset delay before starting.
   LED3 = new mappedAnalog(&profile,LED_PIN3,CHANGE_MS,PROFILE_MS);  // Third LED
   LED3->start(250);                                                 // And again a delay from time 0 for actual start.
}


void loop(void) {
   
   idle();  // Runs all the background things.
}

If you want to try it you will need to install LC_baseTools from the library manager. And you'll need 3 LEDs As it is they all follow the same fade pattern and speed. They can have different fade patters and speeds if you make up different profiles, but for now they all use the same profile and just use sort of a delayed start to overlap.

-jim lee

You use the variable brightness for both fade3 and fade5. That doesn't look good.

Or, if you have nothing against libraries and just want it done: see my signature :slight_smile:

Polliwog:
Sorry, wrong forum

This is the Arduino forum. Are you not using an Arduino? How could it be the wrong forum?

If you want to fade the LEDs independently you need to treat the two LEDs as separate entities (so don't use a common "brightness" that you change in both functions...) You need to give them their own timers (millis() variables etc) and try not to use delay().

Does this modified version of your code do what you want?

#define FADE3_STEP_TIME     10ul            //10mS/step for LED3
#define FADE5_STEP_TIME     20ul            //20mS/step for LED5
#define FADE5_HOLDOFF       1000ul          //amount of time out of reset we wait before fading 5

#define FADE_MAX            255             //max brightness PWM value
#define FADE_MIN            0               //min brightness PWM value

uint8_t led3 = 3;
uint8_t led5 = 5;
         
unsigned long thisMillis;

void setup() 
{
    pinMode(led3, OUTPUT);
    pinMode(led5, OUTPUT);
    
}//setup

void loop() 
{    
    fade_3();
    fade_5();

}//loop

void fade_3() 
{
    static int8_t
        fadeAmt = 1;
    static uint8_t        
        bright3 = FADE_MIN;
    static uint32_t
        timeBright3=0ul;
    
    thisMillis = millis();
    //is it time for a brightness step?
    if( thisMillis - timeBright3 < FADE3_STEP_TIME )
        return;

    //save the time for the next step
    timeBright3 = thisMillis;
    //bump the brightness
    bright3 = bright3 + fadeAmt;
    //write it
    analogWrite(led3, bright3);
    //if we're at the limit now, reverse the step direction
    if( bright3 == FADE_MIN || bright3 == FADE_MAX )
        fadeAmt *= -1;
   
}//fade_3
   
void fade_5 () 
{   
    static bool
        bFade5=false;
    static int8_t
        fadeAmt = 1;
    static uint8_t        
        bright5 = FADE_MIN;
    static uint32_t
        timeBright5=0ul;

    thisMillis = millis();
    //logic is mostly the same except we hold-off stepping pin 5
    //for 1-second (1000mS. The static boolean bFade5 flag is used
    //to signal when that initial 1-second period has elapsed
    //once set, we treat this LED just like the other
    if( !bFade5 )
    {
        if( thisMillis - timeBright5 >= FADE5_HOLDOFF )
        {
            timeBright5 = thisMillis;
            bFade5=true;
            
        }//if
    }//if
    else
    {
        if( thisMillis - timeBright5 < FADE5_STEP_TIME )
            return;

        timeBright5 = thisMillis;
        bright5 = bright5 + fadeAmt;
        analogWrite(led5, bright5);
        if( bright5 == FADE_MIN || bright5 == FADE_MAX )
            fadeAmt *= -1;
        
    }//else
    
}//fade_5

Paul__B:
This is the Arduino forum. Are you not using an Arduino? How could it be the wrong forum?

It's an attempt to say, "oops, duplicate thread".
https://forum.arduino.cc/index.php?topic=724582.0

So a cross post then.

Polliwog:
Sorry, wrong forum
? I'll begin again

Next time use report to moderator.

Do not cross-post.

Threads merged

Paul__B:
So a cross post then.

Yes, sorry. I began in the wrong "section"

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.