Elevator

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <Stepper.

h>

#define btn3 6
#define btn2 7
#define btn1 8
#define led3 2
#define led2 3
#define led1 4

int i = 1;
Stepper small_stepper(100, 8, 10, 9, 11);

void setup() {
small_stepper.setSpeed(300);
Serial.begin(9600);

pinMode(btn3, INPUT_PULLUP);
pinMode(btn2, INPUT_PULLUP);
pinMode(btn1, INPUT_PULLUP);

pinMode(led3, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led1, OUTPUT);

digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
}

void updateLEDs() {
digitalWrite(led1, i == 1 ? HIGH : LOW);
digitalWrite(led2, i == 2 ? HIGH : LOW);
digitalWrite(led3, i == 3 ? HIGH : LOW);
}

void moveElevator(int targetFloor) {


while (i != targetFloor) {
if (i < targetFloor) {
i++;
small_stepper.step(-2000);
} else if (i > targetFloor) {
i--;
small_stepper.step(2000);
}

Serial.print("On the way to Floor: ");


Serial.println(i);
updateLEDs();
delay(1000);
}

Serial.print("Arrived at Floor: ");


Serial.println(i);
updateLEDs();
}

void loop() {
Serial.print("Current Floor: ");
Serial.println(i);
delay(1000);

if (digitalRead(btn3) == HIGH) {
moveElevator(3);
}
if (digitalRead(btn2) == HIGH) {
moveElevator(2);
}
if (digitalRead(btn1) == HIGH) {
moveElevator(1);
}
}

You might also like