#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
//==================================================
// KONFIGURASI DHT22 & LCD
//==================================================
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
//==================================================
// DEKLARASI PIN
//==================================================
const int ldrPin = A0;
const int pirPin = 3;
const int buttonPin = 4;
const int lampPin = 5;
const int fanPin = 6;
const int alarmLed = 7;
const int buzzerPin = 8;
//==================================================
// VARIABEL GLOBAL
//==================================================
int manualMode = 0; // 0: Auto, 1: Manual ON, 2: Manual OFF
const int ldrThreshold = 500;
unsigned long lastLCD = 0;
unsigned long lastSerial = 0; // Variabel penanda waktu untuk Serial Monitor
byte lcdPage = 0;
void setup() {
Serial.begin(9600);
dht.begin();
lcd.init();
lcd.backlight();
pinMode(ldrPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(lampPin, OUTPUT);
pinMode(fanPin, OUTPUT);
pinMode(alarmLed, OUTPUT);
pinMode(buzzerPin, OUTPUT);
lcd.setCursor(0,0);
lcd.print("SMART CLASSROOM");
lcd.setCursor(0,1);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
}
void loop() {
// MEMBACA DATA SENSOR
float suhu = dht.readTemperature();
float hum = dht.readHumidity();
int ldrValue = analogRead(ldrPin);
bool adaOrang = digitalRead(pirPin);
//==================================================
// SOAL E: MODE MANUAL ( TIMEOUT 2 DETIK)
//==================================================
static bool lastButton = HIGH;
bool button = digitalRead(buttonPin);
static unsigned long waktuTombolDitekan = 0;
if (lastButton == HIGH && button == LOW) {
manualMode++;
if (manualMode > 2) manualMode = 0;
waktuTombolDitekan = millis();
delay(250); // Debounce sederhana
}
lastButton = button;
// Kembali otomatis ke mode 0 setelah 2 detik (2000 ms)
if (manualMode != 0 && (millis() - waktuTombolDitekan >= 2000)) {
manualMode = 0;
}
//==================================================
// SOAL A: KONTROL LAMPU
//==================================================
bool lampStatus;
if (manualMode == 1) {
lampStatus = true;
} else if (manualMode == 2) {
lampStatus = false;
} else {
lampStatus = (ldrValue < ldrThreshold && adaOrang);
}
digitalWrite(lampPin, lampStatus);
//==================================================
// SOAL B: VENTILASI OTOMATIS
//==================================================
bool fanStatus = (!isnan(suhu) && suhu > 30);
digitalWrite(fanPin, fanStatus);
//==================================================
// SOAL C: ALARM SUHU TINGGI
//==================================================
bool alarmStatus = (!isnan(suhu) && suhu > 35);
digitalWrite(alarmLed, alarmStatus);
if (alarmStatus) tone(buzzerPin, 1000);
else noTone(buzzerPin);
//==================================================
// SOAL D: TAMPILAN LCD
//==================================================
if (millis() - lastLCD >= 3000) {
lastLCD = millis();
lcd.clear();
switch (lcdPage) {
case 0:
lcd.setCursor(0,0);
lcd.print("Suhu:");
lcd.print(suhu,1);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Hum:");
lcd.print(hum,0);
lcd.print("%");
break;
case 1:
lcd.setCursor(0,0);
lcd.print("Lampu:");
lcd.print(lampStatus ? "ON " : "OFF");
lcd.setCursor(0,1);
lcd.print("Kipas:");
lcd.print(fanStatus ? "ON " : "OFF");
break;
case 2:
lcd.setCursor(0,0);
lcd.print(adaOrang ? "Ada Orang" : "Kosong ");
lcd.setCursor(0,1);
if (manualMode == 1) lcd.print("Mode: Man ON ");
else if (manualMode == 2) lcd.print("Mode: Man OFF");
else lcd.print("Mode: Auto ");
break;
}
lcdPage++;
if(lcdPage > 2) lcdPage = 0;
}
//==================================================
//SERIAL MONITOR
//==================================================
if (millis() - lastSerial >= 1000) {
lastSerial = millis();
Serial.println("=== MONITOR SMART CLASSROOM ===");
Serial.print("Suhu : "); Serial.print(suhu, 1); Serial.println(" *C");
Serial.print("Kelembapan : "); Serial.print(hum, 0); Serial.println(" %");
Serial.print("Nilai LDR : "); Serial.println(ldrValue);
Serial.print("PIR Sensor : "); Serial.println(adaOrang ? "Ada Gerakan" : "Tidak Ada Gerakan");
Serial.print("Mode Sistem: ");
if (manualMode == 1) Serial.println("MANUAL ON (Timeout 2s)");
else if (manualMode == 2) Serial.println("MANUAL OFF (Timeout 2s)");
else Serial.println("AUTO");
Serial.print("Status Lampu: "); Serial.println(lampStatus ? "AKTIF (ON)" : "MATI (OFF)");
Serial.print("Status Kipas: "); Serial.println(fanStatus ? "AKTIF (ON)" : "MATI (OFF)");
Serial.println("===============================\n");
}
delay(50);
}