// -------------------------------------------------------------
// Test Program: Hardware Control - Shoe Dryer (Group Test & Latching)
// -------------------------------------------------------------
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Set the LCD address to 0x27 untuk 16x2 atau 20x4 display
LiquidCrystal_I2C lcd(0x27, 20, 4);
// ===== SENSOR DHT22 =====
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// ===== DEFINISI RELAY (IN1 - IN4 di D3 - D6) =====
const int relay1 = 3; // IN1 (D3)
const int relay2 = 4; // IN2 (D4)
const int relay3 = 5; // IN3 (D5)
const int relay4 = 6; // IN4 (D6)
// Asumsi modul relay active-LOW
const int RELAY_ON = LOW;
const int RELAY_OFF = HIGH;
// Definisi Pin Komponen Lainnya
const int ledHijau = 7; // D7
const int ledKuning = 8; // D8
const int ledMerah = A2; // A2
const int ledBiru = 9; // D9
const int buzzer = A3; // A3
const int tombol1 = 12; // D12
const int tombol2 = 13; // D13
const int tombol3 = A0; // A0 (Analog pin sebagai Digital Input)
// Variabel untuk status Latching Tombol & Edge Detection
bool lastBtn1State = LOW;
bool btn1Latch = false;
bool lastBtn2State = LOW;
bool btn2Latch = false;
bool lastBtn3State = LOW;
bool btn3Latch = false;
void setup() {
Serial.begin(9600);
// Inisialisasi Pin Relay sebagai Output & matikan di awal
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
digitalWrite(relay1, RELAY_OFF);
digitalWrite(relay2, RELAY_OFF);
digitalWrite(relay3, RELAY_OFF);
digitalWrite(relay4, RELAY_OFF);
// Inisialisasi Pin LED & Buzzer sebagai Output
pinMode(ledHijau, OUTPUT);
pinMode(ledKuning, OUTPUT);
pinMode(ledMerah, OUTPUT);
pinMode(ledBiru, OUTPUT);
pinMode(buzzer, OUTPUT);
// Inisialisasi Pin Tombol (Pull-down eksternal)
pinMode(tombol1, INPUT);
pinMode(tombol2, INPUT);
pinMode(tombol3, INPUT);
// Inisialisasi Sensor DHT22 & LCD
dht.begin();
lcd.init();
lcd.backlight();
// Skenario 1: Tampilan Awal sesuai permintaan
lcd.setCursor(0, 0);
lcd.print("Test Hardware");
lcd.setCursor(0, 1);
lcd.print("Shoe Dryer");
delay(2500);
// Skenario 2: Membaca Sensor DHT22 secara live selama 5 detik
unsigned long dhtStartTime = millis();
while (millis() - dhtStartTime < 5000) {
float t = dht.readTemperature();
float h = dht.readHumidity();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Test DHT22");
lcd.setCursor(0, 1);
if (isnan(t) || isnan(h)) {
lcd.print("Sensor: ERROR!");
} else {
char tbuf[8];
char hbuf[8];
dtostrf(t, 4, 1, tbuf);
dtostrf(h, 4, 0, hbuf);
char buf[20];
snprintf(buf, sizeof(buf), "T:%sC H:%s%%", tbuf, hbuf);
lcd.print(buf);
}
delay(1000);
}
// Skenario 3: Test Relay Serentak (IN1 - IN4 ON Bersamaan)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Test Relays");
lcd.setCursor(0, 1);
lcd.print("All Relays ON...");
delay(500);
digitalWrite(relay1, RELAY_ON);
delay(500);
digitalWrite(relay2, RELAY_ON);
delay(500);
digitalWrite(relay3, RELAY_ON);
delay(500);
digitalWrite(relay4, RELAY_ON);
delay(2000);
lcd.setCursor(0, 1);
lcd.print("All Relays OFF ");
digitalWrite(relay1, RELAY_OFF);
digitalWrite(relay2, RELAY_OFF);
digitalWrite(relay3, RELAY_OFF);
digitalWrite(relay4, RELAY_OFF);
delay(1000);
// Skenario 4: Test LED Indikator Serentak (Semua LED ON Bersamaan)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Test LEDs");
lcd.setCursor(0, 1);
lcd.print("All LEDs ON...");
digitalWrite(ledHijau, HIGH);
digitalWrite(ledKuning, HIGH);
digitalWrite(ledMerah, HIGH);
digitalWrite(ledBiru, HIGH);
delay(1200);
lcd.setCursor(0, 1);
lcd.print("All LEDs OFF ");
digitalWrite(ledHijau, LOW);
digitalWrite(ledKuning, LOW);
digitalWrite(ledMerah, LOW);
digitalWrite(ledBiru, LOW);
delay(800);
// Skenario 5: Pengujian Buzzer singkat
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Test Buzzer");
lcd.setCursor(0, 1);
lcd.print("Beep...");
tone(buzzer, 1000);
delay(1000);
noTone(buzzer);
// Skenario 6: Masuk ke Mode Interaktif (Tombol Latching)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Test Tombol");
lcd.setCursor(0, 1);
lcd.print("T1:OFF T2:OFF T3:OFF");
delay(1500);
lcd.clear();
}
void loop() {
// Membaca status fisik tombol (Pull-down eksternal: HIGH saat ditekan)
bool reading1 = (digitalRead(tombol1) == HIGH);
bool reading2 = (digitalRead(tombol2) == HIGH);
bool reading3 = (digitalRead(tombol3) == HIGH);
// Logika Latching (Toggle) Tombol 1
if (reading1 && !lastBtn1State) {
btn1Latch = !btn1Latch; // Balik status (toggle)
delay(50); // Debounce sederhana
}
lastBtn1State = reading1;
// Logika Latching (Toggle) Tombol 2
if (reading2 && !lastBtn2State) {
btn2Latch = !btn2Latch;
delay(50);
}
lastBtn2State = reading2;
// Logika Latching (Toggle) Tombol 3
if (reading3 && !lastBtn3State) {
btn3Latch = !btn3Latch;
delay(50);
}
lastBtn3State = reading3;
// Update Tampilan LCD Baris 1 & 2 untuk Mode Interaktif
lcd.setCursor(0, 0);
lcd.print("Test Tombol");
lcd.setCursor(0, 1);
lcd.print("T1:");
lcd.print(btn1Latch ? "ON " : "OFF");
lcd.print(" T2:");
lcd.print(btn2Latch ? "ON " : "OFF");
lcd.print(" T3:");
lcd.print(btn3Latch ? "ON " : "OFF");
// Kontrol aktuator/LED berdasarkan status latching tombol
digitalWrite(ledHijau, btn1Latch ? HIGH : LOW);
digitalWrite(ledKuning, btn2Latch ? HIGH : LOW);
digitalWrite(ledMerah, btn3Latch ? HIGH : LOW);
// Contoh opsional: Relay 1 ikut merespon Latching Tombol 1
digitalWrite(relay1, btn1Latch ? RELAY_ON : RELAY_OFF);
delay(30);
}