#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD with address 0x27, 16 columns and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pin Definitions
const int floor1 = 2; // Limit switch for floor 1
const int floor2 = 3; // Limit switch for floor 2
const int floor3 = 4; // Limit switch for floor 3
const int callbutton1 = 5; // Call button for floor 1
const int callbutton2 = 6; // Call button for floor 2
const int callbutton3 = 7; // Call button for floor 3
const int relayup = 8; // Relay to drive elevator up
const int relaydown = 9; // Relay to drive elevator down
const int buzzer = 10; // Buzzer for warning or alarm
// Variables
int callelevator1 = 0;
int callelevator2 = 0;
int callelevator3 = 0;
int floor1sense = 0;
int floor2sense = 0;
int floor3sense = 0;
int lastFloorDisplay = 0; // Track the last displayed floor
// Debouncing constants
const unsigned long debounceDelay = 50; // Debounce delay in milliseconds
unsigned long lastButtonPress = 0;
// Function prototypes
void floor1function();
void floor2function();
void floor3function();
bool readButton(int buttonPin);
void beepPattern(int floor);
void setup() {
pinMode(floor1, INPUT);
pinMode(floor2, INPUT);
pinMode(floor3, INPUT);
pinMode(callbutton1, INPUT);
pinMode(callbutton2, INPUT);
pinMode(callbutton3, INPUT);
pinMode(relayup, OUTPUT);
pinMode(relaydown, OUTPUT);
pinMode(buzzer, OUTPUT);
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" SWASTIK STEEL ");
lcd.setCursor(0, 1);
lcd.print(" FURNITURES ");
delay(2000);
}
void loop() {
// Read the state of the call buttons
bool call1 = readButton(callbutton1);
bool call2 = readButton(callbutton2);
bool call3 = readButton(callbutton3);
// Read the state of the limit switches
floor1sense = digitalRead(floor1);
floor2sense = digitalRead(floor2);
floor3sense = digitalRead(floor3);
if (call1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Lift is Called");
lcd.setCursor(0, 1);
lcd.print("Floor 1 to Go");
delay(1000); // Allow time for button release
floor1function();
} else if (call2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Lift is Called");
lcd.setCursor(0, 1);
lcd.print("Floor 2 to Go");
delay(1000);
floor2function();
} else if (call3) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Lift is Called");
lcd.setCursor(0, 1);
lcd.print("Floor 3 to Go");
delay(1000);
floor3function();
} else {
int currentFloorDisplay = 0;
if (floor1sense == LOW) {
currentFloorDisplay = 1;
} else if (floor2sense == LOW) {
currentFloorDisplay = 2;
} else if (floor3sense == LOW) {
currentFloorDisplay = 3;
}
if (currentFloorDisplay != lastFloorDisplay) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("At Floor ");
lcd.print(currentFloorDisplay);
lastFloorDisplay = currentFloorDisplay;
}
}
digitalWrite(relayup, LOW);
digitalWrite(relaydown, LOW);
digitalWrite(buzzer, LOW);
delay(10); // Small delay for smooth operation
}
bool readButton(int buttonPin) {
if (digitalRead(buttonPin) == LOW) {
unsigned long currentTime = millis();
if (currentTime - lastButtonPress > debounceDelay) {
lastButtonPress = currentTime;
return true;
}
}
return false;
}
void beepPattern(int floor) {
switch(floor) {
case 1: // Ground floor - BEEP...PAUSE...BEEP
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(500);
break;
case 2: // 2nd floor - BEEP BEEP...PAUSE...BEEP BEEP
for (int i = 0; i < 2; i++) {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
}
delay(500);
break;
case 3: // 3rd floor - BEEP BEEP BEEP...PAUSE...BEEP BEEP BEEP
for (int i = 0; i < 3; i++) {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
}
delay(500);
break;
default:
break;
}
}
void floor1function() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Called floor 1");
lcd.setCursor(0, 1);
lcd.print("Moving Down");
while (floor1sense == HIGH) {
digitalWrite(relaydown, HIGH);
beepPattern(1); // BEEP...PAUSE...BEEP for 1st floor
floor1sense = digitalRead(floor1);
delay(10);
}
digitalWrite(relaydown, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("At Floor 1");
delay(2000);
}
void floor2function() {
lcd.clear();
if (floor3sense == LOW) {
lcd.setCursor(0, 0);
lcd.print("Called floor 2");
lcd.setCursor(0, 1);
lcd.print("Moving Down");
while (floor2sense == HIGH) {
digitalWrite(relaydown, HIGH);
beepPattern(2); // BEEP BEEP...PAUSE...BEEP BEEP for 2nd floor
floor2sense = digitalRead(floor2);
delay(10);
}
} else if (floor1sense == LOW) {
lcd.setCursor(0, 0);
lcd.print("Called floor 2");
lcd.setCursor(0, 1);
lcd.print("Moving Up");
while (floor2sense == HIGH) {
digitalWrite(relayup, HIGH);
beepPattern(2); // BEEP BEEP...PAUSE...BEEP BEEP for 2nd floor
floor2sense = digitalRead(floor2);
delay(10);
}
}
digitalWrite(relayup, LOW);
digitalWrite(relaydown, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("At Floor 2");
delay(2000);
}
void floor3function() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Called floor 3");
lcd.setCursor(0, 1);
lcd.print("Moving Up");
if (floor2sense == LOW || floor1sense == LOW) {
while (floor3sense == HIGH) {
digitalWrite(relayup, HIGH);
beepPattern(3); // BEEP BEEP BEEP...PAUSE...BEEP BEEP BEEP for 3rd floor
floor3sense = digitalRead(floor3);
delay(10);
}
}
digitalWrite(relayup, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("At Floor 3");
delay(2000);
}