#include <DHT.h>
#include <Servo.h>
#define DHTPIN 7
#define DHTTYPE DHT22
//previous value of temperature
double T_pre = -100;
//previous value of humidity
double H_pre = 0;
//if temperature or humidity changes by 1 degree or 1%, it rotates 10 degrees
int degree = 10;
DHT dht(DHTPIN, DHTTYPE);
Servo lacingUnit;
void setup() {
Serial.begin(9600);
dht.begin();
lacingUnit.attach(9);
}
void loop() {
double humi = dht.readHumidity();
double temp = dht.readTemperature();
if(temp != T_pre || humi != H_pre ){
//how temperature change
int n = (temp - T_pre) / degree;
//how humidity change
int hn = (humi - H_pre) / degree;
//lacing unit starts wokring depend on temperature and humidty
lacingUnit.write((n+hn) * degree);
}
//store temp to T_pre for next round
T_pre = temp;
//store humi to T_pre for next round
H_pre = humi;
Serial.print("T: ");
Serial.print(temp);
Serial.print("°C");
Serial.print("%\t");
Serial.print("H: ");
Serial.print(humi);
Serial.print("\n");
//detect temperature and humidity every 30s
delay(30000);
}