#include "DHT.h"
#include <Servo.h>
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define SERVOPIN 9
DHT dht(DHTPIN, DHTTYPE);
Servo myservo; // create servo object to control a servo
int is_open;
const int sensorPin = A0;
const float GAMMA = 0.7;
const float RL10 = 50;
int pos = 0; // variable to store the servo position
void setup() {
Serial.begin(9600);
dht.begin();
myservo.attach(SERVOPIN);
is_open = false;
}
void loop() {
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;
}
// Serial.print(F("Humidity: "));
// Serial.print(humidity);
// Serial.print(F("% Temperature: "));
// Serial.print(temperature);
// Serial.println(F("°C "));
// Read the Photoresistor sensor value
int sensorReading = analogRead(sensorPin);
// Convert the analog value into lux value
float voltage = sensorReading / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, ( 1 / GAMMA));
if(!is_open && (humidity >= 60 || lux >= 300)){
// OPEN THE Umbrella
Serial.print(F("Openning"));
is_open = true;
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
}
else if(is_open && !(humidity >= 60 || lux >= 300)){
// CLOSE THE Umbrella
Serial.print(F("Closing"));
is_open = false;
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
}
// Wait a few seconds between measurements.
delay(2000);
}