// // Pin definitions
// const int sensorClosedPin = 2; // Sensor detecting the closed position
// const int sensorOpenPin = 3; // Sensor detecting the open position
// const int buttonClosePin = 4; // Button for closing the door (unused in this update)
// const int buttonOpenPin = 5; // Button for opening the door (unused in this update)
// const int buttonStopPin = 6; // Button to stop the door (unused in this update)
// const int motorPin = 7; // Motor control pin (controls opening/closing)
// const int yellowLEDPin = 8; // Yellow LED for movement indication
// const int greenLEDPin = 9; // Green LED for door closed indication
// const int redLEDPin = 10; // Red LED for door open indication
// const int buzzerPin = 11; // Buzzer pin
// const int buttonControlPin = 12; // New control button for open/stop/close logic
// Pin definitions
const int sensorClosedPin = 2; // Sensor detecting the closed position
const int sensorOpenPin = 3; // Sensor detecting the open position
const int buttonControlPin = 12; // New control button for open/stop/close logic
const int relayPin = 9; // Relay control pin (replaces motorPin)
const int yellowLEDPin = 8; // Yellow LED for movement indication
const int greenLEDPin = 7; // Green LED for door closed indication
const int redLEDPin = 10; // Red LED for door open indication
const int buzzerPin = 11; // Buzzer pin
// Door states
enum DoorState { STOPPED, CLOSING, OPENING };
DoorState doorState = STOPPED;
// Timing variables
unsigned long previousMillis = 0; // To handle yellow LED blinking
unsigned long doorOpenMillis = 0; // To track when the door was fully opened
unsigned long buzzerMillis = 0; // To track time for buzzer sound
unsigned long buzzerStartMillis = 0; // To track when the buzzer started
const long blinkInterval = 500; // Interval for blinking (500ms)
const long buzzerInterval = 10000; // Buzzer interval (10 seconds)
const long buzzerSoundDuration = 1000; // Buzzer sound duration (1 second)
const long doorOpenDurationLimit = 300000; // 5 minutes in milliseconds (5 * 60 * 1000)
// Debounce variables
unsigned long lastDebounceTimeButton = 0; // To track debounce for control button
unsigned long debounceDelay = 50; // Debounce delay for buttons (50ms)
bool lastButtonState = HIGH; // To track the last stable state of the control button
bool buttonState = HIGH; // Current button state after debouncing
unsigned long lastDebounceTimeClosedSensor = 0; // Debounce for closed sensor
unsigned long lastDebounceTimeOpenSensor = 0; // Debounce for open sensor
bool lastSensorClosedState = HIGH; // Last stable state of closed sensor
bool lastSensorOpenState = HIGH; // Last stable state of open sensor
bool isClosed = false; // Current state of the closed sensor
bool isOpen = false; // Current state of the open sensor
// Buzzer status
bool isBuzzerOn = false;
bool doorIsMoving = false; // To track whether the door is currently moving
void setup() {
// Initialize input pins
pinMode(sensorClosedPin, INPUT);
pinMode(sensorOpenPin, INPUT);
pinMode(buttonControlPin, INPUT_PULLUP); // Control button for open/stop/close
// Initialize relay, LED, and buzzer pins
pinMode(relayPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Turn off relay at the start (door stopped)
stopRelay();
}
void loop() {
// Debounce the control button
debounceButton();
// Debounce the closed and open sensors
debounceSensors();
// Handle button press logic based on debounced state
handleButtonPress(buttonState);
// Check if the door is fully closed or open
if (isClosed && doorState == CLOSING) {
stopRelay();
}
if (isOpen && doorState == OPENING) {
stopRelay();
// Start tracking the time when the door is fully open
if (doorOpenMillis == 0) {
doorOpenMillis = millis(); // Start tracking when the door is open
}
} else {
// Reset door open timer if the door is closed or moving
doorOpenMillis = 0;
buzzerMillis = 0; // Reset buzzer timer
buzzerStartMillis = 0;
isBuzzerOn = false; // Make sure the buzzer is off when the door is closed
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}
// Handle LEDs
updateLEDs();
// Handle yellow LED blinking if the door is moving
if (doorState == CLOSING || doorState == OPENING) {
blinkYellowLED();
} else {
digitalWrite(yellowLEDPin, LOW); // Turn off yellow LED when stopped
}
// Handle buzzer if the door is open for more than 5 minutes
if (isOpen && millis() - doorOpenMillis >= doorOpenDurationLimit) {
manageBuzzer();
}
}
// Function to debounce the control button
void debounceButton() {
bool reading = digitalRead(buttonControlPin);
if (reading != lastButtonState) {
lastDebounceTimeButton = millis(); // Reset debounce timer
}
if ((millis() - lastDebounceTimeButton) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
}
}
lastButtonState = reading;
}
// Function to debounce the sensors
void debounceSensors() {
bool currentClosedSensor = digitalRead(sensorClosedPin);
bool currentOpenSensor = digitalRead(sensorOpenPin);
if (currentClosedSensor != lastSensorClosedState) {
lastDebounceTimeClosedSensor = millis(); // Reset debounce timer for closed sensor
}
if (currentOpenSensor != lastSensorOpenState) {
lastDebounceTimeOpenSensor = millis(); // Reset debounce timer for open sensor
}
if ((millis() - lastDebounceTimeClosedSensor) > debounceDelay) {
isClosed = currentClosedSensor;
}
if ((millis() - lastDebounceTimeOpenSensor) > debounceDelay) {
isOpen = currentOpenSensor;
}
lastSensorClosedState = currentClosedSensor;
lastSensorOpenState = currentOpenSensor;
}
// Function to handle the control button logic
void handleButtonPress(bool controlButtonPressed) {
if (controlButtonPressed == LOW && lastButtonState == HIGH) { // Button press detected (active low)
lastButtonState = LOW; // Debounce and prevent multiple presses
if (doorState == STOPPED) {
if (isClosed) {
openDoor(); // Open door if it's closed
} else if (isOpen) {
closeDoor(); // Close door if it's open
}
} else {
stopRelay(); // Stop the door if it's moving
}
}
if (controlButtonPressed == HIGH) {
lastButtonState = HIGH; // Reset the button state when released
}
}
// Function to close the door
void closeDoor() {
if (doorState != CLOSING) {
digitalWrite(relayPin, HIGH); // Turn relay on to close the door
doorState = CLOSING;
doorIsMoving = true;
}
}
// Function to open the door
void openDoor() {
if (doorState != OPENING) {
digitalWrite(relayPin, HIGH); // Turn relay on to open the door
doorState = OPENING;
doorIsMoving = true;
}
}
// Function to stop the relay
void stopRelay() {
digitalWrite(relayPin, LOW); // Turn relay off
doorState = STOPPED;
doorIsMoving = false;
}
// Function to blink the yellow LED when the door is moving
void blinkYellowLED() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= blinkInterval) {
previousMillis = currentMillis;
// If the LED is off, turn it on, and vice-versa
if (digitalRead(yellowLEDPin) == LOW) {
digitalWrite(yellowLEDPin, HIGH);
} else {
digitalWrite(yellowLEDPin, LOW);
}
}
}
// Function to update green and red LEDs based on door state
void updateLEDs() {
if (isClosed) {
digitalWrite(greenLEDPin, HIGH); // Green LED on if door is closed
digitalWrite(redLEDPin, LOW); // Red LED off
} else if (isOpen) {
digitalWrite(redLEDPin, HIGH); // Red LED on if door is open
digitalWrite(greenLEDPin, LOW); // Green LED off
} else {
// Door is neither fully closed nor fully open (in between)
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
}
}
// Function to manage the buzzer activation
void manageBuzzer() {
unsigned long currentMillis = millis();
// Check if it is time to activate the buzzer (every 10 seconds)
if (currentMillis - buzzerMillis >= buzzerInterval) {
buzzerMillis = currentMillis; // Update the buzzer timer
if (isBuzzerOn == false) {
isBuzzerOn = true;
buzzerStartMillis = millis();
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
}
}
// Turn off the buzzer after the sound duration (1 second)
if (isBuzzerOn && (currentMillis - buzzerStartMillis >= buzzerSoundDuration)) {
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
isBuzzerOn = false;
}
}
/*WOKWI JSON CODE
{
"version": 1,
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": -24, "left": -86.9, "attrs": {} },
{
"type": "wokwi-led",
"id": "yellowLED",
"top": -118.8,
"left": -197.8,
"attrs": { "color": "yellow" }
},
{ "type": "wokwi-led", "id": "greenLED", "top": -234, "left": -197.8, "attrs": {} },
{
"type": "wokwi-led",
"id": "redLED",
"top": -176.4,
"left": -197.8,
"attrs": { "color": "limegreen" }
},
{ "type": "wokwi-button", "id": "controlButton", "top": 150, "left": 150, "attrs": {} },
{ "type": "wokwi-piezo", "id": "buzzer", "top": 50, "left": 450, "attrs": {} },
{ "type": "wokwi-servo", "id": "motor", "top": -290, "left": 249.6, "attrs": {} },
{
"type": "wokwi-pushbutton",
"id": "sensorClosed",
"top": -329.8,
"left": 124.8,
"attrs": {}
},
{
"type": "wokwi-pushbutton",
"id": "sensorOpen",
"top": -483.4,
"left": 124.8,
"attrs": { "color": "blue" }
},
{ "type": "wokwi-slide-switch", "id": "sw1", "top": -322, "left": -179.3, "attrs": {} },
{ "type": "wokwi-slide-switch", "id": "sw2", "top": -322, "left": -102.5, "attrs": {} },
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": -276,
"left": -36.6,
"attrs": { "volume": "0.1" }
},
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": -176.2,
"left": 115.2,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": -406.6,
"left": 124.8,
"attrs": { "color": "green" }
}
],
"connections": [
[ "nano:12", "btn1:2.l", "green", [ "v0" ] ],
[ "nano:GND.2", "btn1:2.r", "black", [ "v-67.2", "h144" ] ],
[ "nano:11", "bz1:2", "green", [ "v-144", "h57.6" ] ],
[ "bz1:1", "nano:GND.2", "black", [ "v86.4", "h48" ] ],
[ "nano:10", "greenLED:A", "violet", [ "v0" ] ],
[ "nano:9", "redLED:A", "limegreen", [ "v0" ] ],
[ "nano:8", "yellowLED:A", "gold", [ "v0" ] ],
[ "redLED:C", "greenLED:C", "black", [ "v0", "h-28.4", "v0", "h0", "v0", "h0", "v-57.6" ] ],
[ "yellowLED:C", "redLED:C", "black", [ "v0", "h-28.4", "v-57.6" ] ],
[ "greenLED:C", "bz1:1", "black", [ "v9.6", "h154", "v-9.6" ] ],
[ "nano:7", "motor:PWM", "green", [ "v-38.4", "h268.8", "v-9.6" ] ],
[ "nano:5V", "motor:V+", "red", [ "v19.2", "h192", "v-288" ] ],
[ "nano:GND.2", "motor:GND", "black", [ "v0" ] ],
[ "nano:6", "sensorClosed:2.l", "purple", [ "v-57.6", "h96", "v-220.8" ] ],
[ "motor:GND", "sensorClosed:1.r", "black", [ "h0", "v-76.8" ] ],
[ "motor:GND", "btn2:1.r", "black", [ "h0", "v-153.6" ] ],
[ "nano:5", "btn2:2.l", "green", [ "v-134.4", "h57.6", "v-220.8", "h19.2" ] ],
[
"nano:4",
"sensorOpen:2.l",
"blue",
[ "v-115.2", "h57.6", "v-134.4", "h0", "v-182.4", "h57.6" ]
],
[ "motor:GND", "sensorOpen:1.r", "black", [ "h0", "v-230.4" ] ],
[ "sw2:2", "nano:2", "cyan", [ "v192", "h115.3" ] ],
[ "sw1:2", "nano:3", "orange", [ "v115.2", "h182.5" ] ],
[ "nano:3.3V", "sw2:1", "green", [ "v19.2", "h-182.4", "v-316.8", "h144" ] ],
[ "sw1:3", "sw2:1", "green", [ "v0" ] ],
[ "nano:GND.2", "sw2:3", "black", [ "v0" ] ],
[ "nano:GND.2", "sw1:1", "black", [ "v-249.6", "h-211.2" ] ]
],
"dependencies": {}
}
*/