#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define DHTPIN 11
#define DHTTYPE DHT22
#define LDR 34
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4);
void printCenter(String text, int row) {
int totalCols = 20; // jumlah kolom LCD 20x4
int textLength = text.length();
if (textLength > totalCols) {
text = text.substring(0, totalCols); // potong kalau terlalu panjang
textLength = totalCols;
}
int startCol = (totalCols - textLength) / 2; // posisi mulai agar di tengah
lcd.setCursor(startCol, row);
lcd.print(text);
}
void setup() {
Serial.begin(9600);
Wire.begin();
lcd.init();
lcd.backlight();
lcd.clear();
dht.begin();
printCenter("Weather Station", 1);
printCenter("Mini", 2);
delay(2000);
}
void loop() {
float kelembapan = dht.readHumidity();
float suhu = dht.readTemperature();
float nilai = analogRead(LDR);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Suhu: " + String(nilai) + " °C");
lcd.setCursor(0, 2);
lcd.print("Kelembapan: " + String(nilai) + " %");
lcd.setCursor(0, 3);
lcd.print("Cahaya: " + String(nilai) + " Lux");
}