#include <SPI.h>
#include <Wire.h>
#include <GyverOLED.h>
GyverOLED<SSH1106_128x64> oled;
// Version
long int Version = 10012024;
// First Floor & Second Floor Button / Status
const int FL1_BTN = A0;
int FL1_ButtonState = 0;
int FL1_lastButtonState = 0;
const int FL2_BTN = 13;
int FL2_ButtonState = 0;
int FL2_lastButtonState = 0;
// Up & Downt Button Relays
const int Relay_UP = 2;
const int Relay_Down = 3;
// Switch 1 + 2 / Status
const int SW1 = 4;
int SW1_ButtonState = 0;
const int SW2 = 5;
int SW2_ButtonState = 0;
const int SW3 = 6;
int SW3_ButtonState = 0;
// Led & buzzer Setup
const int Led1 = A2;
int led1_State = LOW;
const int buzzer = A3; //buzzer to arduino pin 9
//const int buzzer = 9; //buzzer to arduino pin 9
// Reset Position Button X - Y
const int RstP_BTN = A1;
int RstP_ButtonState = 0;
int RstP_lastButtonState = 0;
// Test Position Button X - Y
const int TestP_BTN = 12;
int TestP_ButtonState = 0;
int TestP_lastButtonState = 0;
// توقيت ايقاف المصعد اذا وصل الى الوقت لمدخل
// 1000 = 15sec, 2000 = 25sec ...etc
const long SW1_TimeOut = 2000;
const long SW2_TimeOut = 2170;
int time1 = 0;
//long arrow;
//const long arrowtime_Down = ((SW1_TimeOut / 75));
// 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);
Serial.println(" System Booting Up In Progress");
oled.init();
oled.clear();
oled.update();
// Buttons
pinMode(FL1_BTN, INPUT);
pinMode(FL2_BTN, INPUT);
pinMode(RstP_BTN, INPUT);
pinMode(TestP_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
Boot();
Start();
}
void Start() {
time1 = 0;
SW1_ButtonState = digitalRead(SW1);
SW2_ButtonState = digitalRead(SW2);
// LCD INTERFACE العرض على الشاشة
// First Floor (1)
if (( SW1_ButtonState == HIGH ) and ( SW2_ButtonState == LOW )){
Serial.println(" Elevator Position (SW1) F1 ");
oled.clear();
oled.setScale(4);
oled.setCursor(10, 1);
oled.println("> F1");
oled.setScale(1);
oled.setCursor(0, 7);
oled.println("Xerotk Tech");
oled.update();
}
// Second Floor (2)
else if (( SW2_ButtonState == HIGH ) and ( SW1_ButtonState == LOW )){
Serial.println(" Elevator Position (SW2) F2");
oled.clear();
oled.setScale(4);
oled.setCursor(10, 1);
oled.println("> F2");
oled.setScale(1);
oled.setCursor(0, 7);
oled.println("Xerotk Tech");
oled.update();
}
else {
digitalWrite(Relay_UP,LOW);
digitalWrite(Relay_Down,LOW);
digitalWrite(Led1, HIGH);
Serial.println(" ERROR!! Elevator Position ERROR!!");
oled.clear();
oled.setScale(2);
oled.setCursor(3, 1);
oled.println("Switch");
oled.setCursor(40, 4);
oled.println("Error!");
oled.setScale(1);
oled.setCursor(40, 6);
oled.println("Check Position");
oled.update();
}
}
void loop() {
FL1_ButtonState = digitalRead(FL1_BTN);
SW1_ButtonState = digitalRead(SW1);
FL2_ButtonState = digitalRead(FL2_BTN);
SW2_ButtonState = digitalRead(SW2);
RstP_ButtonState = digitalRead(RstP_BTN);
TestP_ButtonState = digitalRead(TestP_BTN);
// == Elevator Start going UP level !
if ((SW1_ButtonState == HIGH) and (SW2_ButtonState == LOW )){
digitalWrite(Relay_Down,LOW);
if (( FL2_ButtonState != FL2_lastButtonState ) and ( SW2_ButtonState == LOW )) {
// If FL2 Pushed
if ( FL2_ButtonState == HIGH ){
GoUP();
}
delay(10);
}
FL2_lastButtonState = FL2_ButtonState;
}
// ## END Elevator Start going UP level
else if (( SW2_ButtonState == HIGH ) and ( SW1_ButtonState == LOW )){
digitalWrite(Relay_UP,LOW); // ايقاف المحرك عند الوصول الى الاسفل
if ((FL1_ButtonState != FL1_lastButtonState) and (SW1 != HIGH)) {
// If FL1_BTN Pushed
if (FL1_ButtonState == HIGH){
GoDown();
}
delay(10);
}
FL1_lastButtonState == FL1_ButtonState;
}
// ## END Elevator Start going DOWN level
// == RST Button Pushed !
else {
if (RstP_ButtonState == HIGH){
Serial.println(" RST Button Pushed");
Serial.println(" Searching For Position...");
oled.clear();
oled.setScale(2);
oled.setCursor(1, 1);
oled.println("Reset");
oled.setCursor(18, 4);
oled.println("Position");
oled.setScale(1);
oled.setCursor(18, 6);
oled.println("Searching Position");
oled.update();
delay(5000);
GoDown();
delay(10); //++
}
RstP_lastButtonState == RstP_ButtonState; //++
}
//RstP_lastButtonState == RstP_ButtonState;
//TestP_lastButtonState == TestP_ButtonState;
if (TestP_ButtonState == HIGH){
Serial.println(" Runing Test");
Serial.println(" TESTING MOVEMENT...");
oled.clear();
oled.setScale(2);
oled.setCursor(1, 1);
oled.println("Runing");
oled.setCursor(18, 4);
oled.println("Test");
oled.setScale(1);
oled.setCursor(18, 6);
oled.println("TESTING MOVEMENT");
oled.update();
delay(10); //++
TestP();
}
TestP_lastButtonState == TestP_ButtonState; //++
}
// Go Up Section
void GoUP() {
Serial.println(" GoUP() Elevator Going Up From (SW1) ...");
digitalWrite(Relay_UP,HIGH);
oled.clear();
oled.setScale(3);
oled.setCursor(60, 2);
oled.println("Up");
oled.setScale(1);
oled.setCursor(0, 7);
oled.println("Xerotk Tech");
while (SW2_ButtonState == LOW){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (led1_State == LOW) {
led1_State = HIGH;
tone(buzzer, 2000);
oled.update();
}
else {
oled.update();
led1_State = LOW;
noTone(buzzer);
}
digitalWrite(Led1, led1_State);
}
SW2_ButtonState = digitalRead(SW2);
if(SW2_ButtonState == LOW){
time1++;
if ((time1) >= (SW2_TimeOut )){
digitalWrite(Relay_UP,LOW);
digitalWrite(Relay_Down,LOW);
Serial.print(" Time Out:");
Serial.print((time1/100));
Serial.println("sec");
oled.clear();
oled.setScale(2);
oled.setCursor(10, 1);
oled.println("Time Out:");
oled.setCursor(34, 4);
oled.print((time1/100));
oled.println("sec");
oled.update();
//tone(buzzer, 2000);
noTone(buzzer);
digitalWrite(Led1, LOW);
delay(6000);
time1 = 0;
Start();
break;
}
delay(10);
} else {
noTone(buzzer);
digitalWrite(Led1, LOW);
delay(10);
time1 = 0;
Start();
}
}
}
// Go Down Section
void GoDown() {
Serial.println(" GoDown() Elevator Going Down From (SW2) ...");
digitalWrite(Relay_Down,HIGH);
oled.clear();
oled.setScale(3);
oled.setCursor(25, 2);
oled.println("Down");
oled.setScale(1);
oled.setCursor(0, 7);
oled.println("Xerotk Tech");
while (SW1_ButtonState == LOW){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (led1_State == LOW) {
led1_State = HIGH;
tone(buzzer, 2000);
oled.update();
}
else {
oled.rect(100, 25, 130, 40, OLED_CLEAR);
oled.rect(95, 40, 125, 55, OLED_CLEAR);
oled.update();
led1_State = LOW;
noTone(buzzer);
}
digitalWrite(Led1, led1_State);
}
SW1_ButtonState = digitalRead(SW1);
if(SW1_ButtonState == LOW){
time1++;
if ((time1 ) >= SW1_TimeOut){
digitalWrite(Relay_UP,LOW);
digitalWrite(Relay_Down,LOW);
Serial.print(" Time Out:");
Serial.print((time1/100));
Serial.println("sec");
oled.clear();
oled.setScale(2);
oled.setCursor(10, 1);
oled.println("Time Out:");
oled.setCursor(34, 4);
oled.print((time1/100));
oled.println("sec");
oled.update();
//tone(buzzer, 2000);
noTone(buzzer);
digitalWrite(Led1, LOW);
delay(6000);
time1 = 0;
Start();
break;
}
delay(10);
} else {
noTone(buzzer);
digitalWrite(Led1, LOW);
delay(10);
time1 = 0;
Start();
}
} // END while
}
void Boot(void) {
oled.clear();
oled.setScale(1);
oled.setCursor(42, 2);
oled.println("Version ");
oled.setScale(1);
oled.setCursor(40, 4);
oled.println(Version);;
oled.update();
delay(2000);
}
void TestP(void) {
Serial.println(" Start Live Switch Tester");
if ((SW1_ButtonState == HIGH) and (SW2_ButtonState == LOW )){
Serial.println(" OK (SW1 HIGH) DETECTED");
Serial.println(" Moving to SW2...");
digitalWrite(Relay_Down,LOW);
digitalWrite(Relay_UP,HIGH);
oled.clear();
oled.setScale(1);
oled.setCursor(1, 1);
oled.println("OK !!");
oled.setCursor(1, 3);
oled.println("SW1 DETECTED");
oled.setCursor(1, 5);
oled.println("Moving to SW2");
oled.setScale(1);
oled.setCursor(0, 7);
oled.println("Xerotk Tech");
while (SW2_ButtonState == LOW){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (led1_State == LOW) {
led1_State = HIGH;
tone(buzzer, 2000);
oled.update();
}
else {
oled.rect(100, 10, 130, 25, OLED_CLEAR);
oled.rect(100, 25, 130, 40, OLED_CLEAR);
oled.update();
led1_State = LOW;
noTone(buzzer);
}
digitalWrite(Led1, led1_State);
}
SW2_ButtonState = digitalRead(SW2);
if (SW2_ButtonState == LOW){
time1++;
if ((time1) >= (SW2_TimeOut )) {
digitalWrite(Relay_UP,LOW);
//digitalWrite(Relay_Down,LOW);
Serial.print(" Time Out:");
Serial.print((time1/100));
Serial.println("sec");
oled.clear();
oled.setScale(2);
oled.setCursor(10, 1);
oled.println("Time Out:");
oled.setCursor(34, 4);
oled.print((time1/100));
oled.println("sec");
oled.update();
//tone(buzzer, 2000);
noTone(buzzer);
digitalWrite(Led1, LOW);
delay(6000);
time1 = 0;
Start();
break;
}
delay(10);
} else {
digitalWrite(Relay_UP,LOW);
oled.clear();
oled.setScale(2);
oled.setCursor(1, 1);
oled.println("OK !!");
oled.setCursor(1, 3);
oled.println("SW1 PASS ...");
oled.setCursor(1, 5);
oled.println("SW2 PASS ...");
oled.setScale(1);
oled.setCursor(0, 7);
oled.println("Xerotk Tech");
oled.update();
noTone(buzzer);
digitalWrite(Led1, LOW);
delay(10);
time1 = 0;
delay(6000);
oled.clear();
oled.setScale(2);
oled.setCursor(1, 1);
oled.println("OK !!");
oled.setCursor(1, 5);
oled.println("I'm backing to SW1");
oled.update();
delay(6000);
GoDown();
}
} //end while
}
else if ((SW2_ButtonState == HIGH) and (SW1_ButtonState == LOW )){
Serial.println(" OK (SW2 HIGH) DETECTED");
digitalWrite(Relay_Down,HIGH);
digitalWrite(Relay_UP,LOW);
oled.clear();
oled.setScale(2);
oled.setCursor(1, 2);
oled.println("OK !!");
oled.setCursor(1, 4);
oled.println("SW2 DETECTED");
oled.setScale(1);
oled.setCursor(0, 7);
oled.println("Xerotk Tech");
while (SW1_ButtonState == LOW){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (led1_State == LOW) {
led1_State = HIGH;
tone(buzzer, 2000);
oled.update();
}
else {
oled.rect(100, 25, 130, 40, OLED_CLEAR);
oled.rect(95, 40, 125, 55, OLED_CLEAR);
oled.update();
led1_State = LOW;
noTone(buzzer);
}
digitalWrite(Led1, led1_State);
}
SW1_ButtonState = digitalRead(SW1);
if(SW1_ButtonState == LOW){
time1++;
if ((time1 ) >= SW1_TimeOut){
digitalWrite(Relay_UP,LOW);
digitalWrite(Relay_Down,LOW);
Serial.print(" Time Out:");
Serial.print((time1/100));
Serial.println("sec");
oled.clear();
oled.setScale(2);
oled.setCursor(10, 1);
oled.println("Time Out:");
oled.setCursor(34, 4);
oled.print((time1/100));
oled.println("sec");
oled.update();
//tone(buzzer, 2000);
noTone(buzzer);
digitalWrite(Led1, LOW);
delay(6000);
time1 = 0;
Start();
break;
}
delay(10);
} else {
digitalWrite(Relay_Down,LOW);
oled.clear();
oled.setScale(2);
oled.setCursor(1, 1);
oled.println("OK !!");
oled.setCursor(1, 3);
oled.println("SW2 PASS ...");
oled.setCursor(1, 5);
oled.println("SW1 PASS ...");
oled.setScale(1);
oled.setCursor(0, 7);
oled.println("Xerotk Tech");
oled.update();
noTone(buzzer);
digitalWrite(Led1, LOW);
delay(10);
time1 = 0;
delay(6000);
Start();
}
} // END while
}
}