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

LiquidCrystal_I2C lcd(0x27, 16, 2);

#define pb 7
#define led 8
bool mode;
unsigned long buttonPressTime = 0;
unsigned long currentMillis = 0;

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  pinMode(pb, INPUT_PULLUP);
  pinMode(led, OUTPUT);
  mode = false;
}

void loop() {
  currentMillis = millis();
  // Cek apakah tombol ditekan
  if (digitalRead(pb) == LOW) {
    buttonPressTime = currentMillis;
    mode = true;
  }

  // Cek apakah 5 detik telah berlalu sejak tombol ditekan
  if (mode && (currentMillis - buttonPressTime >= 5000)) {
    lcd.clear();
    buttonPressTime = 0;
    mode = false;   
  }

  // Kontrol LED berdasarkan mode
  if (mode) {
    digitalWrite(led, HIGH);
  } else {
    digitalWrite(led, LOW);
  }

  // Menampilkan detik sejak tombol ditekan pada LCD
  unsigned long elapsedSeconds = (currentMillis - buttonPressTime) / 1000;

  lcd.setCursor(0, 0); lcd.print("Delay Truck:");
  lcd.setCursor(15, 0); lcd.print("s");
  if(mode){
  lcd.setCursor(13, 0); lcd.print(elapsedSeconds+1);
  }
}