#include <Servo.h>
#include "DHT.h"
int Pir = 8;
int buzzerPin = 9;
int statusPir = 0;
#define DHTPIN 13
#define DHTTYPE DHT22
Servo myservo;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// put your setup code here, to run once
Serial.begin(9600);
myservo.attach(6);
pinMode(Pir, INPUT);
pinMode(buzzerPin, OUTPUT);
dht.begin();
}
void loop() {
statusPir = digitalRead(Pir);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
if(statusPir == LOW){
myservo.write(90); // Turn Servo ke kiri to 0 degrees
delay(1000);
noTone(buzzerPin);
}
else{
myservo.write(270); // Turn Servo ke posisi center position (90 degrees)
delay(1000);
tone(buzzerPin, 50);
Serial.println("Door Closed");
}
delay(1000);
}