Skip to content

Instantly share code, notes, and snippets.

@utkarsh-b690
Last active December 24, 2020 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utkarsh-b690/35f9ead6f128f220d797d486f6925289 to your computer and use it in GitHub Desktop.
Save utkarsh-b690/35f9ead6f128f220d797d486f6925289 to your computer and use it in GitHub Desktop.
Arduino code for IoT Controlled Elevator
#include <LiquidCrystal.h>
int floor1_button = 24;
int floor2_button = 25;
int floor3_button = 26;
int floor4_button = 27;
int floor1_sensor = 21;
int floor2_sensor = 20;
int floor3_sensor = 19;
int floor4_sensor = 18;
int motor_up = 22;
int motor_down = 23;
int motor_up_speed = 8;
int motor_down_speed = 9;
const int floors = 4, speed_motor = 80;
// For 4 floors, the floor ID's will be 0,1,2,3.
int current_floor, dir, floor_sense[floors], floor_sense_up[floors], floor_sense_down[floors], count = 0;
char message;
unsigned long previous_time, current_time, delay_time, delay_time_2, start_time = millis();
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
/* This code will run once and initialise the floor buttons, floor sensors and the motor
* The interrupts are attached to the floorCheck function to know which floor the elevator is on, show the current floor on the display, and turns off the motor if the lift has to stop there
* All the elements in the floor_sense array are set to LOW
*/
pinMode(floor1_button, INPUT);
pinMode(floor2_button, INPUT);
pinMode(floor3_button, INPUT);
pinMode(floor4_button, INPUT);
pinMode(floor1_sensor, INPUT);
pinMode(floor2_sensor, INPUT);
pinMode(floor3_sensor, INPUT);
pinMode(floor4_sensor, INPUT);
attachInterrupt(digitalPinToInterrupt(floor1_sensor), floorCheck, FALLING);
attachInterrupt(digitalPinToInterrupt(floor2_sensor), floorCheck, FALLING);
attachInterrupt(digitalPinToInterrupt(floor3_sensor), floorCheck, FALLING);
attachInterrupt(digitalPinToInterrupt(floor4_sensor), floorCheck, FALLING);
pinMode(motor_up, OUTPUT);
pinMode(motor_down, OUTPUT);
floorCheck();
lcd.begin(16,2);
lcd.clear();
lcdPrint(0,0,"Verticel");
lcdPrint(0,1,"Elevator Remote");
delay(2000);
lcd.clear();
for (int i=0; i<floors; i++){
floor_sense[i] = 0;
}
dir = 0;
Serial.begin(9600);
}
void loop() {
if(dir == 0){
lcdPrint(12,1," ");
}
buttonCheck();
floorCheck();
// Read the serial data and store the information in the variable "message"
if(Serial.available()>0) {
message = Serial.read();
Serial.println(String(message));
// Prints the code on the display if the serial data begins with "P"
if(message == 'P'){
//Print 6 blank spaces
lcdPrint(0,0," ");
//Print the lift code
lcd.setCursor(0,0);
lcd.print(Serial.parseInt());
count = 0;
if(count == 0){
count = 28;
delay_time = millis();
lcdPrint(12,0," ");
lcdPrint(12,0,"29");
}
}
// Calls the elevator if the serial data begins with "C"
if(message == 'C'){
int from = Serial.parseInt();
int to = Serial.parseInt();
callElevator(from, to);
Serial.println("Elevator Called");
Serial.println(from);
Serial.println(to);
}
//Holds the elevator on the current floor if the serial data begins with "H"
if(message == 'H'){
previous_time = millis();
}
}
current_time = millis();
if(current_time-previous_time >= 5200){
dir = 0;
}
if (current_time-previous_time >= 5000){
motorControl();
}
current_time = millis();
if(count>0){
if(current_time-delay_time >= 1000){
delay_time = current_time;
lcdPrint(12,0," ");
lcdPrint(12,0,String(count));
count--;
}
}
current_time = millis();
if(current_time - start_time >= 2000){
start_time = current_time;
Serial.print("Current Floor: ");
Serial.println(current_floor);
Serial.print("Floor sense: ");
Serial.print(floor_sense[0]);
Serial.print(", ");
Serial.print(floor_sense[1]);
Serial.print(", ");
Serial.print(floor_sense[2]);
Serial.print(", ");
Serial.println(floor_sense[3]);
Serial.print("Floor sense up: ");
Serial.print(floor_sense_up[0]);
Serial.print(", ");
Serial.print(floor_sense_up[1]);
Serial.print(", ");
Serial.print(floor_sense_up[2]);
Serial.print(", ");
Serial.println(floor_sense_up[3]);
Serial.print("Floor sense down: ");
Serial.print(floor_sense_down[0]);
Serial.print(", ");
Serial.print(floor_sense_down[1]);
Serial.print(", ");
Serial.print(floor_sense_down[2]);
Serial.print(", ");
Serial.println(floor_sense_down[3]);
Serial.print("Motor state: ");
Serial.println(dir);
Serial.print("Delay time: ");
Serial.println(current_time-previous_time);
}
}
// Assigns a HIGH value on floor_sense array for indices from_floor and to_floor
void callElevator(int from_floor, int to_floor){
if(from_floor <= floors && to_floor <= floors) {
if(from_floor < to_floor){
floor_sense_up[from_floor] = 1;
floor_sense_up[to_floor] = 1;
}
else if(from_floor > to_floor){
floor_sense_down[from_floor] = 1;
floor_sense_down[to_floor] = 1;
}
}
}
//Displays the text passed on the LCD
void lcdPrint(int col, int row, String text){
lcd.setCursor(col,row);
lcd.print(text);
}
//Checks the floor number the elevator is currently in and assigns it to variable current_floor
void floorCheck(){
if(digitalRead(floor1_sensor) == HIGH){
current_floor = 0;
lcdPrint(0,1,"G");
}
else if(digitalRead(floor2_sensor) == HIGH){
current_floor = 1;
lcdPrint(0,1,"1");
}
else if(digitalRead(floor3_sensor) == HIGH){
current_floor = 2;
lcdPrint(0,1,"2");
}
else if(digitalRead(floor4_sensor) == HIGH){
current_floor = 3;
lcdPrint(0,1,"3");
}
if(floor_sense[current_floor] == 1){
motorOff();
}
if(floor_sense_up[current_floor] == 1 && dir == 1){
motorOff();
}
if(floor_sense_down[current_floor] == 1 && dir == -1){
motorOff();
}
}
//Checks if any button in the elevator panel is pressed and assigns a high value to the corresponding floor
void buttonCheck(){
if(digitalRead(floor1_button) == HIGH){
floor_sense[0] = 1;
previous_time = millis();
}
else if(digitalRead(floor2_button) == HIGH){
floor_sense[1] = 1;
previous_time = millis();
}
else if(digitalRead(floor3_button) == HIGH){
floor_sense[2] = 1;
previous_time = millis();
}
else if(digitalRead(floor4_button) == HIGH){
floor_sense[3] = 1;
previous_time = millis();
}
}
// Checks if the elevator has to go to another floor and turns motor on for positive result
void motorControl(){
if (dir == 1){
int i = current_floor;
for (i; i<floors; i++){
if (floor_sense[i] == 1 || floor_sense_up[i] == 1){
controlElevator(i);
}
}
}
else if (dir == -1){
int i = current_floor;
for (i; i>=0; i--){
if (floor_sense[i] == 1 || floor_sense_down[i] == 1){
controlElevator(i);
}
}
}
else if(dir == 0){
int i = 0;
for (i; i<floors; i++){
if (floor_sense[i] == 1 || floor_sense_up[i] == 1 || floor_sense_down[i] == 1){
controlElevator(i);
}
}
}
}
// Turns off the motor
void motorOff(){
digitalWrite(motor_up, LOW);
digitalWrite(motor_down, LOW);
floor_sense[current_floor] = 0;
if(dir == 1){
floor_sense_up[current_floor] = 0;
}
else if(dir == -1){
floor_sense_down[current_floor] = 0;
}
previous_time = millis();
}
// Compares the floor number passed on to the function with the current floor and moves the elevator up or down depending on the result
void controlElevator(int flr) {
if(flr>current_floor){
digitalWrite(motor_down, LOW);
digitalWrite(motor_up, HIGH);
analogWrite(motor_up_speed, speed_motor);
analogWrite(motor_down_speed, speed_motor);
dir = 1;
lcdPrint(12,1," ");
lcdPrint(12,1,"UP");
}
else if(flr<current_floor){
digitalWrite(motor_up, LOW);
digitalWrite(motor_down, HIGH);
analogWrite(motor_up_speed, speed_motor);
analogWrite(motor_down_speed, speed_motor);
dir = -1;
lcdPrint(12,1," ");
lcdPrint(12,1,"DOWN");
}
else if(flr==current_floor){
dir = 0;
lcdPrint(12,1," ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment