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

#define DHTPIN 2       // Pin data sensor DHT11 terhubung ke pin 2 pada Arduino
#define DHTTYPE DHT22  // Jenis sensor DHT22

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal_I2C lcd(0x27, 16, 2);  // Alamat I2C LCD 0x27, 16x2 karakter

void setup() {
  Serial.begin(9600);
  dht.begin();
  lcd.init();                      // Inisialisasi LCD
  lcd.backlight();                 // Nyalakan latar belakang LCD
}

void loop() {
  delay(2000);  // Tunggu 2 detik antara pembacaan

  float temperature = dht.readTemperature();
  float kelembaban = dht.readHumidity();

  // Periksa apakah pembacaan sukses
  if (isnan(temperature)) {
    Serial.println("Gagal membaca sensor DHT11!");
    lcd.setCursor(0, 0);
    lcd.print("Error");
    return;
  }

  // Menampilkan suhu di Serial Monitor
  Serial.print("Suhu: ");
  Serial.print(temperature);
  Serial.println(" °C");

  lcd.clear();
  lcd.setCursor(2,0);
  lcd.print("Suhu Ruangan");
  lcd.setCursor(2,1);
  lcd.print("Lab Komputer");

  delay(5000);
//
//  // Menampilkan suhu di LCD
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("Suhu: ");
    lcd.print(temperature,1);
    lcd.print((char)223);
    lcd.print("C");
  
    // Menampilkan kriteria suhu di LCD berdasarkan rentang
    lcd.setCursor(2,1);
    if (temperature < 20) {
      lcd.print("Udara Dingin");
    } else if (temperature >= 20 && temperature < 30) {
      lcd.print("Udara Sejuk");
    } else if (temperature >= 30 && temperature < 35) {
      lcd.print("Udara Normal");
    } else {
      lcd.print("Udara Panas");
    }
    delay(5000);

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Lembab: ");
  lcd.print(kelembaban,1);
  lcd.print("%   "); 

  lcd.setCursor(0,1);
    if (kelembaban < 20) {
      lcd.print("Sangat Kering");
    } else if (kelembaban >= 20 && kelembaban < 40) {
      lcd.print("Ruangan Kering");
    } else if (kelembaban >= 40 && kelembaban < 60) {
      lcd.print("Ruangan Nyaman");
    } else {
      lcd.print("Ruangan Lembab");
    }
  delay(5000);
}