/*
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 blueLEDPin = 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;
const int relayOpenPin = 13; // Relay control pin for opening the door
const int relayClosePin = 14; // Relay control pin for closing the door
const int relayStopPin = 15; // Relay control pin to stop the door
DOOR_CLOSED state means:
sensorClosedPin = LOW
sensorOpenPin = Ignoring,
DOOR_OPEN state means:
sensorClosedPin = Ignoring
sensorOpenPin = LOW,
DOOR_MOVING state means:
sensorClosedPin = HIGH
sensorOpenPin = HIGH
*/
// Pin definitions
const int sensorClosedPin = 2;
const int sensorOpenPin = 3;
const int buttonClosePin = 4;
const int buttonOpenPin = 5;
const int buttonStopPin = 6;
const int blueLEDPin = 7;
const int yellowLEDPin = 8;
const int greenLEDPin = 9;
const int redLEDPin = 10;
const int buzzerPin = 11;
const int relayOpenPin = 13;
const int relayClosePin = 14;
const int relayStopPin = 15;
// Time definitions
unsigned long previousMillis = 0;
unsigned long movingStartTime = 0;
const unsigned long doorMovingTimeout = 60000; // 1 minute
const unsigned long redLEDBlinkOnTime = 500;
const unsigned long redLEDBlinkOffTime = 200;
const unsigned long buzzerBeepTime = 1000; // 1 second buzzer ON
const unsigned long buzzerSilentTime = 3000; // 3 second silence
// Door states
enum DoorState { DOOR_CLOSED, DOOR_OPEN, DOOR_MOVING, STOP_EMERGENCY };
DoorState currentState = DOOR_CLOSED;
// Timing variables
bool yellowLEDBlinkState = false;
bool redLEDBlinkState = false;
unsigned long yellowLEDBlinkMillis = 0;
unsigned long redLEDBlinkMillis = 0;
unsigned long buzzerMillis = 0;
bool buzzerState = false;
void setup() {
Serial.begin(9600); // Start Serial Monitor
pinMode(sensorClosedPin, INPUT_PULLUP);
pinMode(sensorOpenPin, INPUT_PULLUP);
pinMode(buttonClosePin, INPUT_PULLUP);
pinMode(buttonOpenPin, INPUT_PULLUP);
pinMode(buttonStopPin, INPUT_PULLUP);
pinMode(blueLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(relayOpenPin, OUTPUT);
pinMode(relayClosePin, OUTPUT);
pinMode(relayStopPin, OUTPUT);
// Initial state setup
digitalWrite(relayOpenPin, LOW);
digitalWrite(relayClosePin, LOW);
digitalWrite(relayStopPin, LOW);
digitalWrite(blueLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, HIGH); // Start with door closed
digitalWrite(redLEDPin, LOW);
digitalWrite(buzzerPin, LOW);
}
void loop() {
// Check if emergency stop button is pressed
if (digitalRead(buttonStopPin) == LOW) {
enterStopEmergencyState();
}
// Exit emergency stop state if buttonClosePin or buttonOpenPin is pressed
if (currentState == STOP_EMERGENCY) {
if (digitalRead(buttonClosePin) == LOW || digitalRead(buttonOpenPin) == LOW) {
exitStopEmergencyState();
}
} else {
handleNormalOperation();
}
// Handle LED blinking in emergency state
if (currentState == STOP_EMERGENCY) {
handleRedLEDBlinking();
}
// Handle door moving timeout and buzzer logic
if (currentState == DOOR_MOVING) {
handleDoorMovingTimeout();
handleYellowLEDBlinking();
}
}
void enterStopEmergencyState() {
currentState = STOP_EMERGENCY;
digitalWrite(relayOpenPin, LOW);
digitalWrite(relayClosePin, LOW);
digitalWrite(relayStopPin, HIGH); // Stop the door
digitalWrite(blueLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(buzzerPin, LOW); // Turn off buzzer if it was on
Serial.println("Entering STOP_EMERGENCY state");
}
void exitStopEmergencyState() {
currentState = DOOR_CLOSED; // Reset to DOOR_CLOSED state
digitalWrite(relayStopPin, LOW); // Turn off stop relay
digitalWrite(redLEDPin, LOW); // Stop red LED blinking
digitalWrite(greenLEDPin, HIGH); // Green LED for door closed
Serial.println("Exiting STOP_EMERGENCY state");
}
void handleNormalOperation() {
int sensorClosedState = digitalRead(sensorClosedPin);
int sensorOpenState = digitalRead(sensorOpenPin);
if (digitalRead(buttonOpenPin) == LOW && currentState != DOOR_OPEN) {
openDoor();
Serial.println("Opening door");
} else if (digitalRead(buttonClosePin) == LOW && currentState != DOOR_CLOSED) {
closeDoor();
Serial.println("Closing door");
}
if (sensorClosedState == LOW) {
// Door is closed
if (currentState != DOOR_CLOSED) {
currentState = DOOR_CLOSED;
digitalWrite(greenLEDPin, HIGH);
digitalWrite(blueLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
Serial.println("Door is now CLOSED");
}
digitalWrite(buzzerPin, LOW);
} else if (sensorOpenState == LOW) {
// Door is open
if (currentState != DOOR_OPEN) {
currentState = DOOR_OPEN;
digitalWrite(blueLEDPin, HIGH);
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
Serial.println("Door is now OPEN");
}
digitalWrite(buzzerPin, LOW);
} else {
// Door is moving
if (currentState != DOOR_MOVING) {
currentState = DOOR_MOVING;
digitalWrite(greenLEDPin, LOW);
digitalWrite(blueLEDPin, LOW);
digitalWrite(buzzerPin, LOW);
movingStartTime = millis(); // Start timing the moving duration
Serial.println("Door is MOVING");
}
}
}
void openDoor() {
digitalWrite(relayOpenPin, HIGH);
digitalWrite(relayClosePin, LOW);
}
void closeDoor() {
digitalWrite(relayClosePin, HIGH);
digitalWrite(relayOpenPin, LOW);
}
void handleYellowLEDBlinking() {
unsigned long currentMillis = millis();
if (currentMillis - yellowLEDBlinkMillis >= 500) {
yellowLEDBlinkMillis = currentMillis;
yellowLEDBlinkState = !yellowLEDBlinkState;
digitalWrite(yellowLEDPin, yellowLEDBlinkState ? HIGH : LOW);
}
}
void handleRedLEDBlinking() {
unsigned long currentMillis = millis();
if (redLEDBlinkState) {
if (currentMillis - redLEDBlinkMillis >= redLEDBlinkOnTime) {
redLEDBlinkMillis = currentMillis;
redLEDBlinkState = false;
digitalWrite(redLEDPin, LOW);
}
} else {
if (currentMillis - redLEDBlinkMillis >= redLEDBlinkOffTime) {
redLEDBlinkMillis = currentMillis;
redLEDBlinkState = true;
digitalWrite(redLEDPin, HIGH);
}
}
}
void handleDoorMovingTimeout() {
unsigned long currentMillis = millis();
if (currentMillis - movingStartTime >= doorMovingTimeout) {
// If the door has been moving for more than 1 minute, activate buzzer
if (currentMillis - buzzerMillis >= (buzzerState ? buzzerBeepTime : buzzerSilentTime)) {
buzzerMillis = currentMillis;
buzzerState = !buzzerState;
digitalWrite(buzzerPin, buzzerState ? HIGH : LOW);
Serial.println(buzzerState ? "Buzzer ON" : "Buzzer OFF");
}
}
}
/*
{
"version": 1,
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": -24, "left": -86.9, "attrs": {} },
{
"type": "wokwi-led",
"id": "yellowLED",
"top": -90,
"left": -197.8,
"attrs": { "color": "yellow" }
},
{ "type": "wokwi-led", "id": "greenLED", "top": -195.6, "left": -197.8, "attrs": {} },
{
"type": "wokwi-led",
"id": "redLED",
"top": -138,
"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-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" }
},
{ "type": "wokwi-relay-module", "id": "relay2", "top": 38.6, "left": 259.2, "attrs": {} },
{ "type": "wokwi-relay-module", "id": "relay3", "top": 144.2, "left": 259.2, "attrs": {} },
{ "type": "wokwi-relay-module", "id": "relay4", "top": 249.8, "left": 259.2, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": -253.2,
"left": -197.8,
"attrs": { "color": "blue" }
}
],
"connections": [
[ "nano:12", "btn1:2.l", "green", [ "v0" ] ],
[ "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: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:6", "sensorClosed:2.l", "purple", [ "v-57.6", "h96", "v-220.8" ] ],
[ "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" ]
],
[ "sw2:2", "nano:2", "cyan", [ "v192", "h115.3" ] ],
[ "sw1:2", "nano:3", "orange", [ "v115.2", "h182.5" ] ],
[ "sw1:3", "sw2:1", "red", [ "v0" ] ],
[ "nano:GND.2", "sw2:3", "black", [ "v0" ] ],
[ "nano:GND.2", "sw1:1", "black", [ "v-249.6", "h-211.2" ] ],
[ "nano:GND.2", "sensorClosed:1.r", "black", [ "v-48", "h172.8", "v-249.6" ] ],
[ "nano:GND.2", "btn1:1.r", "black", [ "v-86.4", "h163.2", "v-57.6" ] ],
[ "nano:GND.2", "btn2:1.r", "black", [ "v-48", "h172.8", "v-326.4" ] ],
[ "nano:GND.2", "sensorOpen:1.r", "black", [ "v-48", "h172.8", "v-403.2" ] ],
[ "nano:5V", "sw1:3", "red", [ "v19.2", "h-297.6", "v-336", "h105.4" ] ],
[ "nano:9", "redLED:A", "green", [ "v0" ] ],
[ "relay2:VCC", "relay3:VCC", "red", [ "h-28.8", "v105.6" ] ],
[ "relay3:VCC", "relay4:VCC", "red", [ "h-28.8", "v105.6" ] ],
[ "relay3:GND", "relay4:GND", "black", [ "h-48", "v105.2" ] ],
[ "relay3:GND", "relay2:GND", "black", [ "h-19.2", "v-106" ] ],
[ "nano:13", "relay2:IN", "green", [ "v0" ] ],
[ "nano:A0", "relay3:IN", "green", [ "v0" ] ],
[ "nano:A1", "relay4:IN", "green", [ "v0" ] ],
[ "nano:7", "led1:A", "green", [ "v-86.4", "h-19.2", "v-105.6" ] ],
[ "bz1:1", "led1:C", "black", [ "v-9.6", "h-172.8" ] ],
[ "nano:GND.1", "relay2:GND", "black", [ "v0" ] ]
],
"dependencies": {}
}
*/