// | Date 14-10-2023
// | Elevator 2 Switchs
// | By Samer Halawani [email protected]
// | ----------------------------------------
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include<LiquidCrystal.h>
// Liquid Crystal_I2C Setup
// MEGA (21 SCL) (20 SDA)
LiquidCrystal_I2C lcd(0x3F, 20, 4);
// First Floor & Second Floor Button / Status
const int FL1_BTN = A0; int FL1_BTN_ButtonState = 0; int FL1_BTN_lastButtonState = 0;
const int FL2_BTN = 13; int FL2_BTN_ButtonState = 0; int FL2_BTN_lastButtonState = 0;
// Up & Downt Button Relays
const int Relay_UP = 4; const int Relay_Down = 5;
// Switch 1 + 2 / Status
const int SW1 = 6; int SW1_State = 0;
const int SW2 = 7; int SW2_State = 0;
const int SW3 = 8; int SW3_State = 0;
// Led & buzzer Setup
const int Led1 = A1; int led1_State = LOW;
const int buzzer = A2; //buzzer to arduino pin 9
// Reset Position Button X - Y
const int RstP_BTN = A3; int RstP_BTN_State = 0; int RstP_BTN_lastButtonState = 0;
const long SW1_TimeOut = 500;
const long SW2_TimeOut = 1000;
int time1 = 0;
// constants won't change: LED & BUZZ
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 400; // interval at which to blink (milliseconds)
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
//lcd.noBacklight();
// Buttons
pinMode(FL1_BTN, INPUT);
pinMode(FL2_BTN, INPUT);
pinMode(RstP_BTN, INPUT);
// Relays
pinMode(Relay_UP, OUTPUT); pinMode(Relay_Down, OUTPUT);
// Switchs
pinMode(SW1, INPUT); pinMode(SW2, INPUT); pinMode(SW3, INPUT);
// LED & Buzzer
pinMode(Led1, OUTPUT); pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
Start();
}
void Start() {
time1 = 0;
SW1_State = digitalRead(SW1);
SW2_State = digitalRead(SW2);
// LCD INTERFACE العرض على الشاشة
// First Floor (1)
if ((SW1_State == HIGH) and (SW2_State == LOW)){
Serial.println(" Elevator Position (SW1)");
lcd.clear();
lcd.setCursor(0,0);
lcd.write(126);
lcd.print("(XEROTK)");
lcd.setCursor(0,1);
lcd.print("FLOOR (1)");
}
// Second Floor (2)
else if ((SW2_State == HIGH) and (SW1_State == LOW)){
Serial.println(" Elevator Position (SW2)");
lcd.clear();
lcd.setCursor(0,0);
lcd.write(126);
lcd.print("(XEROTK)");
lcd.setCursor(0,1);
lcd.print("FLOOR (2)");
}
else {
digitalWrite(Relay_UP,LOW);
digitalWrite(Relay_Down,LOW);
digitalWrite(Led1, HIGH);
Serial.println(" ERROR!! Elevator Position error.");
lcd.clear();
lcd.setCursor(0,0);
lcd.write(126);
lcd.print(" Switch Error!! ");
lcd.write(127);
lcd.setCursor(0,1);
lcd.print(" Check Position");
digitalWrite(Led1, HIGH);
}
}
void loop() {
FL1_BTN_ButtonState = digitalRead(FL1_BTN);
SW1_State = digitalRead(SW1);
FL2_BTN_ButtonState = digitalRead(FL2_BTN);
SW2_State = digitalRead(SW2);
RstP_BTN_State = digitalRead(RstP_BTN);
// == Elevator Start going UP level !
if ((SW1_State == HIGH) and (SW2_State == LOW)){
digitalWrite(Relay_Down,LOW);
if ((FL2_BTN_ButtonState != FL2_BTN_lastButtonState) and (SW2_State != HIGH)) {
// If FL2_BTN Pushed
if (FL2_BTN_ButtonState == HIGH){
GoUP();
}
delay(10);
}
FL2_BTN_lastButtonState = FL2_BTN_ButtonState;
}
// ## END Elevator Start going UP level
// == Elevator Start going DOWN level !
else if ((SW2_State == HIGH) and (SW1_State == LOW)){
//delay(180); // توقيت الوقوق غتد السطح المستوي
digitalWrite(Relay_UP,LOW); // ايقاف المحرك عند الوصول الى الاسفل
if ((FL1_BTN_ButtonState != FL1_BTN_lastButtonState) and (SW1 != HIGH)) {
//Serial.println(" Start Moving From FLOOR (2)");
// If FL1_BTN Pushed
if (FL1_BTN_ButtonState == HIGH){
GoDown();
}
delay(10);
}
FL1_BTN_lastButtonState == FL1_BTN_ButtonState;
}
// ## END Elevator Start going DOWN level
// == RST Button Pushed !
else {
if (RstP_BTN_State == HIGH){
Serial.println(" RST Button Pushed");
Serial.println(" Searching Position");
GoDown();
}
delay(10);
}
RstP_BTN_lastButtonState == RstP_BTN_State;
}
// Go Up Section
void GoUP() {
digitalWrite(Relay_UP,HIGH);
Serial.println(" GoUP() Elevator Going Up SW1 ...");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Elevator going up ...");
lcd.setCursor(15,1);
lcd.write(126);
while (SW2_State == LOW){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (led1_State == LOW) {
led1_State = HIGH;
tone(buzzer, 1000);
}
else {
led1_State = LOW;
noTone(buzzer);
}
digitalWrite(Led1, led1_State);
}
SW2_State = digitalRead(SW2);
if(SW2_State == LOW){
time1++;
if (time1 >= SW2_TimeOut){
Serial.print(" SW2 Time Out:");
Serial.print(time1/100);
Serial.println("sec");
noTone(buzzer);
delay(10);
time1 = 0;
Start();
break;
}
delay(10);
} else {
digitalWrite(Led1, LOW);
noTone(buzzer);
delay(15);
time1 = 0;
Start();
}
}
}
// Go Down Section
void GoDown() {
digitalWrite(Relay_Down,HIGH);
Serial.println(" GoDown() Elevator Going Down SW2 ...");
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Elevator Going Down ...");
lcd.setCursor(15,1);
lcd.write(127);
while (SW1_State == LOW){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (led1_State == LOW) {
led1_State = HIGH;
tone(buzzer, 2000);
} else {
led1_State = LOW;
noTone(buzzer);
}
digitalWrite(Led1, led1_State);
}
SW1_State = digitalRead(SW1);
if(SW1_State == LOW){
time1++;
if (time1 >= SW1_TimeOut){
Serial.print(" SW1 Time Out:");
Serial.print(time1/100);
Serial.println("sec");
noTone(buzzer);
delay(10);
time1 = 0;
Start();
break;
}
delay(10);
} else {
digitalWrite(Led1, LOW);
noTone(buzzer);
delay(10);
time1 = 0;
Start();
}
}
}