#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <Servo.h> // Tambahkan pustaka Servo
#define DHTPIN 3 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
Servo myservo; // Buat objek Servo
const int pirPin = 2; // PIR sensor connected to digital pin 2
const int redLedPin = 4; // Pin for the red LED
const int buzzerPin = 2; // Pin for the buzzer
const int servoPin = 5; // Pin for the servo
void setup() {
pinMode(pirPin, INPUT);
pinMode(redLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
myservo.attach(servoPin); // Attach servo to pin
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Check humidity
if (humidity > 40) {
digitalWrite(redLedPin, HIGH); // Turn on the red LED
} else {
digitalWrite(redLedPin, HIGH); // Turn off the red LED
}
// Check temperature
if (temperature > 40) {
// Sound the buzzer for half a second
digitalWrite(redLedPin, HIGH); // Turn on the red LED
tone(buzzerPin, 1000);
delay(1000);
noTone(buzzerPin);
// Move the servo to a certain position
myservo.write(90); // Example position, adjust as needed
// Wait for another half a second
delay(500);
} else {
noTone(buzzerPin); // Turn off the buzzer
digitalWrite(redLedPin, LOW); // Turn off the red LED
// Move the servo to another position
myservo.write(0); // Example position, adjust as needed
}
}