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