#include <Servo.h>
#include <DHT.h>
Servo servo; //creates a servo object
float angle = 0;
float degreeToAngle = 1.5; //each degree is 1.5 degrees on the servo
#define DHTPIN 7 //assignes the pin on the arduino that the sensor connects to
#define DHTTYPE DHT22 //specifies the type of sensor used
DHT dht(DHTPIN, DHTTYPE); //creates na instance of the dht object
float temperature;
void setup() {
Serial.begin(9600); //starts serial communication at 9600 baud rate
servo.attach(5); //attaches the servo to pin 5 on the arduino
dht.begin(); //initialisees the dht sensor
}
void loop() {
// put your main code here, to run repeatedly:
temperature = dht.readTemperature(); //reads the current temperature from thet sensor and stores in temperature variable
Serial.print("Temperature: ");
Serial.print(temperature); //prints out the temperature to the console
Serial.println(" °C");
if (temperature <= 0)
{
angle = abs(-40 + abs(temperature)) * degreeToAngle; //finds the angle for the temperature when it is less than 0
} else if(temperature > 0)
{
angle = 60 + (temperature * degreeToAngle); //finds thet angle when the temperature is above 0
}
servo.write(angle); //moves the servo to that angle
//angle = map(temperature, -40, 80, 0, 180); //maps each degree c to an angle in the range -40 to 80 degrees to 0 to 180
Serial.println(angle);
delay(1000);
}