Need help with a timer problem

So, I'm trying to do an auto-cancelling turn signal code for a motorcycle project. I found a great bit of code that does a great job of Left button press = Blink left LED > Right button press = Blink right LED > Press again to cancel > Press both and both blink > press again to cancel

What i need to add to this is another timer to cancel the active blinking LED after say 2 minutes... but my attempts have failed so far to get it to work.

I'm a new developer and don't really understand what needs to be done to achieve this. I'm not sure if I need to create another set of state machines in order to add another timer or what. What's a good way to cancel the active blinking LED after n seconds and no button press?

Any help is appreciated.

#include <SwitchManager.h>
#include "Timer.h"


typedef enum {
    NONE,
    LH_DOWN,
    RH_DOWN,
    LH_LIGHT_ON,
    RH_LIGHT_ON,
    BOTH
};

const unsigned long BLINK_INTERVAL = 300; // ms
const unsigned long BLINK_TIMEOUT = 30000; // 30 seconds

// pin assignments
const byte LH_SWITCH_PIN = 2;
const byte RH_SWITCH_PIN = 3;
const byte LH_LIGHT = A4;
const byte RH_LIGHT = A5;


SwitchManager LHswitch; 
SwitchManager RHswitch; 
 
byte state = NONE;
 
void handleLHPress (const byte newState, const unsigned long interval, const byte whichPin)
  {
  // switch down?
  if (newState == LOW)
     {
     switch (state)
       {
       // if other switch down, switch to warning mode
       case RH_DOWN:
         state = BOTH;
         break;
         
       // if already on or warning signal, turn all off
       case LH_LIGHT_ON:
       case BOTH:
         state = NONE;
         break;
         
       // otherwise switch is now down, but not yet released
       default:
         state = LH_DOWN;
         break;
       }  // end of switch
     return;
     }  // end of LH switch down
  
  // switch must be up

  if (state == LH_DOWN)  // if down, switch to down-and-released mode
    state = LH_LIGHT_ON;  
  }  // end of handleLHPress
 
void handleRHPress (const byte newState, const unsigned long interval, const byte whichPin)
  {
  // switch down?
  if (newState == LOW)
     {
     switch (state)
       {
       // if other switch down, switch to warning mode
       case LH_DOWN:
         state = BOTH;
         break;
         
       // if already on or warning signal, turn all off
       case RH_LIGHT_ON:
       case BOTH:
         state = NONE;
         break;
         
       // otherwise switch is now down, but not yet released
       default:
         state = RH_DOWN;
         break;
       }  // end of switch
     return;
     }  // end of RH switch down
  
  // switch must be up

  if (state == RH_DOWN)  // if down, switch to down-and-released mode
    state = RH_LIGHT_ON;  
  }  // end of handleRHPress
  
void setup ()
  {
  LHswitch.begin (LH_SWITCH_PIN, handleLHPress);
  RHswitch.begin (RH_SWITCH_PIN, handleRHPress);
  pinMode (LH_LIGHT, OUTPUT);
  pinMode (RH_LIGHT, OUTPUT);
  Serial.begin(9600);
  }  // end of setup

unsigned long lastBlink;
bool onCycle;
 
void blinkLights ()
  {
  lastBlink = millis ();
  onCycle = !onCycle;
  
  // default to off
  digitalWrite (LH_LIGHT, LOW);
  digitalWrite (RH_LIGHT, LOW);

  // every second time, turn them all off
  if (!onCycle)
    return;
    //Serial.print("pressed");
  // blink light
  switch (state)
    {
    case NONE:
      break;
    
    case LH_DOWN:
    case LH_LIGHT_ON:
      digitalWrite (LH_LIGHT, HIGH);
      break;
      
    case RH_DOWN:
    case RH_LIGHT_ON:
      digitalWrite (RH_LIGHT, HIGH);
      break;
      
    case BOTH:
      digitalWrite (LH_LIGHT, HIGH);
      digitalWrite (RH_LIGHT, HIGH);
      break;
    
    }  // end of switch on state
    
  }  // end of blinkLights
  
void loop ()
  {

  LHswitch.check ();  // check for presses
  RHswitch.check ();  // check for presses

 if (millis () - lastBlink >= BLINK_INTERVAL)
    blinkLights ();

 // other stuff
  }  // end of loop

You handle the timeout as you handle each blink:

 if (millis () - lastBlink >= BLINK_INTERVAL)
    blinkLights ();

When you start to blink save in a global long int the starting time, then keep blinking untile passed more millis than BLINK_TIMEOUT

 if (millis () - lastBlink >= BLINK_INTERVAL && millis() - blinkStartMillis < BLINK_TIMEOUT)
    blinkLights (); 
else turnLightsOff();

I had not read the entire sketch, just passed the idea. The "turnLightsOff()" supposed, as it name suggests, to turn lights off in the likely case the blink timeout has expired when the lights were on.

Thanks for the input... Maybe I'm not getting it but I can't quite get it to work as you describe as the original code doesn't really... I can only get it to work partially but end up having to press the button twice as there's not an easy way to set the state of the lights vs. switches that I can tell. But your timer && timer logic looks sound. but the way the functions are written doesn't make for a clean on-off logic.

I wrote similar code and added a counter. Each time the signal is blinked, counter counts +1 (counter++).

A long-press (to be checked with: when function is activated, check after 300 ms whether button is still being pressed => extend = true) activates 15 blinks, a short press only 5.

So (not tested code, wrote live for this question):

int nrBlinks = 5; 
bool extend = false;
long startTimeFunction = 0;

if(buttonIsPressed() == true){
  if(startTimeFunction == 0)
     startTimeFunction = millis();
  for(i = 0; i < nrBlinks; i++){
    if(millis() - startTimeFunction > 300 && extend == false){
      if(buttonIsPressed() == true){
          extend = true;
          nrBlinks = 15;
         }
       blink();
       counter++;
       if(counter == nrBlinks){
          startTimeFunction = 0; 
          return;
          }
      }
  }
}

bool buttonIsPressed(){
  if(digitalRead(pinButton) == LOW){
    return(true);
    } else {
    return(false);
    }
}