FadeLed.h cannot shut off LEDS immediately

I am using the FadeLed library to control 4 leds in 8 different scenarios.

The code I have works almost 100% every time I change to a new scenario; however, If I switch scenarios in the middle of a fade sequence some of the LEDS stay on and will not fade out. Is there a way to turn off all leds a once before starting the next scenario.
Attached is my code; so far I am only using scenes 1 thru 4. Selecting scenes 1 and work very well however when I change to the single led scenarios one of the leds stays on, usually the RED led( leds[3]).
Any help appreciated

    /**
 *  @file
 *  @Author Septillion (https://github.com/sseptillion)
 *  @date 2016-08-11
 *  6/16/18 - WJL modified to work for lighthouse
 *  6/17/18 - Modified to add four scenarios
 *  6/18/18 - Modified to use LINK system with 5-stand REV_1 board
 *  7/22/18 - Changed to 4 LED
 *  6/12/19 Added delay(500) to on-off switch
 *  EAGLE BOARD "LIGHTHOUSE_LINX_4LED_NORELAY
 *  4/22/2021 MADE NEW BOARD REV A
 *  6/28/2023 CONVERTED TO NEW LIGHTHOUSE USING MOTEINO RADIOS  
 */

#include <FadeLed.h>

//make four FadeLed objects for pin 3 (leds[0]), pin 9 (leds[1], pin 10(leds[2] and pin 11(leds[3])
FadeLed leds[] = {3, 9, 10, 11};
//leds[0] = WHITE
//leds[1] = GREEN
//leds[2] = BLUE
//leds[3] = RED

int seqA = 8;        //BUTTON LINX#D0
int seqB = 4;         //BUTTON LINX#D1 
int seqC = 7;         //BUTTON LINX#D2 
int seqD = 12;         //BUTTON LINX#D3 
int seqE = 2;         //BUTTON LINX#D4 
int seqF = 17;         //BUTTON LINX#D6 
int seqG = 16;         //BUTTON LINX#D5 
int seqH = 15;          //scene #8 , 9 button pushes
int out_on_off = 16;  //POLOLU SWITCH
int state = HIGH;       //current state of the output pin
int previous = LOW;     //previous reading from the input pin

int patA;
int patB;
int patC;  
int patD;   
int patE;
int patF;
int patG;

unsigned long millisLast_0;
unsigned long millisLast_1;
unsigned long millisLast_2;
unsigned long millisLast_3;
unsigned long millisLast_4;
unsigned long millisLast_5;
unsigned long millisLast_6;

int patA_A = 1;
int patB_A = 0;
int patC_A = 0;
int patD_A = 0;
int patE_A = 0;
int patF_A = 0;
int patG_A = 0;

byte currentLed = 0;

void setup() 
{
  Serial.begin(115200);
  pinMode (seqA, INPUT);
  pinMode (seqB, INPUT);
  pinMode (seqC, INPUT);
  pinMode (seqD, INPUT);
  pinMode (seqE, INPUT);
  pinMode (seqF, INPUT);
  pinMode (seqG, INPUT); 
  pinMode (out_on_off, OUTPUT);  
  delay(5000);
  Serial.println("File name = LIGHTHOUSE_9_4LED_NORELAY_MOTEINO");
}

void loop() 
{
  FadeLed::update();

  patA = digitalRead(seqA);
  patB = digitalRead(seqB);
  patC = digitalRead(seqC);
  patD = digitalRead(seqD);
  patE = digitalRead(seqE);
  patF = digitalRead(seqF);
  patG = digitalRead(seqG);

  //Serial.print("patA = ");Serial.println(patA);
  //Serial.print("patB = ");Serial.println(patB);
  //Serial.print("patC = ");Serial.println(patC);
  //Serial.print("patD = ");Serial.println(patD);
  
  if (patA == HIGH)       //LINX BUTTON #1
  {
    patA_A = 1;
    patB_A = 0;
    patC_A = 0;
    patD_A = 0;
    patE_A = 0;
    patF_A = 0;
    patG_A = 0;

    leds[0].setTime(2000,true);
    leds[1].setTime(2000,true);
    leds[2].setTime(2000,true);
    leds[3].setTime(2000,true);  
    //and I start fading the first
    currentLed = 0;
    leds[currentLed].on();
  }
        
  if (patA_A == 1)
  {
     routineA();
  }

  if (patB == HIGH)       //LINX BUTTON #2
  {
    patA_A = 0;
    patB_A = 1;
    patC_A = 0;
    patD_A = 0;
    patE_A = 0;
    patF_A = 0;
    patG_A = 0;
    
    leds[0].setTime(2000,true);
    leds[1].setTime(2000,true);
    leds[2].setTime(2000,true);
    leds[3].setTime(2000,true);  
    //and I start fading the first
    currentLed = 3;   //3 = RED
    leds[currentLed].on(); 
  }
  if (patB_A == 1)
  {
    routineB();
  }
    
  if (patC == HIGH)     //LINX BUTTON #3
  {
    patA_A = 0;
    patB_A = 0;
    patC_A = 1;
    patD_A = 0;
    patE_A = 0;
    patF_A = 0;
    patG_A = 0;
    leds[0].begin(0);
    leds[1].begin(0);
    leds[2].begin(0); 
    leds[2].setTime(2000);
  }
  if (patC_A == 1)
  {
    routineC();
  }

  if (patD == HIGH)   //LINX BUTTON #4
  {
    patA_A = 0;
    patB_A = 0;
    patC_A = 0;
    patD_A = 1;
    patE_A = 0;
    patF_A = 0;
    patG_A = 0;
    leds[0].begin(0);
    leds[1].begin(0);
    leds[2].begin(0); 
  }
  if (patD_A == 1)
  {
    leds[0].setTime(2000);
    routineD  ();
  }  

  if (patE == HIGH)    //LINX BUTTON #5
  {
    patA_A = 0;
    patB_A = 0;
    patC_A = 0;
    patD_A = 0;
    patE_A = 1;
    patF_A = 0;
    patG_A = 0;
    leds[0].setTime(2000,true);
    leds[1].setTime(2000,true);
    leds[2].setTime(2000,true);
    leds[3].setTime(2000,true);  
    //and I start fading the first
    currentLed = 3;   //3 = RED
    leds[currentLed].on(); 
  }
  if (patE_A == 1)
  {
    routineE  ();
  }

    if (patF == HIGH)
  {
    patA_A = 0;
    patB_A = 0;
    patC_A = 0;
    patD_A = 0;
    patE_A = 0;
    patF_A = 1;
    patG_A = 0;
    leds[0].begin(0);
    leds[1].begin(0);
    leds[3].begin(0); 
  }
  if (patF_A == 1)
  {
    leds[0].setTime(1000);
    routineD  ();
  }

  if (patG == HIGH)
  {
    patA_A = 0;
    patB_A = 0;
    patC_A = 0;
    patD_A = 0;
    patE_A = 0;
    patF_A = 0;
    patG_A = 1;
    leds[0].begin(0);
    leds[1].begin(0);
    leds[2].begin(0); 
  }
  if (patG_A == 1)
  {
    leds[0].setTime(1000);
    routineD  ();
  }
} 
void routineA()       //RED-WHITE-GREEN-BLUE, LINX BUTTON #1
 { 
  Serial.println("AT routineA()");
  if(leds[currentLed].done() && leds[currentLed].get())
    {
      //Serial.print("FADE DOWN - leds[current.led].get = "); Serial.println(leds[currentLed].get());
      //fade down again
      leds[currentLed].off();
    }
  //if done fading down
  else if(leds[currentLed].done() && !leds[currentLed].get())
   {
    //      Serial.print("FADE UP - !leds[current.led].get = "); Serial.println(leds[currentLed].get());
    //go to next led
    currentLed++;
    if(currentLed >= 4)
    {
      currentLed = 0;
      Serial.println("ROUTINE A, NEXT LED");
    }
   
    //and start fading that
    leds[currentLed].set(100);
   }
 }

void routineB()   //RED-WHITE-BLUE, LINX BUITTON #2
 {
  Serial.println("AT routineB()");
  if(leds[currentLed].done() && leds[currentLed].get())
    {
      //fade down again
      leds[currentLed].off();
   }
  //if done fading down
   else if(leds[currentLed].done() && !leds[currentLed].get())
   {
    //go to next led
    if(currentLed == 3)
    {
      Serial.println("ROUTINE b, NEXT LED #0");
      Serial.print("currentLed = "); Serial.println(currentLed);
      currentLed = 0;
      leds[currentLed].set(100);
    }
    else if(currentLed == 0)
    {
      Serial.println("ROUTINE b, NEXT LED #2");
      Serial.print("currentLed = "); Serial.println(currentLed);
      currentLed = 2;
      leds[currentLed].set(100);
    }
    else if(currentLed == 2)
    {
      Serial.println("ROUTINE b, NEXT LED #3");
      Serial.print("currentLed = "); Serial.println(currentLed);
      currentLed = 3;
      leds[currentLed].set(100);
    }
    /*
    else if   (currentLed == 3)
    {
      Serial.println("ROUTINE b, NEXT LED #3");
      Serial.print("currentLed = "); Serial.println(currentLed);
      currentLed = 0;
      leds[currentLed].set(75); 
    }
    */
    //and start fading that
    //leds[currentLed].set(75);
   }
 }

void routineC()       //BLUE LED, LINX BUTON#3
 {
  Serial.println("at routineC()");
  leds[0].off();
  leds[1].off();
  leds[3].off();
  if(leds[2].done())            //BLUE LED
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if(leds[2].get())
    {
      leds[2].setTime(2000,true);
      //then we are done fading up, let's fade down again
      leds[2].off();
    }
    //or we are at 0
    else
    {
      if (millis() - millisLast_2 > 7000)
      {
      leds[2].setTime(1000,true);
      //then we are done fading down, let's fade up again
      leds[2].set(100);
      millisLast_2 = millis();
      }
    }
  }
 }  

void routineD()         //WHITE LED, LINX BUTTON #4
 {
  Serial.println("at routineD()");
  leds[1].off();
  leds[2].off();
  leds[3].off();
  if(leds[0].done())        //WHITE LED
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if(leds[0].get())
    {
      leds[0].setTime(1000, true);
      //then we are done fading up, let's fade down again
      leds[0].off();
    }
    //or we are at 0
    else
    {
      if (millis() - millisLast_0 > 7000)
      {
        leds[0].setTime(1000,true);
      //then we are done fading down, let's fade up again
      leds[0].set(100);
      millisLast_0 = millis();
      }
    }
  }
 }

 void routineE()   //RED-WHITE-GREEN, LINX BUTTON #5
 {
  Serial.println("at routinE()");
  if(leds[currentLed].done() && leds[currentLed].get())
    {
      //fade down again
      leds[currentLed].off();
   }
  //if done fading down
   else if(leds[currentLed].done() && !leds[currentLed].get())
   {
    //go to next led
    if(currentLed == 3)
    {
      Serial.println("ROUTINE b, NEXT LED #0");
      Serial.print("currentLed = "); Serial.println(currentLed);
      currentLed = 0;
      leds[currentLed].set(75);
    }
    else if(currentLed == 0)
    {
      Serial.println("ROUTINE b, NEXT LED #1");
      Serial.print("currentLed = "); Serial.println(currentLed);
      currentLed = 1;
      leds[currentLed].set(75);
    }
    else if(currentLed == 1)
    {
      Serial.println("ROUTINE b, NEXT LED #3");
      Serial.print("currentLed = "); Serial.println(currentLed);
      currentLed = 3;
      leds[currentLed].set(75);
    }
    /*
    else if   (currentLed == 3)
    {
      Serial.println("ROUTINE b, NEXT LED #3");
      Serial.print("currentLed = "); Serial.println(currentLed);
      currentLed = 0;
      leds[currentLed].set(75); 
    }
    */
    //and start fading that
    //leds[currentLed].set(75);
   }
 }
/**********************************************
    if(leds[1].done())          //GREEN LED
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if(leds[1].get())
    {
       leds[1].setTime(3000,true);
      //then we are done fading up, let's fade down again
      leds[1].off();
    }
    //or we are at 0
    else
    {
      //if (millis() - millisLast_1 > 7000)
      //{
      leds[1].setTime(3000,true);
      //then we are done fading down, let's fade up again
      leds[1].set(25);
      //millisLast_1 = millis();
      //}
    }
  }

    if(leds[2].done())            //WHITE LED
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if(leds[2].get())
    {
      leds[2].setTime(1000,true);
      //then we are done fading up, let's fade down again
      leds[2].off();
    }
    //or we are at 0
    else
    {
      if (millis() - millisLast_2 > 7000)
      {
      leds[2].setTime(1000,true);
      //then we are done fading down, let's fade up again
      leds[2].set(25);
      millisLast_2 = millis();
      }
    }
  }
 }

void routineE()
 {
  Serial.println("at routineE");
  if(leds[0].done())
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if(leds[0].get())
    {
      //then we are done fading up, let's fade down again
      leds[0].off();
    }
    //or we are at 0
    else
    {
      if (millis() - millisLast_0 > 14000)
      {
      //then we are done fading down, let's fade up again
      leds[0].set(50);
      millisLast_0 = millis();
      }
    }
  }

    if(leds[1].done())
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if(leds[1].get())
    {
      //then we are done fading up, let's fade down again
      leds[1].off();
    }
    //or we are at 0
    else
    {
      if (millis() - millisLast_1 > 14000)
      {
      //then we are done fading down, let's fade up again
      leds[1].set(50);
      millisLast_1 = millis();
      }
    }
  }

    if(leds[2].done())
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if(leds[2].get())
    {
      //then we are done fading up, let's fade down again
      leds[2].off();
    }
    //or we are at 0
    else
    {
      if (millis() - millisLast_2 > 14000)
      {
      //then we are done fading down, let's fade up again
      leds[2].set(50);
      millisLast_2 = millis();
      }
    }
  }
 }

void routineF()
 {
  Serial.println("at routineE");
  if(leds[0].done())
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if(leds[0].get())
    {
      //then we are done fading up, let's fade down again
      leds[0].off();
    }
    //or we are at 0
    else
    {
      if (millis() - millisLast_0 > 14000)
      {
      //then we are done fading down, let's fade up again
      leds[0].set(50);
      millisLast_0 = millis();
      }
    }
  }

    if(leds[1].done())
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if(leds[1].get())
    {
      //then we are done fading up, let's fade down again
      leds[1].off();
    }
    //or we are at 0
    else
    {
      if (millis() - millisLast_1 > 14000)
      {
      //then we are done fading down, let's fade up again
      leds[1].set(50);
      millisLast_1 = millis();
      }
    }
  }

    if(leds[2].done())
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if(leds[2].get())
    {
      //then we are done fading up, let's fade down again
      leds[2].off();
    }
    //or we are at 0
    else
    {
      if (millis() - millisLast_2 > 14000)
      {
      //then we are done fading down, let's fade up again
      leds[2].set(50);
      millisLast_2 = millis();
      }
    }
  }
 }

void routineG()
 {
  Serial.println("at routineF");
  if(leds[0].done())
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0 or using !xxx if we are at zero
    if(leds[0].get())
    {
      //then we are done fading up, let's fade down again
      leds[0].off();
    }
    //or we are at 0
    else
    {
      if (millis() - millisLast_0 > 14000)
      {
      //then we are done fading down, let's fade up again
      leds[0].set(50);
      millisLast_0 = millis();
      }
    }
  }

    if(leds[1].done())
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if(leds[1].get())
    {
      //then we are done fading up, let's fade down again
      leds[1].off();
    }
    //or we are at 0
    else
    {
      if (millis() - millisLast_1 > 14000)
      {
      //then we are done fading down, let's fade up again
      leds[1].set(50);
      millisLast_1 = millis();
      }
    }
  }

    if(leds[2].done())
  {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if(leds[2].get())
    {
      //then we are done fading up, let's fade down again
      leds[2].off();
    }
    //or we are at 0
    else
    {
      if (millis() - millisLast_2 > 14000)
      {
      //then we are done fading down, let's fade up again
      leds[2].set(50);
      millisLast_2 = millis();
      }
    }
  }
 }
*******************************************/  

Write a function to do it and call it whenever you need to turn them all off

I have tried leds[xx].off() in several locations in the code but it does not work.
do you mean something like this

void routineA()       //RED-WHITE-GREEN-BLUE, LINX BUTTON #1
 { 
  Serial.println("AT routineA()");
  shutOff():
  if(leds[currentLed].done() && leds[currentLed].get())
    {
      //Serial.print("FADE DOWN - leds[current.led].get = "); Serial.println(leds[currentLed].get());
      //fade down again
      leds[currentLed].off();
    }
  //if done fading down
  else if(leds[currentLed].done() && !leds[currentLed].get())
   {
    //      Serial.print("FADE UP - !leds[current.led].get = "); Serial.println(leds[currentLed].get());
    //go to next led
    currentLed++;
    if(currentLed >= 4)
    {
      currentLed = 0;
      Serial.println("ROUTINE A, NEXT LED");
    }
   
    //and start fading that
    leds[currentLed].set(100);
   }
 }

 void shutOff{
   leds[0].off();
   leds[1].off();
   leds[2].off();
   leds[3].off();
 }

Or should I use another function to turn off the leds??

Thanks for your help
Bill

I added

void shutOff()
{
  Serial.println("SHUTDOWN");
  leds[0].off();
  leds[1].off();
  leds[2].off();
  leds[3].off();
  return;

at the place where I call for a change i.e.


  if (patA == HIGH)       //LINX BUTTON #1
  {
    Serial.print(" SET patA HIGH");
    patA_A = 1;
    patB_A = 0;
    patC_A = 0;
    patD_A = 0;
    patE_A = 0;
    patF_A = 0;
    patG_A = 0;

    leds[0].setTime(2000,true);
    leds[1].setTime(2000,true);
    leds[2].setTime(2000,true);
    leds[3].setTime(2000,true);  
    //and I start fading the first
    currentLed = 0;
    shutOff();
    leds[currentLed].on();
  }

and it seems to work but it takes a long time, I think its because each led has to fade out.

Is there a quicker way to shutdown the leds??

Thanks
Bill

I am not familiar with the library but it looks like the first parameter in the setTime() function controls how long it takes to fade the LED so you could set it to zero

How about simply setting the state of the LED pins to HIGH or LOW to turn them off ?

I'l keep making changes.

Thanks

Your code needs to go on a serious diet! It's almost 7 times longer than it needs to be.

Anywhere you have a group of variables who's names are the same except for a number or identifying letter which is part of their name, you should be using an array. Example:

const byte seqCount = 7;
const byte seqBtn[seqCount] = {
  8,       //BUTTON LINX#D0
  4,         //BUTTON LINX#D1 
  7,         //BUTTON LINX#D2 
  12,         //BUTTON LINX#D3 
  2,         //BUTTON LINX#D4 
  17,         //BUTTON LINX#D6 
  16         //BUTTON LINX#D5
};

int pat[seqCount];

void setup() 
{
  Serial.begin(115200);
  for (byte s=0; s<seqCount; s++) {
    pinMode (seqBtn[s], INPUT);
  }
...
}

void loop() 
{
  FadeLed::update();

  for (byte s=0; s<seqCount; s++) {
    pat[s] = digitalRead(seqBtn[s]);
  }
...

If you are not getting the idea, just ask!

Any time you find yourself copying & pasting sections of code and making a few small changes to each copy, stop and think how you can avoid that. Arrays and for-loops are usually the answer.

Thank you; I am always looking for a way to shorten my code.

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