#include <LiquidCrystal.h>
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
#define PIR_PIN 9
#define LIGHT_PIN 6
#define TRIG_PIN 11
#define ECHO_PIN 12
#define LED_PIN 8
#define BUZZER_PIN 4
LiquidCrystal lcd(10, 5, 7, 3, 13, A1);
DHT dht(DHTPIN, DHTTYPE);
bool alarm = false;
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("RUMAH BIJARKSS");
delay(2000);
lcd.clear();
pinMode(PIR_PIN, INPUT);
pinMode(LIGHT_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
dht.begin();
}
void loop() {
int motion = digitalRead(PIR_PIN);
float temp = dht.readTemperature();
float hum = dht.readHumidity();
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.034 / 2;
int light = analogRead(A0);
String statusSec = "SAFE ";
alarm = false;
if (motion == HIGH) {
statusSec = "NEAR ";
alarm = true;
}
if (light > 400 ) {
statusSec = "DARK ";
alarm = true;
}
if (distance > 0 && distance < 30) {
statusSec = "CLOSE";
alarm = true;
}
if (temp > 40 && hum < 60) {
statusSec = "HOT ";
alarm = true;
}
// LCD Row 1
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temp, 0);
lcd.print(" H:");
lcd.print(hum, 0);
lcd.print("%");
// LCD Row 2
lcd.setCursor(0, 1);
lcd.print(statusSec);
lcd.print("dist ");
lcd.print(distance, 0);
lcd.print("cm ");
delay(2000);
if (alarm == true) {
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 1000);
delay(200);
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
delay(200);
}
else {
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
delay(1000);
}
}