int currentLevel = 0;

void setup() {
  pinMode(9, INPUT);   // Call button at Level 0
  pinMode(10, INPUT);  // Call button at Level 1
  pinMode(11, INPUT);  // Slide switch for floor selection
  pinMode(12, OUTPUT); // LED indicating the door status
  pinMode(2, OUTPUT);  // 7-segment display A
  pinMode(3, OUTPUT);  // 7-segment display B
  pinMode(4, OUTPUT);  // 7-segment display C
  pinMode(5, OUTPUT);  // 7-segment display D
  pinMode(6, OUTPUT);  // 7-segment display E
  pinMode(7, OUTPUT);  // 7-segment display F
  pinMode(8, OUTPUT);  // 7-segment display G
  pinMode(13, OUTPUT); // DC motor control pin
  pinMode(A0, INPUT_PULLUP);  // Emergency stop button
}

void loop() {
  if (digitalRead(A0) == LOW) {
    digitalWrite(12, LOW); // Door LED off if emergency stop is pressed
  }

  if (digitalRead(9) == HIGH && currentLevel != 0) {
    moveLift(0);
  }

  if (digitalRead(10) == HIGH && currentLevel != 1) {
    moveLift(1);
  }

  int selectedLevel = digitalRead(11);
  if (selectedLevel != currentLevel) {
    moveLift(selectedLevel);
  }
}

void moveLift(int targetLevel) {
  digitalWrite(12, HIGH); // Door LED on
  displayLevel(targetLevel);
  digitalWrite(13, HIGH); // Motor on
  delay(10000);
  digitalWrite(13, LOW); // Motor off
  currentLevel = targetLevel;
}

void displayLevel(int level) {
  if (level == 1) {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
  } else if (level == 0) {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
  } else {
    // Display nothing (all segments off)
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
  }
}