Here, the basic idea of the elevator system that you can find. Using arduino to create an elevator is so easy. Only you need an algorithm which controls stepper, servo and buttons. Before understand the code, I need to say I can not explain the structure of the elevator. You need to build it yourself. In the video above, the components which are used in the project is represented , so you can google it. For instance, I used a stepper motor to go up and down the cabin of the elevator but if you want, it is possible to change the components. Hovewer, if you use an different element instead a stepper motor then you need to change some lines in the code.
First let`s see how the cabin can move up or down. Here is the code. The “i” variable shows the number of floor. The movement of the cabin because of variable “i”. As you see elevator consist of 4 floor then for each floor, the code is written. First time you have to start the elevator in the first floor. Because elevator can not understand where it is when the system firstly ready to go. Now elevator is ready.For example, push the button 4 which means exactly you want to elevator of the cabin goes up to forth floor. Then, due to if(buttonState4 == HIGH), the codes will be worked inside of this line. As we know i=1 firstly so i<4. Then until i goes to the 3, up() function will be active. So cabin will be on the fourth floor. After that which floor you want to go just push the button !. Here it is good to say , as you see until forth floor, up() function is calling for three
times.
/*
*************************************************************
Arduino Basic Elevator Simulation
www.ulasdikme.com
**********************************************************
*/
int i=1; // the value represents where elevator stay ( fisrt time elevator start in the first floor
int btn4=8; // represents the 4 th floor
int btn3=7; // 2 nd floor
int btn2=6; // 3 th floor
int btn1=5; // 1 st floor
int buttonState=0;
int buttonState2=0;
int buttonState3=0;
int buttonState4=0;
int motorPin1 = 10;
int motorPin2 = 11;
int motorPin3 = 12;
int motorPin4 = 13;
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
int delayTime = 50;
Serial.begin(9600);
pinMode(btn4,INPUT);
pinMode(btn2,INPUT);
pinMode(btn3,INPUT);
pinMode(btn1,INPUT);
}
void loop() {
// Serial.println("You are on the i st floor");
buttonState = digitalRead(btn1);
buttonState2 = digitalRead(btn2);
buttonState3 = digitalRead(btn3);
buttonState4 = digitalRead(btn4);
/*------------------------------------------------------------------*/
// 4 th floor required codes
if(buttonState4 == HIGH){
while(i<4){
i++;
up();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
}
i=4;
}// end of the buttonState4
//------ end of the 4th floor ------
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
//start of the 3 td floor codes ----
if(buttonState3==HIGH){
if(i>3){
while(i>3) {
i--;
down();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
}
}
if(i<3){ // person is waiting on the first or second floor. Call the elevater
while(i<3){
i++;
up();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
}
}
i=3; //assign the elevator value to the three
} // end of the buttonState3
//end of the 3 td floor codes ----
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
// 2 nd floor required codes
if(buttonState2 == HIGH){
if(i>2){
while(i>2)
{
i--;
down();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
}
i=2;
}
if(i<2){
while(i<2){
i++;
up();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
}
i=2;
}
}// end of the buttonState2
// --- end of the 2 th floor codes ---
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
// start of the 1 st floor codes ------
if(buttonState == HIGH){
if(i>1){
while(i>1){
i--;
down();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
}
}
i=1;
}// end of the buttonState1
//--- end of the first floor codes-----
/*------------------------------------------------------------------*/
}
// ------- up function which is for to go up elevator
void up() { // each floor distance where it works to up
for(int b=0; b<300; b++){
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(3);
}
}
void down() { // each floor distance where it works to down
for(int a=0;a<300;a++){
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(3);
}
}
The up() and down() functions are important. If you look at to the code closely, you see up() and down() functions, in the for loop.Inside of the foor loop variable a and b are increased until it is equal to 299. Which makes 300 steps. These steps represent our elevators each floor length.Basicly my stepper can be moved the cabin as a one floor in 300 steps. I mean 300 will be depend on the your system. Also delay() time
can be changed to take the one floor. Here is the question, how can you know the how much step you need to take for a one floor or how much time you need ? When I thougt that, first I need to find the one revolution of my stepper as a cm in one milli sec. Then I need to measure the distance one floor… But no !! Just insert your stepper in your system and work it. Give some value after a few trying you find the perfect number. As an example, our one floor distance is closely 10 cm. It is easy to change the number not delay time. Give first some delay then change the number in the for loop.
Attention !! If you copy and paste the code it is not good idea to understand the idea. Just follow the steps. Then you get the whole points
Now we are ready to show the number of each floor using 7 segment display. Before that let me explain the trick. If you see the video, you should see we used an arduino leonardo. Which has no enough pin number to drive an 7 segment display, so It was needed an integreated circuit which is known as a shift register, to drive the 7 segment display only using 3 digital pin. It is great !!!
I will explain this situation other time. But for now just google it. HOW TO DRIVE 7 SEGMENT DISPLAY USING SHIFHT REGISTER ARDUINO. Or SHIFT REGISTER ARDUINO. then you get what you want exactly. I will give you the code and some explanation which is enough to use.
/*
*************************************************************
Arduino Basic Elevator Simulation
www.ulasdikme.com
**********************************************************
*/
int i=1; // the value represents where elevator stay ( fisrt time elevator start in the first floor
int btn4=8; // represents the 4 th floor
int btn3=7; // 2 nd floor
int btn2=6; // 3 th floor
int btn1=5; // 1 st floor
int buttonState=0;
int buttonState2=0;
int buttonState3=0;
int buttonState4=0;
int motorPin1 = 10;
int motorPin2 = 11;
int motorPin3 = 12;
int motorPin4 = 13;
const int latchPin = 3; // Pin connected to Pin 12 of 74HC595 (Latch)
const int dataPin = 4; // Pin connected to Pin 14 of 74HC595 (Data)
const int clockPin = 2; // Pin connected to Pin 11 of 74HC595 (Clock)
const byte numbers[16] = {
0b11111100,
0b01100000,
0b11011010,
0b11110010,
0b01100110,
0b10110110,
0b10111110,
0b11100000,
0b11111110,
0b11100110,
0b11101110,
0b00111110,
0b10011100,
0b01111010,
0b10011110,
0b10001110
};
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
int delayTime = 50;
Serial.begin(9600);
pinMode(btn4,INPUT);
pinMode(btn2,INPUT);
pinMode(btn3,INPUT);
pinMode(btn1,INPUT);
}
void loop() {
// Serial.println("You are on the i st floor");
buttonState = digitalRead(btn1);
buttonState2 = digitalRead(btn2);
buttonState3 = digitalRead(btn3);
buttonState4 = digitalRead(btn4);
show(numbers[i]); //write number on the 7 segment display which floor elevator stays
/*------------------------------------------------------------------*/
// 4 th floor required codes
if(buttonState4 == HIGH){
while(i<4){
i++;
up();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
show(numbers[i]);
}
i=4;
show(numbers[i]);
}// end of the buttonState4
//------ end of the 4th floor ------
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
//start of the 3 td floor codes ----
if(buttonState3==HIGH){
if(i>3){
while(i>3) {
i--;
down();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
show(numbers[i]);
}
}
if(i<3){ // person is waiting on the first or second floor. Call the elevater
while(i<3){
i++;
up();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
show(numbers[i]);
}
}
i=3; //assign the elevator value to the three
show(numbers[i]);
delay(1000);
} // end of the buttonState3
//end of the 3 td floor codes ----
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
// 2 nd floor required codes
if(buttonState2 == HIGH){
if(i>2){
while(i>2)
{
i--;
down();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
show(numbers[i]);
}
i=2;
}
if(i<2){
while(i<2){
i++;
up();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
show(numbers[i]);
}
i=2;
show(numbers[i]);
delay(1000);
}
}// end of the buttonState2
// --- end of the 2 th floor codes ---
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
// start of the 1 st floor codes ------
if(buttonState == HIGH){
if(i>1){
while(i>1){
i--;
down();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
show(numbers[i]);
}
}
i=1;
show(numbers[i]);
delay(1000);
}// end of the buttonState1
//--- end of the first floor codes-----
/*------------------------------------------------------------------*/
}
// ------- up function which is for to go up elevator
void up() { // each floor distance where it works to up
for(int b=0; b<300; b++){
show(numbers[i]);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(3);
}
}
void down() { // each floor distance where it works to down
for(int a=0;a<300;a++){
show(numbers[i]);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(3);
}
}
void show( byte number)
{
// Use a loop and a bitwise AND to move over each bit that makes up
// the seven segment display (from left to right, A => G), and check
// to see if it should be on or not
for(int j = 0; j <= 7; j++)
{
byte toWrite = number & (0b10000000 >> j);
// If all bits are 0 then no point writing it to the shift register,
// so break out and move on to next segment.
if(!toWrite) {
continue;
}
// Otherwise shift it into the register
shiftIt(toWrite);
}
}
void shiftIt (byte data)
{
// Set latchPin LOW while clocking these 8 bits in to the register
digitalWrite(latchPin, LOW);
for (int k=0; k <= 7; k++)
{
// clockPin LOW prior to sending a bit
digitalWrite(clockPin, LOW);
// Note that in our case, we need to set pinState to 0 (LOW) for
// “On†as the 74HC595 is sinking current when using a common
// anode display. If you want to use a common cathode display then
// switch this around.
if ( data & (1 << k) )
{
digitalWrite(dataPin, LOW); // turn “Onâ€
}
else
{
digitalWrite(dataPin, HIGH); // turn “Offâ€
}
// and clock the bit in
digitalWrite(clockPin, HIGH);
}
//stop shifting out data
digitalWrite(clockPin, LOW);
//set latchPin to high to lock and send data
digitalWrite(latchPin, HIGH);
// put delay here if you want to see the multiplexing in action!
//delay(100);
}
</textarea>
<p> Show(numbers[i]); this line represents the floor number on the 7 segment. I said before if you worry about the code, I mean you want to learn how it works,
google it. Or you just need to use it then copy the code. Here is the some useful information about the shift register. When you use it in your elevator, it is good
idea to use external 5v power supply for seven segment. Do not use arduino as a power supply ! As in the video when cabin is moving the number is not seen . Actually
the reason is not about the power supply. Basicly I forgot to insert the line show(numbers[i]); in the up() and down() function :) . After that, I solved. But it is still
not clear when the cabin is moving. This is because of the power supply! Again I said do not use 5v pin of arduino. Use external one. </p>
<p> So here is the final step. The door !!!. Our basic idea when the cabin stop, open the door for a two second then close it. Just it.
We use servo to open and close the door. So whole code is blow.</p>
<textarea>
/*
*************************************************************
Arduino Basic Elevator Simulation
www.ulasdikme.com
**********************************************************
*/
#include <Servo.h>
Servo door;
int i=1; // the value represents where elevator stay ( fisrt time elevator start in the first floor
int btn4=8; // represents the 4 th floor
int btn3=7; // 2 nd floor
int btn2=6; // 3 th floor
int btn1=5; // 1 st floor
int buttonState=0;
int buttonState2=0;
int buttonState3=0;
int buttonState4=0;
int motorPin1 = 10;
int motorPin2 = 11;
int motorPin3 = 12;
int motorPin4 = 13;
const int latchPin = 3; // Pin connected to Pin 12 of 74HC595 (Latch)
const int dataPin = 4; // Pin connected to Pin 14 of 74HC595 (Data)
const int clockPin = 2; // Pin connected to Pin 11 of 74HC595 (Clock)
const byte numbers[16] = {
0b11111100,
0b01100000,
0b11011010,
0b11110010,
0b01100110,
0b10110110,
0b10111110,
0b11100000,
0b11111110,
0b11100110,
0b11101110,
0b00111110,
0b10011100,
0b01111010,
0b10011110,
0b10001110
};
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
int delayTime = 50;
Serial.begin(9600);
pinMode(btn4,INPUT);
pinMode(btn2,INPUT);
pinMode(btn3,INPUT);
pinMode(btn1,INPUT);
door.attach(9);
}
void loop() {
// Serial.println("You are on the i st floor");
buttonState = digitalRead(btn1);
buttonState2 = digitalRead(btn2);
buttonState3 = digitalRead(btn3);
buttonState4 = digitalRead(btn4);
show(numbers[i]); //write number on the 7 segment display which floor elevator stays
/*------------------------------------------------------------------*/
// 4 th floor required codes
if(buttonState4 == HIGH){
while(i<4){
i++;
up();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
show(numbers[i]);
}
i=4;
show(numbers[i]);
door();
}// end of the buttonState4
//------ end of the 4th floor ------
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
//start of the 3 td floor codes ----
if(buttonState3==HIGH){
if(i>3){
while(i>3) {
i--;
down();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
show(numbers[i]);
}
}
if(i<3){ // person is waiting on the first or second floor. Call the elevater
while(i<3){
i++;
up();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
show(numbers[i]);
}
}
i=3; //assign the elevator value to the three
show(numbers[i]);
delay(1000);
door();
} // end of the buttonState3
//end of the 3 td floor codes ----
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
// 2 nd floor required codes
if(buttonState2 == HIGH){
if(i>2){
while(i>2)
{
i--;
down();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
show(numbers[i]);
}
i=2;
}
if(i<2){
while(i<2){
i++;
up();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
show(numbers[i]);
}
i=2;
show(numbers[i]);
delay(1000);
}
door();
}// end of the buttonState2
// --- end of the 2 th floor codes ---
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
// start of the 1 st floor codes ------
if(buttonState == HIGH){
if(i>1){
while(i>1){
i--;
down();
Serial.print("You are on the ");
Serial.print(i);
Serial.println("floor");
show(numbers[i]);
}
}
i=1;
show(numbers[i]);
delay(1000);
door();
}// end of the buttonState1
//--- end of the first floor codes-----
/*------------------------------------------------------------------*/
}
// ------- up function which is for to go up elevator
void up() { // each floor distance where it works to up
for(int b=0; b<300; b++){
show(numbers[i]);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(3);
}
}
void down() { // each floor distance where it works to down
for(int a=0;a<300;a++){
show(numbers[i]);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(3);
}
}
void show( byte number)
{
// Use a loop and a bitwise AND to move over each bit that makes up
// the seven segment display (from left to right, A => G), and check
// to see if it should be on or not
for(int j = 0; j <= 7; j++)
{
byte toWrite = number & (0b10000000 >> j);
// If all bits are 0 then no point writing it to the shift register,
// so break out and move on to next segment.
if(!toWrite) {
continue;
}
// Otherwise shift it into the register
shiftIt(toWrite);
}
}
void shiftIt (byte data)
{
// Set latchPin LOW while clocking these 8 bits in to the register
digitalWrite(latchPin, LOW);
for (int k=0; k <= 7; k++)
{
// clockPin LOW prior to sending a bit
digitalWrite(clockPin, LOW);
// Note that in our case, we need to set pinState to 0 (LOW) for
// “On†as the 74HC595 is sinking current when using a common
// anode display. If you want to use a common cathode display then
// switch this around.
if ( data & (1 << k) )
{
digitalWrite(dataPin, LOW); // turn “Onâ€
}
else
{
digitalWrite(dataPin, HIGH); // turn “Offâ€
}
// and clock the bit in
digitalWrite(clockPin, HIGH);
}
//stop shifting out data
digitalWrite(clockPin, LOW);
//set latchPin to high to lock and send data
digitalWrite(latchPin, HIGH);
// put delay here if you want to see the multiplexing in action!
//delay(100);
}
void door()
{
door.write(0); delay(2000);
door.write(90); delay(2000);
}
Enjoy the code. If you have any question or anything to ask or to say , do not hasitate , just send me an e-mail. 20/05/2014
i need arduino simulation for this project