// Lift Control Code for Arduino Uno
// Define the pin numbers for the limit switches, the relays, and the push buttons
const int topLimitSwitch = 2; // Top limit switch connected to pin 2
const int bottomLimitSwitch = 3; // Bottom limit switch connected to pin 3
const int relay1 = 4; // Relay 1 connected to pin 4
const int relay2 = 5; // Relay 2 connected to pin 5
const int insideButton = 8; // Inside button connected to pin 8
// Define the pin numbers for the push buttons
const int groundFloorButton = 6; // Ground floor button connected to pin 6
const int firstFloorButton = 7; // First floor button connected to pin 7
// Define the variables to store the current floor and the destination floor
int currentFloor = 0; // Start at ground floor
int destinationFloor = 0;
void setup() {
// Set the pin modes for the limit switches, the relays, and the push buttons
pinMode(topLimitSwitch, INPUT_PULLUP);
pinMode(bottomLimitSwitch, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(groundFloorButton, INPUT_PULLUP);
pinMode(firstFloorButton, INPUT_PULLUP);
pinMode(insideButton, INPUT_PULLUP);
}
void loop() {
// Check if the lift is at the top or bottom floor
if (digitalRead(topLimitSwitch) == LOW) {
currentFloor = 1;
} else if (digitalRead(bottomLimitSwitch) == LOW) {
currentFloor = 0;
}
// Check if a button has been pressed on either floor or inside the lift
if (digitalRead(groundFloorButton) == LOW) {
destinationFloor = 0;
} else if (digitalRead(firstFloorButton) == LOW) {
destinationFloor = 1;
} else if (digitalRead(insideButton) == LOW) {
destinationFloor = (currentFloor + 1) % 2; // Change the destination floor to the other floor
}
// Move the lift to the destination floor
if (destinationFloor > currentFloor) {
digitalWrite(relay1, HIGH); // Activate relay 1 to move the lift up
digitalWrite(relay2, LOW); // Deactivate relay 2
} else if (destinationFloor < currentFloor) {
digitalWrite(relay1, LOW); // Deactivate relay 1
digitalWrite(relay2, HIGH); // Activate relay 2 to move the lift down
} else {
digitalWrite(relay1, LOW); // Deactivate both relays
digitalWrite(relay2, LOW);
}
}