#include <DHT22.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
// Create an instance of the DHT11 class and set the digital I/O pin.
DHT22 dht22(3);
// Change to DHT22 if you're using DHT22
#define FANPIN 6 // PWM pin where the fan is connected
void setup() {
Serial.begin(9600);
//dht.begin();
pinMode(2, OUTPUT);
myservo.attach(6);
}
void loop() {
float temperature = dht22.getTemperature(); // Get temperature value
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
if (temperature > 30) {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
// If temperature is less than 25°C
digitalWrite(2, HIGH); // Fan off
} else{ // If temperature is between 25°C and 27°C
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(2, LOW); // Fan at low speed (40% of 255)
}
delay(2000); // Wait for 2 seconds
}