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

int DHTPin = A0;
int DHTType = DHT22;

DHT dht(DHTPin, DHTType);

LiquidCrystal_I2C lcd(0x27, 16, 2);

const uint8_t x = 4;
const uint8_t y = 4;

uint8_t pinBaris[x] = {9,8,7,6};
uint8_t pinKolom[y] = {5,4,3,2};

char keys[x][y] = {{'1','2','3','A'},
                   {'4','5','6','B'},
                   {'7','8','9','C'},
                   {'*','0','#','D'}};

Keypad k = Keypad(makeKeymap(keys),pinBaris,pinKolom,x,y);

bool kesesuaianPIN = false;
String PINTersimpan = "1589";
String PINTerinput = "";

void selamatDatang(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("WELCOME TO EBT");
  lcd.setCursor(0,1);
  lcd.print("PIN:");
}

void setup() {
  dht.begin();
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  selamatDatang();

  while (!kesesuaianPIN) {
    char bacaKey = k.getKey();
    if (bacaKey != NO_KEY){
      Serial.print(bacaKey);
      if (bacaKey == 'D'){
        if (PINTersimpan == PINTerinput) {
          kesesuaianPIN = true;
        }
        PINTerinput = "";
        selamatDatang();       
      } else {
        PINTerinput = PINTerinput + bacaKey;
        lcd.setCursor(0,1);
        lcd.print("PIN:" + PINTerinput);
      }
    }
  }
}

void loop() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();

  lcd.clear();
  delay(300);
  lcd.setCursor(0,0);
  lcd.print("Temp  :" + String(t) + "'C");
  lcd.setCursor(0,1);
  lcd.print("Humid :" + String(h) + "%");
  delay(300);
}