//Welcome in the project Moch Rijki Supriyatna:("MrS_Iky")
//Smart Home Multi-Sensor System with IR Remote Control & Alarm
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <Servo.h>
#include <IRremote.h>
// --- Konfigurasi Pin ---
#define DHTPIN 7
#define DHTTYPE DHT22
#define MQ2_PIN A0
#define TRIG_PIN 8
#define ECHO_PIN 9
#define BUZZER_PIN 10
#define LED_PIN 11
#define SERVO_PIN 6
#define IR_PIN 2
// --- Inisialisasi Komponen ---
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myServo;
IRrecv irrecv(IR_PIN);
decode_results results;
// --- Variabel Sistem ---
unsigned long lastLogTime = 0;
bool alarmActive = false;
int distance = 0;
void setup() {
Serial.begin(9600);
dht.begin();
lcd.begin(16, 2);
lcd.backlight();
myServo.attach(SERVO_PIN);
pinMode(MQ2_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
irrecv.enableIRIn();
lcd.setCursor(0, 0);
lcd.print("Smart System");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
}
void loop() {
// --- IR Remote ---
if (irrecv.decode(&results)) {
long key = results.value;
if (key == 0xFFA25D) { // tombol Power
alarmActive = !alarmActive;
lcd.setCursor(0, 0);
lcd.print("Alarm: ");
lcd.print(alarmActive ? "ON " : "OFF");
} else if (key == 0xFF629D) { // Vol+
lcd.setCursor(0, 1);
lcd.print("Open Door ");
myServo.write(90);
} else if (key == 0xFFE21D) { // Vol-
lcd.setCursor(0, 1);
lcd.print("Close Door ");
myServo.write(0);
}
irrecv.resume();
}
// --- Sensor Ultrasonik ---
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
// --- Sensor DHT22 ---
float h = dht.readHumidity();
float t = dht.readTemperature();
// --- Sensor Gas MQ2 ---
int gasValue = analogRead(MQ2_PIN);
// --- Cek Alarm ---
if (alarmActive && (distance < 20 || gasValue > 300)) {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
lcd.setCursor(0, 1);
lcd.print("!!! ALERT !!! ");
} else {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
}
// --- LCD Output ---
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(t, 1);
lcd.print("C ");
lcd.print("H:");
lcd.print(h, 0);
lcd.print("%");
// --- Logging ke Serial tiap 10 detik ---
if (millis() - lastLogTime >= 10000) {
Serial.print("Log >> Temp: ");
Serial.print(t);
Serial.print("C | Hum: ");
Serial.print(h);
Serial.print("% | Gas: ");
Serial.print(gasValue);
Serial.print(" | Jarak: ");
Serial.print(distance);
Serial.println(" cm");
lastLogTime = millis();
}
delay(500);
}