#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // Inisialisasi LCD I2C dengan alamat 0x27 dan ukuran 16x2

// Pin untuk tombol naik, tombol turun, dan tombol darurat
const int buttonUpPin = 2;
const int buttonDownPin = 3;
const int emergencyButtonPin = 4;
const int doorSensorPin = 7; // Pin untuk sensor pintu

// Pin untuk kontrol motor relay
const int motorUpPin = 5;
const int motorDownPin = 6;

// Status elevator
#define STOPPED 0
#define MOVING_UP 1
#define MOVING_DOWN 2
int elevatorStatus = STOPPED;

// Status pintu
#define DOOR_CLOSED HIGH
#define DOOR_OPEN LOW
int doorStatus = DOOR_CLOSED;

void setup() {
  // Setup LCD
  lcd.init();
  lcd.backlight();
  lcd.clear();
  digitalWrite(motorUpPin, LOW);
  digitalWrite(motorDownPin, LOW);
  lcd.print("Lift 2 Lantai");
  delay(500);

  // Setup pin untuk tombol
  pinMode(buttonUpPin, INPUT_PULLUP);
  pinMode(buttonDownPin, INPUT_PULLUP);
  pinMode(emergencyButtonPin, INPUT_PULLUP);

  // Setup pin untuk motor relay
  pinMode(motorUpPin, OUTPUT);
  pinMode(motorDownPin, OUTPUT);

  // Matikan motor relay
  digitalWrite(motorUpPin, LOW);
  digitalWrite(motorDownPin, LOW);

  // Mulai di lantai 1
 // moveToFloor(1);
}

void loop() {
  // Baca tombol
  int buttonUpState = digitalRead(buttonUpPin);
  int buttonDownState = digitalRead(buttonDownPin);

  // Cek tombol naik
  if (buttonUpState == LOW && doorStatus == DOOR_CLOSED) {
    if (elevatorStatus == STOPPED) {
      moveToFloor(2);
    }
  }

  // Cek tombol turun
  if (buttonDownState == LOW && doorStatus == DOOR_CLOSED) {
    if (elevatorStatus == STOPPED) {
      moveToFloor(1);
    }
}

void moveToFloor(int targetFloor) {
  // Update status
  if (targetFloor > 1) {
    elevatorStatus = MOVING_UP;
    lcd.clear();
    lcd.print("Menuju Lantai 2");
    digitalWrite(motorUpPin, HIGH);
    delay(7000); // Waktu yang diperlukan untuk pindah lantai (secara simulasi)
    digitalWrite(motorUpPin, LOW);
    lcd.clear();
    lcd.print("Lantai 2");
    elevatorStatus = STOPPED;
  } else {
    elevatorStatus = MOVING_DOWN;
    lcd.clear();
    lcd.print("Menuju Lantai 1");
    digitalWrite(motorDownPin, HIGH);
    delay(7000); // Waktu yang diperlukan untuk pindah lantai (secara simulasi)
    digitalWrite(motorDownPin, LOW);
    lcd.clear();
    lcd.print("Lantai 1");
    elevatorStatus = STOPPED;
  }
}

  // Reset status
  elevatorStatus = STOPPED;

  // Tampilkan pesan
  lcd.clear();
  lcd.print("Emergency Stop");
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module