The goal for this project was to learn how to program on C++ and then upload the code to an Arduino board which will run multiple servos and ping sensors. The ping sensors will detect a set distance based on commands written in our code. Each project group of 2 students.
The project represents an elevator with three floors. It basically consists of two arduino boards (uno and nano) which have separate codes. The first one runs a stepper which is located on top of the box with three buttons for each floor. Whereas the second board is connected to an ultrasonic sensor from the inner side of top of the box which REPORTS the distance between the ceiling and the room. The sensor reports when the room reaches a DISTANCE OF a certain floor the number of the floor appears on a 7-segment screen located next to the stepper.
Three floors elevator including : stepper, sensor, 7 segment, 3 buttons, 2 arduino boards.
1. Two arduino boards on the back of the box
2. Stepper on top of the box with the room attached to it
3. Sensor and 7-segment on ceiling of the box
4.three buttons on the side of the box
The project aim to work like an elevator with 3 floors and three buttons when pressing on a button the program start working by running the stepper which is attached to the room either up or down when the room reaches a different level it changes the distance between the sensor( fixed on the ceiling of the box) and the blocker therefore a new number representing the new level appears on 7-segment screen and the process keep going depending on the button pressed.
Some of the problems encountered: when using all the equipment a 16 digital input needed and only 12 exists in an arduino board therefore we tried to eliminate the number of extensions that are unnecessary but still additional inputs. Finally another arduino board was added and a separate code for stepper and 7-segment was created leaving the servo and the buttons in a different code.
As for improving the project an H-bridge can be used or Mega board where only one code is necessary to save cost and time for engineers and owners of the project when executing happens.
// pin number
int button_pin1=6; // 1st floor
int button_pin2=7; //2nd floor
int button_pin3=8; // 3rd floor
int motorPin1 = 9; //stepper pins
int motorPin2 = 10;
int motorPin3 = 11;
int motorPin4 = 12;
int a=1; // first time elevator start in the first floor
// button state:
int button1State=0;
int button2State=0;
int button3State=0;
void setup() {
Serial.begin(9600);
stepper_button();
}
void loop() {
button();
// 3 td floor
if(button3State==LOW){
if(a<3){
while(a<3){
a++;
up();
show_floor();
}
}
}
// 2 nd floor
if(button2State == LOW){
if(a>2){
while(a>2)
{
a--;
down();
show_floor();
}
}
if(a<2){
while(a<2){
a++;
up();
show_floor();
}
}
}
// 1 st floor
if(button1State == LOW){
if(a>1){
while(a>1){
a--;
down();
show_floor();
}
}
}
}
void stepper_button()
{
pinMode(button_pin1,INPUT);
pinMode(button_pin2,INPUT);
pinMode(button_pin3,INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
void button()
{
button1State=digitalRead(button_pin1);
button2State=digitalRead(button_pin2);
button3State=digitalRead(button_pin3);
}
// up function void (2) { // each floor distance where it works to up
for(int i=0; i<900; i++){
// code of stepper (reference : )
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(5);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(5);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(5);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(5);
}
// down function (2)
void down() {
for(int j=0;j<900;j++){
// code reference:
// https://www.arduino.cc/en/Reference/Stepper
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(5);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(5);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(5);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(5);
}
}
void show_floor() // check if button are working
properly
{
Serial.print("You are on the "); // test your button
Serial.print(a);
Serial.println("floor");
}
int trigPin = 4;
int echoPin = 3;
// defines variables 7 Segment pin
long duration;
int distance;
int f = 11;
int g = 10;
int e = 9;
int d = 8;
int c = 7;
int b = 6;
int a = 5;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an
Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600);
segment();
}
void loop() {
int distance;
distance = check_distance();
if( distance >= 2 && distance <= 4) // distance
between ultra and 3rd floor
{
show_floor_3();
}
else if ( distance >= 10 && distance<= 12) //
distance between ultra and 2nd floor
{
show_floor_2();
}
else if ( distance >=18 && distance <= 20) //
distance between ultra and 1st floor
{
show_floor_1();
}
else
{
show_off();
}
}
int check_distance()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time
in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor for test
and clarify
Serial.print("Distance: ");
Serial.println(distance);
return distance;
}
void segment()
{
pinMode(f, OUTPUT); // Sets the sides of 7-segment as
an Output
pinMode(g, OUTPUT); // Sets the sides of 7-segment as
an Output
pinMode(e, OUTPUT); // Sets the sides of 7-segment as
an Output
pinMode(d, OUTPUT); // Sets the sides of 7-segment as
an Output
pinMode(c, OUTPUT); // Sets the sides of 7-segment as
an Output
pinMode(b, OUTPUT); // Sets the sides of 7-segment as
an Output
pinMode(a, OUTPUT); // Sets the sides of 7-segment as
an Output
}
void show_floor_1()
{
// code of 7-segment reference :
//https://playground.arduino.cc/Main/SevenSegmentLibrary
digitalWrite(a, LOW); // show a number 1 (1st floor)
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g,LOW);
}
void show_floor_2()
{
digitalWrite(a, HIGH); // show a number 2 (2nd floor)
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
void show_floor_3()
{
digitalWrite(a, HIGH); // show a number 3 (3rd floor)
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g, HIGH);
}
void show_off()
{
digitalWrite(a, LOW); // show a number 3 (3rd floor)
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g, LOW);
}