/*
A Door
https://forum.arduino.cc/t/garage-door-opener-with-esp8266-web-server/1154364/
*/
enum State {OPENED, CLOSING, CLOSED, OPENING};
State state = CLOSED;
const uint8_t output5 = 5;
unsigned long lastTimeDoorWasActivated = 0;
const unsigned long doorDelay = 12000;
const uint8_t input = 4;
/*
OPENED green the garage door is open
CLOSING red please wait...the garage door is closing "this should display for approx 12 sceconds"
CLOSED red the garage door is closed
OPENING green please wait... the garage door is opening "this should display for approx 12 sceconds"
*/
// this will become later part of the server code:
void helper() {
switch (state) {
case OPENED :
Serial.print("green: ");
Serial.println("the garage door is open");
break;
case CLOSING:
Serial.print("red: ");
Serial.println("please wait... the garage door is closing");
break;
case CLOSED:
Serial.print("red: ");
Serial.println("the garage door is closed");
break;
case OPENING:
Serial.print("green: ");
Serial.println("please wait... the garage door is opening");
break;
}
}
void runFSM() {
switch (state) {
case OPENED :
if (digitalRead(input) == LOW) {
lastTimeDoorWasActivated = millis();
digitalWrite (output5, HIGH);
state = CLOSING;
Serial.println(F("state CLOSING"));
helper();
}
break;
case CLOSING :
if (millis() - lastTimeDoorWasActivated > doorDelay) {
digitalWrite (output5, LOW);
state = CLOSED;
Serial.println(F("state CLOSED"));
helper();
}
break;
case CLOSED :
if (digitalRead(input) == LOW) {
lastTimeDoorWasActivated = millis();
digitalWrite (output5, HIGH);
state = OPENING;
Serial.println(F("state OPENING"));
helper();
}
break;
case OPENING :
if (millis() - lastTimeDoorWasActivated > doorDelay) {
digitalWrite (output5, LOW);
state = OPENED;
Serial.println(F("state OPENED"));
helper();
}
break;
}
}
void setup() {
Serial.begin(115200);
pinMode (output5, OUTPUT);
pinMode (input, INPUT_PULLUP);
}
void loop() {
runFSM();
}