#include <Servo.h>
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define SERVO_PIN 9 //Arduino pin that controlsthe servo motor
Servo servo_valve;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
servo_valve.attach(SERVO_PIN);
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
float h = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(h)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("Humidity: "));
Serial.print(h);
// Control the servo based on temperature
if (h > 30) {// Adjust the threshold as needed
Serial.println(F("Temperature is high => activate servo"));
servo_valve.write(180); // Rotate servo to 90 degrees
} else {
Serial.println(F("Temperature is low => deactivate servo"));
servo_valve.write(0);//Rotate servo to 0 degrees
}
}