// Define the pins connected to the relay module
const int relayPinA = 9; // Pin for coil A of the latching relay
const int relayPinB = 10; // Pin for coil B of the latching relay
// Delay time for the off state (in milliseconds)
unsigned long offDelay = 5000; // 5000 milliseconds = 5 seconds
// State variables
bool relayState = false; // Current state of the relay (OFF initially)
unsigned long lastToggleTime = 0; // Time when relay was last toggled
void setup() {
// Initialize relay pins as outputs
pinMode(relayPinA, OUTPUT);
pinMode(relayPinB, OUTPUT);
// Initially turn off the relay
digitalWrite(relayPinA, LOW);
digitalWrite(relayPinB, LOW);
}
void loop() {
unsigned long loDelay = 0;
unsigned int oDelay = analogRead(1);
offDelay = map(oDelay,0,1023,500,5000);
if(offDelay != loDelay){
Serial.println(oDelay);
loDelay = offDelay;
}
// Check if it's time to change the relay state
unsigned long currentTime = millis();
if (currentTime - lastToggleTime >= offDelay) {
// Toggle the relay state
toggleRelay();
// Update the last toggle time
lastToggleTime = currentTime;
}
// Other tasks can be performed here
}
// Function to toggle the relay state
void toggleRelay() {
relayState = !relayState; // Toggle the state
if (relayState) {
// Turn on the relay (set coil A HIGH, coil B LOW)
digitalWrite(relayPinA, HIGH);
digitalWrite(relayPinB, LOW);
} else {
// Turn off the relay (set coil A LOW, coil B HIGH)
digitalWrite(relayPinA, LOW);
digitalWrite(relayPinB, HIGH);
}
}