/* How to use the DHT-22 sensor with Arduino uno
Temperature and humidity sensor
More info: http://www.ardumotive.com/how-to-use-dht-22-sensor-en.html
Dev: Michalis Vasilakis // Date: 1/7/2015 // www.ardumotive.com */
//Libraries
#include <DHT.h>;
#include <Wire.h> // I2C程式庫
#include <LiquidCrystal_I2C.h> // LCD_I2C模組程式庫
#include <Servo.h>
//Constants
#define DHTPIN 7 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
DHT dht2(A5, DHTTYPE);
// LCD I2C位址,默認為0x27或0x3F,依據背板的晶片不同而有差異,16、2為LCD顯示器大小。
LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo myservo; // create servo object to control a servo
//Variables
float hum; //Stores humidity value
float temp; //Stores temperature value
float device_temp; //Stores temperature of device
int status = 0;
void setup()
{
Serial.begin(9600);
dht.begin();
dht2.begin();
myservo.attach(9);
}
void loop()
{
//read the humidity and temperature values from the DHT sensor and store them to variables
hum = dht.readHumidity(); //humidity value
temp= dht.readTemperature();//temperature value
Serial.println("Humidity: "+String(hum,2)+" % "+"Temp: "+String(temp,2)+" Celsius");//print values
delay(2000); //Delay 2 sec.
if((hum>65 && temp>30) && dht2.readTemperature()<80){//if humidity>65% and temperature>30, and the fan is not too hot
myservo.write(map(2, 0, 2, 90, 180));//rotates servo to adjust the fan to mode 2
status = 2;
}else if((hum>65 || temp>30) && dht2.readTemperature()<80){//if humidity>65% or temperature>30
myservo.write(map(1, 0, 2, 90, 180));//rotates servo to adjust the fan to mode 1
}else{//humidity<65% and temperature<30 or the fan is too hot
myservo.write(map(0, 0, 2, 90, 180));//rotates servo to turn off the fan
}
}
//https://www.instructables.com/How-to-use-DHT-22-sensor-Arduino-Tutorial/
//https://crazymaker.com.tw/arduino-lcd-i2c-tutorial/