#include "dht.h"
#define dht_apin A0 // Analog Pin sensor is connected to
#define capteur_lumPin A1 // Analog Pin sensor is connected to

#define ventilateurPin 2 
#define climatiseurPin 3
#define detectPluiePin 4     
#define playEssuiePin 5 
#define playHeadLightPin 6 


unsigned long previousMillis;
int interval = 50;
int analog_lum; 

int detectPluie;  //0 ou 1 si seuil atteint
 
dht DHT;
 
void setup()
{
  Serial.begin(9600);
  
  pinMode(detectPluiePin, INPUT); 
  pinMode(playEssuiePin, OUTPUT);  
  pinMode(ventilateurPin, OUTPUT); 
  pinMode(climatiseurPin, OUTPUT);
  pinMode(playHeadLightPin, OUTPUT);

  delay(1000); 
}
 
void loop()
{
    analog_lum = analogRead(capteur_lumPin);
    if(analog_lum > 100)
      digitalWrite(playHeadLightPin,HIGH);
    else
      digitalWrite(playHeadLightPin,LOW);
    

    DHT.read11(dht_apin);
    if(DHT.temperature > 27)
      digitalWrite(ventilateurPin,HIGH);
    else
      digitalWrite(ventilateurPin,LOW);


    detectPluie = digitalRead(detectPluiePin);
    if (detectPluie==1)
    {
        digitalWrite(playEssuiePin, HIGH);   // LED allumée
    }
    else 
    {
        digitalWrite(playEssuiePin, LOW);   // LED off
    }
    
    if (Serial.available() > 0) 
    {
        char i = 1;//Serial.read();
        switch (i) 
        {
            case '0':
                Serial.println("Tfi koulchi");
                digitalWrite(ventilateurPin,LOW);
                digitalWrite(climatiseurPin,LOW);
            break;
            
            case '1':
                Serial.println("khedem ferfara");
                digitalWrite(ventilateurPin,HIGH);
            break;
            case '2':
                Serial.println("Tfi ferfara");
                digitalWrite(ventilateurPin,LOW);
                digitalWrite(climatiseurPin,LOW);
            break;

            case '3':
               Serial.println("khedem clima");
               digitalWrite(climatiseurPin,HIGH);
            break;

            case '4':
                Serial.println("Tfi clima");
                digitalWrite(climatiseurPin,LOW);
            break;

            case '5':
                if (digitalRead(detectPluiePin) == 1)
                {
                    Serial.println("Kayna chta");
                    digitalWrite(playEssuiePin, HIGH);
                }
                else 
                {
                    Serial.println("Chta walo");
                    digitalWrite(playEssuiePin, LOW);
                }
            break;

            case '6':
                if(analog_lum > 100)
                {
                    Serial.println("Ta7 dlam");
                    digitalWrite(playHeadLightPin,HIGH);
                }
                else 
                {
                    Serial.println("Tle3 nhar");
                    digitalWrite(playHeadLightPin,LOW);
                }
            break;
        } 
    }

    if (millis() - previousMillis >= interval) 
    {
        previousMillis = millis();
        Serial.println(DHT.temperature);
    }
   
    delay(1000);
 }
