int buttonUp = 2;
int buttonDown = 3;
int motorPinUp = 9; /// the wire to the FET for the motor.
//int motorPinDown = 10; // Only for the reverse direction (ignore)
int ledSafeToOpen = 6;
int ledMoving = 7;
// Variables to track the elevator state
bool isMoving = false;
bool isMovingUp = false;
bool isMovingDown = false;
void setup() {
// Set buttons with internal pull-up resistors
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
pinMode(motorPinUp, OUTPUT);
//pinMode(motorPinDown, OUTPUT);
pinMode(ledSafeToOpen, OUTPUT);
pinMode(ledMoving, OUTPUT);
}
void loop() {
// Handle Idle State
if (!isMoving) {
digitalWrite(ledSafeToOpen, HIGH); // Safe to open
// Check if buttons are pressed (LOW due to pull-up)
if (digitalRead(buttonUp) == LOW) {
isMoving = true;
isMovingUp = true;
digitalWrite(ledSafeToOpen, LOW); // Not safe to open
digitalWrite(ledMoving, HIGH); // Indicate moving
digitalWrite(motorPinUp, HIGH);
delay(10000); // 10-second delay for traveling
digitalWrite(motorPinUp, LOW);
isMoving = false;
isMovingUp = false;
digitalWrite(ledMoving, LOW);
delay(1000); // Wait at the floor for a bit
} else if (digitalRead(buttonDown) == LOW) {
isMoving = true;
isMovingDown = true;
digitalWrite(ledSafeToOpen, LOW); // Not safe to open
digitalWrite(ledMoving, HIGH); // Indicate moving
//digitalWrite(motorPinDown, HIGH);
delay(10000); // 10-second delay for traveling
//digitalWrite(motorPinDown, LOW);
isMoving = false;
isMovingDown = false;
digitalWrite(ledMoving, LOW);
delay(1000); // Wait at the floor for a bit
}
}
}