#define sw1 3
#define sw2 2
#define sw3 4
#define sw4 5

#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>

#define I2C_ADDR    0x27
#define LCD_COLUMNS 16
#define LCD_LINES   2

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);

int next, back, enter, forward,
    posisi, pointer, mode, countdown, val, sw;

int addrLed   = 0;
int addrBuzz  = 1;
int addrCount = 2;
int addrMode  = 3;


void setup()
{
  pinMode(sw1, INPUT_PULLUP);
  pinMode(sw2, INPUT_PULLUP);
  pinMode(sw3, INPUT_PULLUP);
  pinMode(sw4, INPUT_PULLUP);

  lcd.init();

  // Led Lcd
  if(EEPROM.read(addrLed) >= 1)
  {
    lcd.backlight();
  }
  else
  {
    lcd.noBacklight();
  }

  lcd.setCursor(1, 0);
  lcd.print("Selamat Datang");
  lcd.setCursor(4, 1);
  lcd.print("TRK 2021");
  delay(1000);
  lcd.clear();
}

void loop()
{
  // 0 = menu utama
  // 0 = start
  // 1 = stop
  // 2 = reset
  // 3 = mode
  // 4 = setting

  // 1 = sub menu mode
  // 0 = stopwatch
  // 1 = count down

  // 2 = sub menu setting
  // 0 = led lcd
  // 1 = buzzer

  // 3 = sub menu led
  // 0 = on
  // 1 = off

  // 4 = sub menu buzzer
  // 0 = on
  // 1 = off

  // 5 = sub menu countdown
  // 0 = 30 detik
  // 1 = 1  menit

  // posisi
  // 0 = menu utama
  // 1 = sub menu mode
  // 2 = sub menu setting
  // 3 = sub menu led
  // 4 = sub menu buzzer
  // 5 = sub menu countdown

  next    = digitalRead(sw1);
  back    = digitalRead(sw2);
  enter   = digitalRead(sw3);
  forward = digitalRead(sw4);

  if(next == 0)
  {
    pointer++;
    delay(200);
  }
  if(back == 0)
  {
    pointer--;
    delay(200);
  }

  if(posisi == 1)
  {
    submenumode();
  }
  else if(posisi == 2)
  {
    submenusetting();
  }
  else if(posisi == 3)
  {
    submenuled();
  }
  else if(posisi == 4)
  {
    submenubuzzer();
  }
  else if(posisi == 5)
  {
    submenucountdown();
  }
  else
  {
    menuutama();
  }
  
}

void menuutama()
{
  if(pointer > 4)
  {
    pointer = 0;
  }
  if(pointer < 0)
  {
    pointer = 4;
  }

  switch (pointer)
  {
    case 1:
    lcd.setCursor(0, 0);
    lcd.print("MENU : STOP   ");
    break;

    case 2:
    lcd.setCursor(0, 0);
    lcd.print("MENU : RESET  ");
    break;

    case 3:
    delay(200);
    lcd.setCursor(0, 0);
    lcd.print("MENU : MODE   ");
    if(enter == 0)
    {
      posisi = 1;
      pointer= 0;
    }
    break;

    case 4:
    delay(200);
    lcd.setCursor(0, 0);
    lcd.print("MENU : SETTING");
    if(enter == 0)
    {
      posisi = 2;
      pointer= 0;
    }
    break;

    default:
    lcd.setCursor(0, 0);
    lcd.print("MENU : START  ");
    if(enter == 0)
    {
      if(EEPROM.read(addrMode) == 1 && EEPROM.read(addrCount) == 2)
      {
        for(val = 60; val >= 0; val--)
        {
          if(val == 9)
          {
            lcd.setCursor(1, 1);
            lcd.print(" ");
          }
          if(val == 0)
          {
            lcd.setCursor(0, 1);
            lcd.print(" ");
          }
          lcd.setCursor(0, 1);
          lcd.print(val);
          delay(1000);
        }
      }
      if(EEPROM.read(addrMode) == 1 && EEPROM.read(addrCount) == 1)
      {
        for(val = 30; val >= 0; val--)
        {
          if(val == 9)
          {
            lcd.setCursor(1, 1);
            lcd.print(" ");
          }
          if(val == 0)
          {
            lcd.setCursor(0, 1);
            lcd.print(" ");
          }
          lcd.setCursor(0, 1);
          lcd.print(val);
          delay(1000);
        }
      }
      while(EEPROM.read(addrMode) == 2)
      {
        sw = sw + 1;
        lcd.setCursor(0, 1);
        lcd.print(sw);
        delay(100);
      }
    }
    break;
  }
}

void submenumode()
{
  if(pointer > 1)
  {
    pointer = 0;
  }
  if(pointer < 0)
  {
    pointer = 1;
  }

  switch (pointer)
  {
    case 1:
    lcd.setCursor(0, 1);
    lcd.print("STOPWATCH");
    if(enter == 0)
    {
      EEPROM.write(addrMode, 2);
      delay(500);
      mode = 0;
      lcd.setCursor(0, 1);
      lcd.print("         ");
      posisi = 0;
      pointer= 3;
    }
    break;
    default:
    lcd.setCursor(0, 1);
    lcd.print("COUNTDOWN");
    if(enter == 0)
    {
      delay(500);
      mode = 1;
      lcd.setCursor(0, 1);
      lcd.print("         ");
      posisi = 5;
    }
    break;
  }
}

void submenusetting()
{
  if(pointer > 1)
  {
    pointer = 0;
  }
  if(pointer < 0)
  {
    pointer = 1;
  }

  switch (pointer)
  {
    lcd.setCursor(0, 0);
    lcd.print("SETTING         ");
    case 1:
    lcd.setCursor(0, 1);
    lcd.print("LED LCD         ");
    if(enter == 0)
    { 
      delay(500);
      lcd.setCursor(0, 1);
      lcd.print("        ");
      posisi = 3;
      pointer= 0;
    }
    break;
    default:
    lcd.setCursor(0, 1);
    lcd.print("BUZZER         ");
    if(enter == 0)
    {
      delay(500);
      lcd.setCursor(0, 1);
      lcd.print("        ");
      posisi = 4;
      pointer= 0;
    }
    break;
  }
}

void submenuled()
{
  if(pointer > 1)
  {
    pointer = 0;
  }
  if(pointer < 0)
  {
    pointer = 1;
  }

  switch (pointer)
  {
    lcd.setCursor(0, 0);
    lcd.print("LED LCD        ");
    case 1:
    lcd.setCursor(0, 1);
    lcd.print("OFF            ");
    if(enter == 0)
    { 
      EEPROM.write(addrLed, 0);
      delay(500);
      lcd.setCursor(0, 1);
      lcd.print("             ");
      lcd.noBacklight();
      posisi = 2;
      pointer= 0;
    }
    break;
    default:
    lcd.setCursor(0, 1);
    lcd.print("ON              ");
    if(enter == 0)
    {
      EEPROM.write(addrLed, 1);
      delay(500);
      lcd.setCursor(0, 1);
      lcd.print("              ");
      lcd.backlight();
      posisi = 2;
      pointer= 0;
    }
    break;
  }
}

void submenubuzzer()
{
  if(pointer > 1)
  {
    pointer = 0;
  }
  if(pointer < 0)
  {
    pointer = 1;
  }

  switch (pointer)
  {
    lcd.setCursor(0, 0);
    lcd.print("BUZZER          ");
    case 1:
    lcd.setCursor(0, 1);
    lcd.print("OFF            ");
    if(enter == 0)
    { 
      EEPROM.write(addrBuzz, 0);
      delay(500);
      lcd.setCursor(0, 1);
      lcd.print("             ");
      posisi = 2;
      pointer= 0;
    }
    break;
    default:
    lcd.setCursor(0, 1);
    lcd.print("ON              ");
    if(enter == 0)
    {
      EEPROM.write(addrBuzz, 1);
      delay(500);
      lcd.setCursor(0, 1);
      lcd.print("              ");
      posisi = 2;
      pointer= 0;
    }
    break;
  }
  
}

void submenucountdown()
{
  if(pointer > 1)
  {
    pointer = 0;
  }
  if(pointer < 0)
  {
    pointer = 1;
  }

  switch (pointer)
  {
    lcd.setCursor(0, 0);
    lcd.print("COUNTDOWN       ");
    case 1:
    lcd.setCursor(0, 1);
    lcd.print("1 menit ");
    if(enter == 0)
    { 
      EEPROM.write(addrMode, 1);
      EEPROM.write(addrCount, 2);
      delay(500);
      countdown = 0;
      lcd.setCursor(0, 1);
      lcd.print("        ");
      posisi = 0;
      pointer= 3;
    }
    break;
    default:
    lcd.setCursor(0, 1);
    lcd.print("30 detik");
    if(enter == 0)
    {
      EEPROM.write(addrMode, 1);
      EEPROM.write(addrCount, 1);
      delay(500);
      countdown = 1;
      lcd.setCursor(0, 1);
      lcd.print("        ");
      posisi = 0;
      pointer= 3;
    }
    break;
  }
}