#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// 1. DEKLARASI PIN & DEVINISI SENSOR
#define MQ2PIN 13
#define DHTPIN 14
#define DHTTYPE DHT22
#define buzz 12
#define ledmerah 2
#define ledhijau 4
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
// VARIABEL GLOBAL
int suhu;
int humid;
int gas;
String statusSystem = "";
// 2. VOID SETUP
void setup() {
Serial.begin(115200);
pinMode(buzz, OUTPUT);
pinMode(ledmerah, OUTPUT);
pinMode(ledhijau, OUTPUT);
dht.begin();
lcd.init();
lcd.backlight();
}
// 3. VOID LOOP
void loop() {
bacaSensor(); // 1. Ambil data
cekKondisi(); // 2. Evaluasi status logika
kontrolOutput(); // 3. Eksekusi LED & Buzzer
tampilLCD(); // 4. Update tampilan layar
serialMonitor(); // 5. Cetak log ke Serial
}
// 4. VOID BACA SENSOR
void bacaSensor() {
suhu = dht.readTemperature();
humid = dht.readHumidity();
int rawgas = analogRead(MQ2PIN);
gas = map(rawgas, 0, 4095, 0, 100);
}
// 5. VOID CEK KONDISI (Khusus Penentuan Status Logika)
void cekKondisi() {
if (gas > 60) {
statusSystem = "KRITIS_GAS";
}
else if (suhu > 30 && humid > 70) {
statusSystem = "BAHAYA_KEDUA"; // Suhu & Humid bermasalah
}
else if (suhu > 30) {
statusSystem = "BAHAYA_SUHU"; // Hanya Suhu bermasalah
}
else if (humid > 70) {
statusSystem = "BAHAYA_HUMID"; // Hanya Humid bermasalah
}
else {
statusSystem = "NORMAL";
}
}
// 6. VOID KONTROL OUTPUT (Khusus LED & Buzzer)
void kontrolOutput() {
if (statusSystem == "KRITIS_GAS") {
digitalWrite(ledhijau, LOW);
digitalWrite(ledmerah, HIGH); // LED Merah ON Full
tone(buzz, 5000); // Buzzer ON Full
}
else if (statusSystem == "BAHAYA_KEDUA" || statusSystem == "BAHAYA_SUHU" || statusSystem == "BAHAYA_HUMID") {
digitalWrite(ledhijau, LOW);
// Buzzer & LED Merah Kedip-kedip (Blinking)
digitalWrite(ledmerah, HIGH);
tone(buzz, 2000);
delay(200);
digitalWrite(ledmerah, LOW);
noTone(buzz);
delay(200);
}
else { // NORMAL
digitalWrite(ledhijau, HIGH); // LED Hijau Nyala
digitalWrite(ledmerah, LOW);
noTone(buzz); // Buzzer OFF
delay(300);
}
}
// 7. VOID TAMPIL LCD (Khusus Mengurus Tampilan Layar)
void tampilLCD() {
lcd.clear();
if (statusSystem == "KRITIS_GAS") {
lcd.setCursor(0, 0);
lcd.print(" GAS KRITIS ");
lcd.setCursor(0, 1);
lcd.print("Gas: "); lcd.print(gas); lcd.print("%");
}
else if (statusSystem == "BAHAYA_KEDUA") {
lcd.setCursor(0, 0);
lcd.print(" STATUS: BAHAYA ");
lcd.setCursor(0, 1);
lcd.print("S:"); lcd.print(suhu); lcd.print((char)223); lcd.print("C ");
lcd.print("H:"); lcd.print(humid); lcd.print("%");
}
else if (statusSystem == "BAHAYA_SUHU") {
lcd.setCursor(0, 0);
lcd.print(" STATUS: BAHAYA ");
lcd.setCursor(0, 1);
lcd.print("Suhu High: "); lcd.print(suhu); lcd.print((char)223); lcd.print("C");
}
else if (statusSystem == "BAHAYA_HUMID") {
lcd.setCursor(0, 0);
lcd.print(" STATUS: BAHAYA ");
lcd.setCursor(0, 1);
lcd.print("Humid HIGH: "); lcd.print(humid); lcd.print("%");
}
else { // NORMAL
lcd.setCursor(0, 0);
lcd.print("S:"); lcd.print(suhu); lcd.print((char)223); lcd.print("C ");
lcd.print("H:"); lcd.print(humid); lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Gas:"); lcd.print(gas); lcd.print("% [NORMAL]");
}
}
// 8. VOID SERIAL MONITOR (Khusus Log Debugging)
void serialMonitor() {
Serial.println("=====================");
Serial.print("Suhu : "); Serial.print(suhu); Serial.println(" C");
Serial.print("Lembab : "); Serial.print(humid); Serial.println(" %");
Serial.print("Gas : "); Serial.print(gas); Serial.println(" %");
Serial.print("Status : "); Serial.println(statusSystem);
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4