#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);

#define tombol1   12
#define tombol2   11
#define tombol3   10
#define led       13

int menu,submenu,subsubmenu,statmenu,statsubsub,
    stat1,stat2,
    stat3,
    statled,statlcd;

void setup()
{
  Serial.begin(115200);

  lcd.init();           
  lcd.backlight();

  pinMode(tombol1, INPUT_PULLUP);
  pinMode(tombol2, INPUT_PULLUP);
  pinMode(tombol3, INPUT_PULLUP);

  pinMode(led,     OUTPUT);
}

void loop()
{
  //Serial.println(statled);
  
  stat1 = digitalRead(tombol1);
  stat2 = digitalRead(tombol2);
  stat3 = digitalRead(tombol3);

  if(statmenu == 0)
  {
    mainmenu();
  }
  
  if(statmenu == 1)
  {
    mainsubmenu();
  }

  if(statmenu == 2)
  {
    mainsubsub();
  }
  
  delay(100);
  //Serial.println(menu);
}

void mainmenu()
{
  if(stat1 == 0)
  {
    menu++;
  }
  if(stat2 == 0)
  {
    menu--;
  }
  if(menu >= 4)
  {
    menu = 0;
  }
  if(menu <= -1)
  {
    menu = 3;
  }

  if(statmenu == 0 && menu == 0 && submenu == 0)
  {
    lcd.setCursor(0,0);
    lcd.print("START            ");
    lcd.setCursor(0,1);
    lcd.print("PRESS START");

    if(stat3 == 0)
    {
      if(statled == 1)
      {
        digitalWrite(led, 1);
        statmenu    = 0;
        menu        = 0;
        submenu     = 0;
        subsubmenu  = 0;
        statsubsub  = 0;
        delay(100);
        mainsubmenu();
      }
      if(statled == 0)
      {
        digitalWrite(led, 0);
        statmenu    = 0;
        menu        = 0;
        submenu     = 0;
        subsubmenu  = 0;
        statsubsub  = 0;
        delay(100);
        mainsubmenu();
      }

      if(statlcd == 1)
      {
        lcd.backlight();
        statmenu    = 0;
        menu        = 0;
        submenu     = 0;
        subsubmenu  = 0;
        statsubsub  = 0;
        delay(100);
        mainsubmenu();
      }
      if(statlcd == 0)
      {
        lcd.noBacklight();
        statmenu    = 0;
        menu        = 0;
        submenu     = 0;
        subsubmenu  = 0;
        statsubsub  = 0;
        delay(100);
        mainsubmenu();
      }
    }
  }
  if(statmenu == 0 && menu == 1 && submenu == 0)
  {
    lcd.setCursor(0,0);
    lcd.print("SETTING");
    lcd.setCursor(0,1);
    lcd.print("PRESS START");

    if(stat3 == 0)
    {
      submenu = 2;
      statmenu  = 1;
    }
  }
  if(statmenu == 0 && menu == 2 && submenu == 0)
  {
    lcd.setCursor(0,0);
    lcd.print("LCD    ");
    lcd.setCursor(0,1);
    lcd.print("PRESS START");

    if(statmenu == 0 && stat3 == 0)
    {
      submenu = 3;
      statmenu  = 1;
    }
  }
  if(statmenu == 0 && menu == 3 && submenu == 0)
  {
    lcd.setCursor(0,0);
    lcd.print("RESET  ");
    lcd.setCursor(0,1);
    lcd.print("PRESS START");

    if(statmenu == 0 && stat3 == 0)
    {
      lcd.backlight();
      digitalWrite(led, 0);
      statled     = 0;
      statlcd     = 1;
      statmenu    = 0;
      menu        = 0;
      submenu     = 0;
      subsubmenu  = 0;
      statsubsub  = 0;
      delay(100);
      mainsubmenu();
    }
  }
}

void mainsubmenu()
{
  if(statmenu == 1 && submenu == 1)
  {
    if(statled == 1)
    {
      digitalWrite(led, 1);
      statmenu    = 0;
      menu        = 0;
      submenu     = 0;
      subsubmenu  = 0;
      statsubsub  = 0;
      delay(100);
      mainmenu();
    }
    if(statled == 2)
    {
      digitalWrite(led, 0);
      statmenu    = 0;
      menu        = 0;
      submenu     = 0;
      subsubmenu  = 0;
      statsubsub  = 0;
      delay(100);
      mainmenu();
    }
  }

  if(statmenu == 1 && submenu == 2)
  {
    lcd.setCursor(0,0);
    lcd.print("SET LED  ");
    lcd.setCursor(0,1);
    lcd.print("PRESS START");

    if(stat3 == 0)
    {
      subsubmenu  = 2;
      statmenu    = 2;
      lcd.setCursor(0,1);
      lcd.print("ON           ");
    }
  }

  if(statmenu == 1 && submenu == 3)
  {
    lcd.setCursor(0,0);
    lcd.print("SET BACKLIGHT  ");
    lcd.setCursor(0,1);
    lcd.print("PRESS START");

    if(stat3 == 0)
    {
      subsubmenu  = 3;
      statmenu    = 2;
      lcd.setCursor(0,1);
      lcd.print("ON           ");
    }
  }
}

void mainsubsub()
{
  if(statmenu == 2 && subsubmenu == 2)
  {
    lcd.setCursor(0,0);
    lcd.print("SET LED        ");

    if(stat1 == 0)
    {
      lcd.setCursor(0,1);
      lcd.print("ON           ");
      statsubsub = 1;
    }

    if(stat2 == 0)
    {
      lcd.setCursor(0,1);
      lcd.print("OFF          ");
      statsubsub = 2;
    }

    if(statsubsub == 1)
    {
      if(stat3 == 0)
      {
        statled     = 1;
        statmenu    = 0;
        menu        = 0;
        submenu     = 0;
        subsubmenu  = 0;
        statsubsub  = 0;
        delay(100);
        mainsubmenu();
      }
    }
    if(statsubsub == 2)
    {
      if(stat3 == 0)
      {
        statled     = 0;
        statmenu    = 0;
        menu        = 0;
        submenu     = 0;
        subsubmenu  = 0;
        statsubsub  = 0;
        delay(100);
        mainsubmenu();
      }
    }
  }

  if(statmenu == 2 && subsubmenu == 3)
  {
    lcd.setCursor(0,0);
    lcd.print("SET BACKLIGHT        ");

    if(stat1 == 0)
    {
      lcd.setCursor(0,1);
      lcd.print("ON           ");
      statsubsub = 3;
    }

    if(stat2 == 0)
    {
      lcd.setCursor(0,1);
      lcd.print("OFF          ");
      statsubsub = 4;
    }

    if(statsubsub == 3)
    {
      if(stat3 == 0)
      {
        statlcd     = 1;
        statmenu    = 0;
        menu        = 0;
        submenu     = 0;
        subsubmenu  = 0;
        statsubsub  = 0;
        delay(100);
        mainsubmenu();
      }
    }
    if(statsubsub == 4)
    {
      if(stat3 == 0)
      {
        statlcd     = 0;
        statmenu    = 0;
        menu        = 0;
        submenu     = 0;
        subsubmenu  = 0;
        statsubsub  = 0;
        delay(100);
        mainsubmenu();
      }
    }
  }
}