#include <DHT.h>
#include <Servo.h>
#define DHTPIN 9 // Define the pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 sensor type
#define SERVO_PIN 5 // Define the pin connected to the servo motor
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
Servo myservo; // Initialize servo motor
int targetAngle = 0; // Initial Angle
void setup() {
Serial.begin(9600);
Serial.println(F("Smart_fan test!"));
dht.begin();
myservo.attach(SERVO_PIN); // Attach the servo to the specified pin
myservo.write(targetAngle); // servo motor initial position
}
void loop() {
// Wait a few seconds between measurements.
delay(5000);
// Sensor reads humidity
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
int currentAngle = myservo.read(); // Get current servo position
// Temperature:
// Comfort zone: 25-30°C (77-86°F)
// Coastal regions: Average highs around 30-32°C (86-90°F)
// Highlands: Average highs around 15-18°C (59-64°F)
// Humidity:
// Generally: 60-90% (can vary significantly)
// Coastal areas: Higher humidity, often exceeding 70% year-round.
// Highlands: Lower humidity, especially during the dry season (can drop to 60%).
if (h >= 40.0 && t >= 20.0) {
Serial.println("Environment is Hot!");
// Set target angle to 90 degrees if the condition is true
targetAngle = 90;
// Move the servo towards the target angle smoothly
for (int i = currentAngle; i <= targetAngle; i++) {
myservo.write(i);
delay(10); // Adjust delay for desired speed
}
} else {
targetAngle = 0; // Set target angle back to 0 degrees otherwise
delay(5000*60); // cool down time for the tool
}
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.println();
}