#include <Servo.h> // Library for servo motor
Servo elevatorMotor; // Create a servo object
// Pin definitions
const int buttonUpPin = 2; // Button for moving elevator up
const int buttonDownPin = 3; // Button for moving elevator down
// Floor and elevator status variables
int currentFloor = 0; // 0: Ground floor, 1: Floor 1, 2: Floor 2
bool movingUp = false;
bool movingDown = false;
// Function to move elevator up
void moveUp() {
elevatorMotor.write(180); // Adjust servo position for moving up
delay(1000); // Adjust delay for required movement time
elevatorMotor.write(0); // Adjust servo position for stopping at the floor
}
// Function to move elevator down
void moveDown() {
elevatorMotor.write(180); // Adjust servo position for moving down
delay(1000); // Adjust delay for required movement time
elevatorMotor.write(0); // Adjust servo position for stopping at the floor
}
void setup() {
pinMode(buttonUpPin, INPUT_PULLUP);
pinMode(buttonDownPin, INPUT_PULLUP);
elevatorMotor.attach(9); // Attach servo motor to pin 9
elevatorMotor.write(0); // Initialize servo position at the ground floor
}
void loop() {
// Check button states for floor selection
if (digitalRead(buttonUpPin) == LOW && !movingUp && currentFloor < 2) {
movingUp = true;
currentFloor++;
moveUp();
movingUp = false;
}
if (digitalRead(buttonDownPin) == LOW && !movingDown && currentFloor > 0) {
movingDown = true;
currentFloor--;
moveDown();
movingDown = false;
}
}