const int buttonPins[] = {3, 4}; // Array containing the pin numbers for the pushbuttons
const int numButtons = sizeof(buttonPins) / sizeof(buttonPins[0]); // Number of buttons
const int strikePin = 7; // the number of the door strike control pin
// Variables
unsigned long lastActivationTime = 0; // time when the door strike was last activated
unsigned long lastLogTime = 0; // last time the status was logged
bool isStrikeActive = false; // current state of the door strike
void setup() {
// Initialize the LED pin as an output:
pinMode(LED_BUILTIN, OUTPUT);
// Initialize the door strike pin as an output:
pinMode(strikePin, OUTPUT);
// Initialize each button pin as an input with an internal pull-up resistor:
for (int i = 0; i < numButtons; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
// Ensure both the strike and LED are off initially
digitalWrite(strikePin, LOW);
digitalWrite(LED_BUILTIN, LOW);
// Start serial communication
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis(); // get current time
bool anyButtonPressed = false; // flag to track if any button is pressed
// Check each button's state
for (int i = 0; i < numButtons; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
anyButtonPressed = true;
break; // Exit the loop if any button is pressed
}
}
// Activate or maintain activation of the strike if any button is pressed
if (anyButtonPressed) {
if (!isStrikeActive) {
digitalWrite(strikePin, HIGH); // turn on the door strike
isStrikeActive = true; // mark the strike as active
lastActivationTime = currentMillis; // update the last activation time
Serial.println("Strike Activated");
}
digitalWrite(LED_BUILTIN, HIGH); // turn on LED to indicate button is pressed
} else {
if (isStrikeActive && (currentMillis - lastActivationTime >= 5000)) {
digitalWrite(strikePin, LOW); // turn off the door strike
isStrikeActive = false; // mark the strike as inactive
Serial.println("Strike Deactivated");
}
digitalWrite(LED_BUILTIN, LOW); // turn off LED
}
// Log status every 3 seconds
if (currentMillis - lastLogTime >= 3000) {
lastLogTime = currentMillis; // update the last log time
Serial.print("Time: ");
Serial.print(currentMillis);
Serial.print(" ms, Strike Status: ");
Serial.println(isStrikeActive ? "Active" : "Inactive");
}
delay(100); // Reduce delay to improve responsiveness
}