This sketch toggle both relay at once from both buttons.
const int latchPin = 10;
const int clockPin = 9;
const int dataPin = 12;
const int OE = 11;
byte leds = 0;
const uint32_t debounceTime = 50; // 5 mSec, enough for most switches
const uint8_t switchPin1 = A0; // with n.o. momentary pb switch to ground
const uint8_t switchPin2 = 5; // with n.o. momentary pb switch to ground
#define ledPin1 13
const bool switchOn = true; // with capasitive touch
const bool switchOff = false;
bool lastState1 = switchOff;
bool newState1 = switchOff;
bool toggleState1 = false;
bool lastState2 = switchOff;
bool newState2 = switchOff;
bool toggleState2 = false;
void setup()
{
Serial.begin(9600);
pinMode(OE, OUTPUT);
digitalWrite(OE, LOW);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode ( switchPin1, INPUT_PULLUP );
pinMode ( switchPin2, INPUT_PULLUP );
pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1, LOW);
}
void loop()
{
newState1 = digitalRead( switchPin1 );
newState2 = digitalRead( switchPin2 );
if ( lastState1 != newState1 ) // state changed
{
delay( debounceTime );
lastState1 = newState1;
// push on, push off
if ( newState1 == switchOn && toggleState1 == false )
{
toggleState1 = true;
leds ^= B00000001;
updateShiftRegister();
}
else if ( newState1 == switchOn && toggleState1 == true )
{
toggleState1 = false;
leds ^= B00000001;
updateShiftRegister();
}
}
if ( lastState2 != newState2 ) // state changed
{
delay( debounceTime );
lastState2 = newState2;
// push on, push off
if ( newState2 == switchOn && toggleState2 == false )
{
toggleState2 = true;
leds ^= B00000010;
updateShiftRegister();
}
else if ( newState2 == switchOn && toggleState2 == true )
{
toggleState2 = false;
leds ^= B00000010;
updateShiftRegister();
}
}
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}