const int momentarySwitchPin = 6; // Arduino pin connected to button's pin
const int latchingSwitchInputPin = 6;
const int latchingSwitchOutputPin = 3; // Arduino pin connected to relay's pin
// Variables to store switch states
int switchState = LOW;
int lastSwitchState = LOW;
int latchingSwitchState = LOW;
void setup() {
// initialize serial
pinMode(momentarySwitchPin, INPUT); // Set the momentary switch pin as INPUT
// Set the latching switch input and output pins as INPUT and OUTPUT, respectively
pinMode(latchingSwitchInputPin, INPUT);
pinMode(latchingSwitchOutputPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// Read the state of the momentary switch
switchState = digitalRead(momentarySwitchPin);
// Check for a rising edge (transition from LOW to HIGH) on the momentary switch
if (switchState == HIGH && lastSwitchState == LOW) {
// Toggle the state of the latching switch
latchingSwitchState = !latchingSwitchState;
delay(1000);
digitalWrite(latchingSwitchOutputPin, latchingSwitchState);
Serial.println("Toggle Latching Switch");
}
// Update the last switch state
lastSwitchState = switchState;
}