#include <Servo.h>;
#include <DHT.h>;
#include <Wire.h>;
#include <LiquidCrystal_I2C.h>;
byte pos , time1=0 , time2=0 , led0=0 , led1=0 , led2=0 , led3=0 , all , led12=0 , led23=0 ;
byte interval1=15 , interval2=10;
float temp , hum ;
DHT datchic (2,DHT22); //Подключение датчика
Servo servo;
LiquidCrystal_I2C LCD (0x27 , 16 ,2 ); //Сколько символов
void setup() 
{
 Serial.begin(9600);
 datchic.begin();
 LCD.init();
 LCD.backlight();
 LCD.setCursor(0,0);
 LCD.print("Temp=");
 LCD.setCursor(0,1);
 LCD.print("Hum=");
 pinMode(3, OUTPUT); //Подключение серво
 servo.attach(3);
 pinMode(13, OUTPUT);
 pinMode(12, OUTPUT);
 pinMode(11, OUTPUT);
 }

void loop() 
{
  temp=datchic.readTemperature();
  hum=datchic.readHumidity(); 
  Serial.print("temp=");
  Serial.print(temp);
  Serial.print("   hum=");
  Serial.print(hum);
  Serial.print("   Time1=");
  Serial.print(time1);
  Serial.print("   Time2=");
  Serial.print(time2);
  Serial.print(" led0=");
  Serial.print(led0);
  Serial.print(" led1=");
  Serial.print(led1);
  Serial.print(" led2=");
  Serial.print(led2);
  Serial.print(" led3=");
  Serial.print(led3);
  Serial.print(" led12=");
  Serial.print(led12);
  Serial.print(" led23=");
  Serial.print(led23);
  Serial.print(" all=");
  Serial.print(all);
  LCD.setCursor (5,0);
  LCD.print(temp);
  LCD.print(char (223));
  LCD.print("  ");
  LCD.setCursor (4,1);
  LCD.print(hum);
  LCD.print("%");
  if(hum<100)
   {
     LCD.setCursor (10,1);
     LCD.print(" ");
    }

   if ((time2==0)||(time2>interval2))
   {
    if((temp>=20)&&(temp<25)&&(led12==0))
    {
     led0=0;
     led1=0;
     led2=0;
     led3=0;
     led12=1;
     led23=0;
     all=0;
     time1=0;
     time2=0;
     digitalWrite(13,1);
     digitalWrite(12,1);
     digitalWrite(11,0);
     LCD.setCursor(12, 0);              
     LCD.print("ON");
    }
     if((temp>25)&&(temp<30)&&(led23==0))
     { 
      led0=0;
      led1=0;
      led2=0;
      led3=0;
      led12=0;
      led23=1;
      all=0;
      time1=0;
      time2=0;
      digitalWrite(13,0);
      digitalWrite(12,1);
      digitalWrite(11,1);
      LCD.setCursor(12, 0);              
      LCD.print("ON");
    }
      if((temp==25)&&(all==0))
      {
       led0=0;
       led1=0;
       led2=0;
       led3=0;
       led12=0;
       led23=0;
       all=1;
       time1=0;
       time2=0;
       digitalWrite(13,1);
       digitalWrite(12,1);
       digitalWrite(11,1);
       LCD.setCursor(12, 0);              
       LCD.print("ON");
      }
   }



   if ((time1==0)||(time1>interval1))
   {
     if((temp<15)&&(led0==0))
     {
       led0=1;
       led1=0;
       led2=0;
       led3=0;
       led12=0;
       led23=0;
       all=0;
       time1=0;
       time2=0;
       digitalWrite(11,0);
       digitalWrite(12,0);
       digitalWrite(13,0);
       LCD.setCursor(12,0);
       LCD.print("OFF");
      }
      if((temp>=15)&&(temp<20)&&(led1==0))
      {
       led1=1;
       led0=0;
       led2=0;
       led3=0;
       led12=0;
       led23=0;
       all=0;
       time1=0;
       time2=0;
       digitalWrite(13,1);
       digitalWrite(12,0);
       digitalWrite(11,0);
       LCD.setCursor(12, 0);              
       LCD.print("ON1");
      }
      if((temp>=30)&&(led3==0))
      {
       led3=1;
       led0=0;
       led1=0;
       led2=0;
       led12=0;
       led23=0;
       all=0;
       time1=0;
       time2=0;
       digitalWrite(11,1);
       digitalWrite(12,0);
       digitalWrite(13,0);
       LCD.setCursor(12, 0);              
       LCD.print("ON3");  
      }
    }
   
   if(pos<100)
   {
     LCD.setCursor (15,1);
     LCD.print(" ");
   }
   LCD.setCursor (13,1);
   LCD.print(pos);
   servo.write(pos);
   if(hum<40)
   {
     pos=0;
     LCD.setCursor (14,1);
     LCD.print("  ");
    }
   else
   {
     pos=map(hum,40,100,0,180);
    }
   
   Serial.print("   pos=");
   Serial.println(pos);
   delay (1000);
   if ((led1==1)||(led0==1)||(led3==1))
  {
   time1++;
   time2++;
  }
  if((led12==1)||(led23==1)||(all==1))
  {
    time1++;
    time2++;
  }
}