Hi all,
I've adapted a project that was on Nick Gammon's switches page to do something similar but different.
The project Nick Wrote takes 2 switches and uses his switchmanager library to flash either a left indicator, right indicator or hazards.
Nick's code that was starting point
Nick's page on his switch library - also includes example that was starting point
I'm not looking to flash the output. I'm wanting to switch relay 1 or relay 2 on for 500ms & then turn off again if switch 1, or switch 2 are pressed. If both switches are pressed relay 3 turns on for 500ms. Whatever is pressed the respective relay only comes on once.
With the modification to the code on Nick's page I've gotten to the point where the respective relays turn on - and off again on a subsequent button press.
I can't get to the point where the relays simply come on for 500ms & go off without the subsequent button press.
The end goal is that they need to come on once for 500ms and then go off.
Any advice on the key change I'm missing please?
Have tried a number of things without success.
Code below,
Thanks
Dave
// Adopted from here https://arduino.stackexchange.com/questions/13488/how-to-detect-if-more-than-one-button-was-pressed
// https://www.gammon.com.au/switches
#include <SwitchManager.h>
typedef enum {
NONE,
LOW_DOWN,
HIGH_DOWN,
LOW_RELAY_ON,
HIGH_RELAY_ON,
BOTH
};
const unsigned long BLINK_INTERVAL = 500; // ms
// pin assignments
const byte LOW_SWITCH_PIN = 7;
const byte HIGH_SWITCH_PIN = 8;
const byte LOW_RELAY = A0;
const byte HIGH_RELAY = A1;
const byte DOUBLE_RELAY = 6;
SwitchManager LOWswitch;
SwitchManager HIGHswitch;
byte state = NONE;
void handleLOWPress (const byte newState, const unsigned long interval, const byte whichPin)
{
// switch down?
if (newState == LOW)
{
switch (state)
{
// if other switch down, switch to double mode
case HIGH_DOWN:
state = BOTH;
break;
// if already on or double signal, turn all off
case LOW_RELAY_ON:
case BOTH:
state = NONE;
break;
// otherwise switch is now down, but not yet released
default:
state = LOW_DOWN;
break;
} // end of switch
return;
} // end of LH switch down
// switch must be up
if (state == LOW_DOWN) // if down, switch to down-and-released mode
state = LOW_RELAY_ON;
} // end of handleLHPress
void handleHIGHPress (const byte newState, const unsigned long interval, const byte whichPin)
{
// switch down?
if (newState == LOW)
{
switch (state)
{
// if other switch down, switch to double mode
case LOW_DOWN:
state = BOTH;
break;
// if already on or double signal, turn all off
case HIGH_RELAY_ON:
case BOTH:
state = NONE;
break;
// otherwise switch is now down, but not yet released
default:
state = HIGH_DOWN;
break;
} // end of switch
return;
} // end of RH switch down
// switch must be up
if (state == HIGH_DOWN) // if down, switch to down-and-released mode
state = HIGH_RELAY_ON;
} // end of handleRHPress
void setup ()
{
LOWswitch.begin (LOW_SWITCH_PIN, handleLOWPress);
HIGHswitch.begin (HIGH_SWITCH_PIN, handleHIGHPress);
pinMode (LOW_RELAY, OUTPUT);
pinMode (HIGH_RELAY, OUTPUT);
pinMode (DOUBLE_RELAY, OUTPUT);
} // end of setup
unsigned long lastBlink;
bool onCycle;
void blinkLights ()
{
lastBlink = millis ();
// onCycle = !onCycle;
// default to off
digitalWrite (LOW_RELAY, HIGH);
digitalWrite (HIGH_RELAY, HIGH);
digitalWrite (DOUBLE_RELAY, HIGH);
// every second time, turn them all off
if (LOW_RELAY == LOW || HIGH_RELAY == LOW || DOUBLE_RELAY == LOW)
return;
// blink light
switch (state)
{
case NONE:
break;
case LOW_DOWN:
case LOW_RELAY_ON:
digitalWrite (LOW_RELAY, LOW);
break;
case HIGH_DOWN:
case HIGH_RELAY_ON:
digitalWrite (HIGH_RELAY, LOW);
break;
case BOTH:
digitalWrite (DOUBLE_RELAY, LOW);
break;
} // end of switch on state
} // end of blinkLights
void loop ()
{
LOWswitch.check (); // check for presses
HIGHswitch.check (); // check for presses
if (millis () - lastBlink >= BLINK_INTERVAL)
blinkLights ();
// other stuff
} // end of loop