#include <SwitchManager.h>
typedef enum {
NONE,
LH_DOWN,
RH_DOWN,
LH_LIGHT_ON,
RH_LIGHT_ON,
BOTH
};
const unsigned long BLINK_INTERVAL = 500; // ms
// pin assignments
const byte LH_SWITCH_PIN = 7;
const byte RH_SWITCH_PIN = 8;
const byte LH_LIGHT = A0;
const byte RH_LIGHT = A1;
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);
} // 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;
// 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