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

// Inisialisasi LCD I2C 20x4
LiquidCrystal_I2C lcd(0x27, 20, 4);

// Pin untuk tombol
const int buttonUp = 4;
const int buttonSet = 3;
const int buttonDown = 2;

// Variabel untuk menyimpan status tombol
int lastButtonUpState = HIGH;
int lastButtonSetState = HIGH;
int lastButtonDownState = HIGH;

// Indeks menu
int menuIndex = 0;
const int totalMenu = 4;

void setup() {
  // Inisialisasi pin tombol sebagai input
  pinMode(buttonUp, INPUT_PULLUP);
  pinMode(buttonSet, INPUT_PULLUP);
  pinMode(buttonDown, INPUT_PULLUP);

  // Inisialisasi LCD
  lcd.init();
  lcd.backlight();

  // Tampilkan menu awal
  displayMenu();
}

void loop() {
  // Baca status tombol
  int buttonUpState = digitalRead(buttonUp);
  int buttonSetState = digitalRead(buttonSet);
  int buttonDownState = digitalRead(buttonDown);

  // Cek apakah tombol Up ditekan
  if (buttonUpState == LOW && lastButtonUpState == HIGH) {
    menuIndex--;
    if (menuIndex < 0) {
      menuIndex = totalMenu - 1;
    }
    displayMenu();
  }

  // Cek apakah tombol Down ditekan
  if (buttonDownState == LOW && lastButtonDownState == HIGH) {
    menuIndex++;
    if (menuIndex >= totalMenu) {
      menuIndex = 0;
    }
    displayMenu();
  }

  // Cek apakah tombol Set ditekan
  if (buttonSetState == LOW && lastButtonSetState == HIGH) {
    executeMenu();
  }

  // Simpan status tombol sebelumnya
  lastButtonUpState = buttonUpState;
  lastButtonSetState = buttonSetState;
  lastButtonDownState = buttonDownState;
  
  // Tambah delay kecil untuk menghindari bouncing
  delay(50);
}

void displayMenu() {
  lcd.clear();
  switch (menuIndex) {
    case 0:
      lcd.setCursor(0, 0);
      lcd.print("> Menu1");
      lcd.setCursor(0, 1);
      lcd.print("  Menu2");
      lcd.setCursor(0, 2);
      lcd.print("  Menu3");
      lcd.setCursor(0, 3);
      lcd.print("  Exit");
      break;
    case 1:
      lcd.setCursor(0, 0);
      lcd.print("  Menu1");
      lcd.setCursor(0, 1);
      lcd.print("> Menu2");
      lcd.setCursor(0, 2);
      lcd.print("  Menu3");
      lcd.setCursor(0, 3);
      lcd.print("  Exit");
      break;
    case 2:
      lcd.setCursor(0, 0);
      lcd.print("  Menu1");
      lcd.setCursor(0, 1);
      lcd.print("  Menu2");
      lcd.setCursor(0, 2);
      lcd.print("> Menu3");
      lcd.setCursor(0, 3);
      lcd.print("  Exit");
      break;
    case 3:
      lcd.setCursor(0, 0);
      lcd.print("  Menu1");
      lcd.setCursor(0, 1);
      lcd.print("  Menu2");
      lcd.setCursor(0, 2);
      lcd.print("  Menu3");
      lcd.setCursor(0, 3);
      lcd.print("> Exit");
      break;
  }
}

void executeMenu() {
  lcd.clear();
  switch (menuIndex) {
    case 0:
      lcd.setCursor(0, 0);
      lcd.print("Executing Menu1");
      break;
    case 1:
      lcd.setCursor(0, 0);
      lcd.print("Executing Menu2");
      break;
    case 2:
      lcd.setCursor(0, 0);
      lcd.print("Executing Menu3");
      break;
    case 3:
      lcd.setCursor(0, 0);
      lcd.print("Exiting...");
      delay(1000);
      // Tambahkan fungsi untuk keluar atau reset ke menu utama
      menuIndex = 0;
      displayMenu();
      break;
  }
  // delay(1000); // Tampilkan pesan selama 1 detik
  // displayMenu(); // Kembali ke menu setelah eksekusi
}

void subMenu(){

}
UP
OK
DOWN
KEY OFF/ON
KEY STARTED
C
ACC
BR