#include "DHT.h"
#include <Servo.h>

#define DHTPIN 5    
#define DHTTYPE DHT22  

#define LDR_PIN 2

const float GAMMA = 0.7;
const float RL10 = 50;

Servo myservo; 

int pos = 0;   

DHT dht(DHTPIN, DHTTYPE);
void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));
  pinMode(4, OUTPUT);
  pinMode(2, OUTPUT);
  myservo.attach(10);  
  dht.begin();
}

void loop() {
  delay(2000);

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);


   int analogValue = analogRead(A0);
  float voltage = analogValue / 1024. * 5;
  float resistance = 2000 * voltage / (1 - voltage / 5);
  float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));

  if (lux < 500) {
      digitalWrite(4,HIGH);
  }
  
  if(lux > 1200 ){
          digitalWrite(4,LOW);
  }

  if(h < 50 ){
     digitalWrite(2,HIGH);
  }

   if(h > 75 ){
     digitalWrite(2,LOW);
  }

  if(t >= 31  ){
          
    for (pos = 0; pos <= 180; pos += 1) { 
    myservo.write(pos);              
  }
  for (pos = 180; pos >= 0; pos -= 1) { 
    myservo.write(pos);              
    delay(15);                      
  }
}

if(t <= 14  ){
      digitalWrite(4,HIGH);
  }
  
if(t > 28  ){
      digitalWrite(4,LOW);
  }
  







  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));
  delay(1000);










}