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

LiquidCrystal_I2C lcd(0x27, 16, 2);

int x, langkah = 0;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
  {'A','1','2','3'},
  {'B','4','5','6'},
  {'C','7','8','9'},
  {'0','*','#','D'},
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
int timerMinutes = 0;
int timerSeconds = 0;
int keyPin = LOW;

const int ledPin = 11;

void baca_keypad() {
  customKey = customKeypad.getKey();
  Serial.print(customKey);
  Serial.print(" : ");
  Serial.println(langkah);
}

void mulai_cancel() {
  baca_keypad();
  if (customKey == '0') {
    langkah = 0;
    timerMinutes = 0;
    timerSeconds = 0;
  } else if (customKey == '5') {
    langkah = 5;
    timerMinutes = 10; // Set waktu mulai pada setiap langkah
    timerSeconds = 0;
  }
}

void pilih() {
  baca_keypad();
  if (customKey == '1') {
    langkah = 1;
    timerMinutes = 10;
    timerSeconds = 0;
  } else if (customKey == '2') {
    langkah = 2;
    timerMinutes = 12;
    timerSeconds = 30;
  } else if (customKey == '3') {
    langkah = 3;
    timerMinutes = 15;
    timerSeconds = 0;
  } else if (customKey == '4') {
    langkah = 4;
    timerMinutes = 18;
    timerSeconds = 0;
  }
}

void setup() {
  lcd.backlight();
  lcd.init();
  Serial.begin(9600);
  pinMode (ledPin, OUTPUT);
}

void loop() {
  baca_keypad();
  while (langkah == 0) {
    pilih();
    lcd.print("4.Kaos");
    lcd.setCursor(8, 0);
    lcd.print("2.Katun");
    lcd.setCursor(0, 1);
    lcd.print("3.Jeans");
    lcd.setCursor(0, 0);
    lcd.print("1.Jaket");
    lcd.setCursor(8, 1);
  }

  lcd.clear();

  while (langkah == 1) {
    mulai_cancel();
    if (timerSeconds == 0 && timerMinutes > 0) {
      timerMinutes--;
      timerSeconds = 59;
    } else if (timerSeconds > 0) {
      timerSeconds--;
    }
    lcd.print("Waktu: ");
    if (timerMinutes < 10) {
      lcd.print("0");
    }
    lcd.print(timerMinutes);
    lcd.print(":");
    if (timerSeconds < 10) {
      lcd.print("0");
    }
    lcd.print(timerSeconds);
    lcd.setCursor(0, 0);
    lcd.print("mulai tekan 5");
    lcd.setCursor(0, 1);
    digitalWrite (ledPin, HIGH);
    
   if (timerMinutes == 0 && timerSeconds == 0) {
      langkah = 5;
    }
  }
  lcd.clear();

  while (langkah == 2) {
    mulai_cancel();
    if (timerSeconds == 0 && timerMinutes > 0) {
      timerMinutes--;
      timerSeconds = 59;
    } else if (timerSeconds > 0) {
      timerSeconds--;
    }
    lcd.print("Waktu : ");
    if (timerMinutes < 10) {
      lcd.print("0");
    }
    lcd.print(timerMinutes);
    lcd.print(":");
    if (timerSeconds < 10) {
      lcd.print("0");
    }
    lcd.print(timerSeconds);
    lcd.setCursor(0, 0);
    lcd.print("mulai tekan 5");
    lcd.setCursor(0, 1);
    if (timerMinutes == 0 && timerSeconds == 0) {
      langkah = 5;
    }
  }
  lcd.clear();

  while (langkah == 3) {
    mulai_cancel();
    if (timerSeconds == 0 && timerMinutes > 0) {
      timerMinutes--;
      timerSeconds = 59;
    } else if (timerSeconds > 0) {
      timerSeconds--;
    }
    lcd.print("Waktu : ");
    if (timerMinutes < 10) {
      lcd.print("0");
    }
    lcd.print(timerMinutes);
    lcd.print(":");
    if (timerSeconds < 10) {
      lcd.print("0");
    }
    lcd.print(timerSeconds);
    lcd.setCursor(0, 0);
    lcd.print("mulai tekan 5");
    lcd.setCursor(0, 1);
    if (timerMinutes == 0 && timerSeconds == 0) {
      langkah = 5;
    }
  }
  lcd.clear();

  while (langkah == 4) {
    mulai_cancel();
    if (timerSeconds == 0 && timerMinutes > 0) {
      timerMinutes--;
      timerSeconds = 59;
    } else if (timerSeconds > 0) {
      timerSeconds--;
    }
    lcd.print("Waktu : ");
    if (timerMinutes < 10) {
      lcd.print("0");
    }
    lcd.print(timerMinutes);
    lcd.print(":");
    if (timerSeconds < 10) {
      lcd.print("0");
    }
    lcd.print(timerSeconds);
    lcd.setCursor(0, 0);
    lcd.print("mulai tekan 5");
    lcd.setCursor(0, 1);
    if (timerMinutes == 0 && timerSeconds == 0) {
      langkah = 5;
    }
  }
  lcd.clear();

  while (langkah == 5) {
    mulai_cancel();
    lcd.print("5.Selesai");
    lcd.setCursor(0, 1);
    if (timerMinutes == 0 && timerSeconds == 0) {
      langkah = 0;
    }
  }
}

void mulai_cancel1() {
  if (customKey == '5') {
    if (timerMinutes > 0 || timerSeconds > 0) {
      langkah = 0;
    } else {
      timerMinutes = 0;
      timerSeconds = 0;
    }
  }
}

void baca_keypad1() {
  uint8_t i;
  uint8_t data;
  if (digitalRead(keyPin) == LOW) {
    data = customKeypad.getKey();
    if (data != NO_KEY) {
      customKey = (char)data;
      while (digitalRead(keyPin) == LOW) {
        delay(50);
      }
    }
  }
}

void selesai() {
  langkah = 0;
  lcd.clear();
  lcd.print("Penjemuran selesai");
  lcd.setCursor(0, 1);
  lcd.print("Tekan tombol");
  digitalWrite (ledPin, LOW);
  delay(2000);
}