#include <DHT.h>
#include <Servo.h>
#define DHTPIN 4
#define DHTTYPE DHT22
int SERVO = 3;
DHT dht(DHTPIN, DHTTYPE);
Servo servo;
int pos = 0;
void setup() {
Serial.begin(38400); // Initialize the serial port
dht.begin();
servo.attach(SERVO);
delay(2000);
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(h)) {
Serial.println("Failed to read from DHT22 sensor!");
return;
}
if (h <= 40) {
Serial.print("Start Humidifier");
Serial.print(" \t\t");
Serial.print(h);
Serial.print(" %\t");
servo.write(180);
delay(6000);
servo.write(0);
Serial.print("Stop Humidifier");
Serial.print(" \t");
}
// Wait a few seconds between measurements. The DHT22 should not be read at a higher frequency of
// about once every 2 seconds. So we add a 3 second delay to cover this.
delay(3000);
}