#include <Servo.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define SERVO_PIN 2
DHT dht(DHTPIN, DHTTYPE);
Servo mainServo;
int position = 0;
int previousPosition;
float tempCelsius; // temperature in Celsius
float tempFahrenheit; // temperature in Fahrenheit
void setup() {
Serial.begin(115200);
Serial.println(F("DHT22 example!"));
dht.begin();
mainServo.attach(SERVO_PIN);
}
void loop() {
//read from dht sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
tempCelsius = temperature; // read temperature in Celsius
tempFahrenheit = tempCelsius * 9 / 5 + 32; // convert Celsius to Fahrenheit
Serial.print("Temperature: ");
Serial.print(tempCelsius); // print the temperature in Celsius
Serial.print("°C");
Serial.print(" ~ "); // separator between Celsius and Fahrenheit
Serial.print(tempFahrenheit); // print the temperature in Fahrenheit
Serial.println("°F");
delay(500);
//int position = map(tempFahrenheit, 50, 80, 0, 180);
if (tempFahrenheit < 80) {
position = 0;
} else
{
position = 180;
}
Serial.println(position);
if (previousPosition != position) {
previousPosition = position;
mainServo.write(position);
delay(1000);
}
}