#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <DHT.h>
#include <DHT_U.h>
#include <Servo.h>

RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27,16,2);
#define DHTPIN 2 
#define DHTTYPE DHT22
char daysOfTheWeek[7][3] = {"Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab"};
DHT dht (DHTPIN, DHTTYPE);

Servo servo1;
const int PIN_SERVO = 10;
const int LED_R = 9;
const int LED_V = 8;
const int LED_A = 7;

void setup () {
  Serial.begin(9600);

  dht.begin();

    if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
 lcd.init();
 lcd.backlight();
 lcd.print("");

 pinMode(LED_R, OUTPUT);
 pinMode(LED_V, OUTPUT);
 pinMode(LED_A, OUTPUT);
 servo1.attach(PIN_SERVO);


}

void loop () {  //MOSTRAR FECHA Y HORA EN LA PRIMER FILA
    DateTime now = rtc.now();
//lcd.print(now.year() % 100); // Mostrar el año en 2 dígito
   lcd.setCursor(0,0);
   lcd.print(now.year() % 100); // Mostrar el año en 2 dígito
   //lcd.print(now.year(), DEC);
   lcd.print('/');
   lcd.print(now.month(), DEC);
   lcd.print('/');
   lcd.print(now.day(), DEC);
   //lcd.print(" (");
   //lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
   //lcd.print(") ");

     lcd.setCursor(8,0);
    lcd.print(now.hour(), DEC);
    lcd.print(':');
    lcd.print(now.minute(), DEC);
    lcd.print(':');
    lcd.print(now.second(), DEC);
    lcd.println();

    //MOSTRAR TEMPERATURA Y HUMEDAD EN LA SEGUNDA FILA
    // Lectura de temperatura y humedad 


  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  if (isnan(temperature) || isnan(humidity)) {
    lcd.print("Error DHT22");
  } else {
    lcd.setCursor(0,1);
    lcd.print("T:");
    lcd.print(temperature);
    //lcd.print(" C ");
  }
   if(temperature <5){
    digitalWrite(LED_R, HIGH);
    digitalWrite(LED_V, LOW);
    digitalWrite(LED_A, LOW);
    servo1.write(0);
    } else if (temperature > 5 && temperature <= 30) {
     digitalWrite(LED_R, LOW);
    digitalWrite(LED_V, HIGH);
    digitalWrite(LED_A, LOW);
    servo1.write(0); 
    }else if(temperature >30){
     digitalWrite(LED_R, LOW);
    digitalWrite(LED_V, HIGH);
    digitalWrite(LED_A, HIGH);
    servo1.write(360);  
    
   } 
    
    lcd.setCursor(8, 1); 
    lcd.print("H:");
    lcd.print(humidity);
    //lcd.print(" %");

  
    Serial.println();
    delay(3000);
}
$abcdeabcde151015202530fghijfghij
GND5VSDASCLSQWRTCDS1307+