// Pin configuration
const int statePin = 2; // Input pin from green/red wire
const int relayA = 8; // Output pin to drive lock
const int relayB = 9; // Output pin to drive unlock
// Debounce and edge detection
bool lastState = HIGH; // Initially assume locked
unsigned long lastActionTime = 0;
bool actionInProgress = false;
void setup() {
pinMode(statePin, INPUT_PULLUP); // Internal pull-up to detect ground
pinMode(relayA, OUTPUT);
pinMode(relayB, OUTPUT);
digitalWrite(relayA, LOW);
digitalWrite(relayB, LOW);
}
void loop() {
bool currentState = digitalRead(statePin); // LOW = unlocked, HIGH = locked
// Detect state change
if (currentState != lastState && !actionInProgress) {
actionInProgress = true;
lastActionTime = millis();
if (currentState == LOW) {
// Central locking says "unlock"
digitalWrite(relayB, HIGH); // Reverse polarity
digitalWrite(relayA, LOW);
} else {
// Central locking says "lock"
digitalWrite(relayA, HIGH); // Forward polarity
digitalWrite(relayB, LOW);
}
lastState = currentState;
}
// Timeout after 2 seconds
if (actionInProgress && (millis() - lastActionTime > 2000)) {
digitalWrite(relayA, LOW);
digitalWrite(relayB, LOW);
actionInProgress = false;
}
}