#include <DHT.h>
#include <LiquidCrystal.h>

#define pinDatos 18      // DHT22 data pin
#define pinPulsador 4    // On button
#define pinPulsador2 5   // Off button
#define pinLed 2         // On LED
#define pinLed2 15       // Off LED
#define pinLed3 23       // New LED
#define trigPin 13       // HC-SR04 Trigger pin
#define echoPin 12       // HC-SR04 Echo pin

LiquidCrystal lcd(22, 21, 25, 26, 14, 27); // LCD Pins

DHT sensorTH(pinDatos, DHT22); // DHT Sensor

void setup() {
  Serial.begin(115200);
  lcd.begin(16, 2); // Initialize the LCD

  sensorTH.begin(); // Initialize DHT sensor
  pinMode(pinPulsador, INPUT);
  pinMode(pinPulsador2, INPUT);
  pinMode(pinLed, OUTPUT);
  pinMode(pinLed2, OUTPUT);
  pinMode(pinLed3, OUTPUT);  // New LED

  // Initialize the HC-SR04
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Read the state of the buttons
  int pulsadorState = digitalRead(pinPulsador);
  int pulsador2State = digitalRead(pinPulsador2);

  lcd.setCursor(0, 0); // Set cursor position for LCD
  if (pulsadorState == HIGH) {
    digitalWrite(pinLed, HIGH); // Turn on the On LED
    digitalWrite(pinLed2, LOW); // Turn off the Off LED

    float humedad = sensorTH.readHumidity();
    float temperatura = sensorTH.readTemperature();

    lcd.print("Temp: ");
    lcd.print(temperatura);
    lcd.print("C");

    lcd.setCursor(0, 1);
    lcd.print("Humedad: ");
    lcd.print(humedad);
    lcd.print("%");

    if (temperatura <= 24.0) {
      digitalWrite(pinLed3, HIGH); // Turn on the new LED
    } else {
      digitalWrite(pinLed3, LOW); // Turn off the new LED
    }

    // Measure distance with the HC-SR04
    float distancia = medirDistancia();
    Serial.print("Distance = ");
    Serial.print(distancia);
    Serial.println(" cm");
  } else if (pulsador2State == HIGH) {
    Serial.print("System Off <3 ");
    digitalWrite(pinLed2, HIGH); // Turn on the Off LED
    digitalWrite(pinLed, LOW); // Turn off the On LED
     digitalWrite(pinLed3, LOW); // Turn off the new LED

    lcd.clear(); // Clear the LCD screen
  } else {
    // If neither button is pressed, turn off both LEDs
    digitalWrite(pinLed, LOW);
    digitalWrite(pinLed2, LOW);
     digitalWrite(pinLed3, LOW); // Turn off the new LED

    lcd.clear(); // Clear the LCD screen
  }
}

float medirDistancia() {
  // Generar un pulso corto en el pin Trigger del HC-SR04
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);


  // Leer el tiempo que tarda en llegar el eco
  long tiempoEcho = pulseIn(echoPin, HIGH);
  
  // Calcular la distancia en centímetros
  float distancia = tiempoEcho / 58.2;
  
  return distancia;

}
$abcdeabcde151015202530fghijfghij
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
led1:A
led1:C
r1:1
r1:2
r2:1
r2:2
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
led2:A
led2:C
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
r3:1
r3:2
r4:1
r4:2
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
r5:1
r5:2
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
lcd1:VSS
lcd1:VDD
lcd1:V0
lcd1:RS
lcd1:RW
lcd1:E
lcd1:D0
lcd1:D1
lcd1:D2
lcd1:D3
lcd1:D4
lcd1:D5
lcd1:D6
lcd1:D7
lcd1:A
lcd1:K
led3:A
led3:C
r6:1
r6:2