const const const const const const const const const
int floorSwitch2 = 10; int floorSwitch2 int floorSwitch3 int floorSwitch3 = 11; int floorSwitch1 int floorSwitch1 = 12; int controlPin1 int controlPin1 = 2; // connected to pin 7 on the H-bridge int controlPin2 int controlPin2 = 3; // connected to pin 2 on the H-bridge int enablePin int enablePin = 9; // connected to pin 1 on the H-bridge int floor1 int floor1 = 4; // connected to the switch for first floor int floor2 int floor2 = 5; // connected to the switch for second floor int floor3 int floor3 = 6; // connected to the switch for the third floor
int f1 = 0; int f1 int f2 int f2 = 0; int f3 int f3 = 0;
// floor 1 boolean checker // floor 2 boolean checker // floor 3 boolean checker
int fs1 = 0; int fs1 int fs2 int fs2 = 0; int fs3 int fs3 = 0; void setup setup() () { // intialize the inputs and outputs pinMode(floor1, pinMode (floor1, INPUT INPUT); ); pinMode(floor2, pinMode (floor2, INPUT INPUT); ); pinMode(floor3, pinMode (floor3, INPUT INPUT); ); pinMode(floorSwitch1, pinMode (floorSwitch1, INPUT INPUT); ); pinMode(floorSwitch2, pinMode (floorSwitch2, INPUT INPUT); ); pinMode(floorSwitch3, pinMode (floorSwitch3, INPUT INPUT); ); pinMode(controlPin1, pinMode (controlPin1, OUTPUT OUTPUT); ); pinMode(controlPin2, pinMode (controlPin2, OUTPUT OUTPUT); ); pinMode(enablePin, pinMode (enablePin, OUTPUT OUTPUT); ); digitalWrite(enablePin, digitalWrite (enablePin, LOW LOW); ); // sets the motor to off Serial. Serial .begin begin(9600); (9600); // for debugging } void loop loop() () { f1 = digitalRead digitalRead(floor1); (floor1); // reads if button for floor 1 is pressed f2 = digitalRead(floor2); digitalRead(floor2); // reads if button for floor 2 is pressed f3 = digitalRead digitalRead(floor3); (floor3); // reads if button for floor 3 is pressed fs1 = digitalRead digitalRead(floorSwitch1); (floorSwitch1); fs2 = digitalRead(floorSwitch2); digitalRead(floorSwitch2); fs3 = digitalRead digitalRead(floorSwitch3); (floorSwitch3); Serial.print Serial. print( ("Floor 1: "); "); Serial. Serial .println println(fs1); (fs1); Serial. Serial .print print( ("Floor 2: "); "); Serial. Serial .println println(fs2); (fs2); Serial. Serial .print print( ("Floor 3: "); ");
Serial.println(fs3); if (fs1 == 1 ) // while the elevator is at the first floor { if (f2 == 1) // when the button for second floor is pressed { while (fs2 == 0) // this runs the code until the elevator reaches the second floor {
floorUp(); // calls the function to move the elevator upward fs2 = digitalRead(floorSwitch2); } analogWrite(enablePin, 0); // turns of the motor when the second floor is reached } if (f3 == 1) // when the button for the third floor is pressed { while (fs3 == 0) // this runs the code until the elevator reaches the third floor { floorUp(); // calls the function to move the elevator upward fs3 = digitalRead(floorSwitch3); } analogWrite(enablePin, 0); // turns of the motor when the third floor is reached }
} else if (fs2 == 1) // while the elevator is at the second floor { if (f1 == 1) // when the button for the first floor is pressed { while (fs1 == 0) // this runs the code until the elevator reaches the first floor {
floorDown(); // calls the function to move the elevator downward fs1 = digitalRead(floorSwitch1); } analogWrite(enablePin, 0); // turns off the motor when the first floor is reached } if (f3 == 1) // when the button for the third floor is pressed { while (fs3 == 0) // this runs the code until the elevator reaches the third floor {
floorUp(); // calls the function to move the elevator upward fs3 = digitalRead(floorSwitch3); } analogWrite(enablePin, 0); // turns off the motor when the third floor is reached }
} else if (fs3 == 1) // while the elevator is at the third floor { if (f1 == 1) // when the button for the first floor is pressed { while (fs1 == 0) // this runs the code until the elevator reaches the first floor {
floorDown(); // calls the function to move the elevator downward fs1 = digitalRead(floorSwitch1); } analogWrite(enablePin, 0); // turns off the motor when the first floor is reached } else if (f2 == 1) // when the button for the second floor is pressed { while (fs2 == 0) // this runs the code until the elevator reaches the second floor {
floorDown(); // calls the function to move the elevator downward fs2 = digitalRead(floorSwitch2); } analogWrite(enablePin, 0); // turns off the motor when the first floor is reached } }
} void floorDown() // class to move the elevator downward { analogWrite(enablePin, 255); // turns on the motor digitalWrite(controlPin1, LOW); // controls the direction digitalWrite(controlPin2, HIGH); // controls the direction } void floorUp() // class to move the elevtor upward { analogWrite(enablePin, 255); // turns on the motor digitalWrite(controlPin1, HIGH); // controls the direction digitalWrite(controlPin2, LOW); // controls the direction }