#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHTPIN 2 // Pin donde se conecta el sensor DHT22
#define DHTTYPE DHT22 // Tipo de sensor DHT
#define SWITCH_PIN 3 // Pin donde se conecta el interruptor
#define NUM_DATOS 10 // Número de datos a capturar
DHT dht(DHTPIN, DHTTYPE);
// Inicializar el LCD: dirección I2C 0x27, 16 columnas y 2 filas
LiquidCrystal_I2C lcd(0x27, 16, 2);
float temperaturas[NUM_DATOS];
float humedades[NUM_DATOS];
unsigned long tiempos[NUM_DATOS];
bool celsius = true; // Variable para manejar la unidad de temperatura
unsigned long lastSwitchChangeTime = 0; // Para manejar el antirrebote
const unsigned long debounceDelay = 200; // Antirrebote en milisegundos
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(SWITCH_PIN, INPUT_PULLUP); // Configura el interruptor con pull-up interno
// Inicializar el LCD
lcd.begin();
lcd.backlight(); // Encender la luz de fondo del LCD
}
void loop() {
// Lectura del interruptor y actualización de la unidad de temperatura
bool currentSwitchState = digitalRead(SWITCH_PIN);
unsigned long currentTime = millis();
if (currentSwitchState == HIGH && (currentTime - lastSwitchChangeTime) > debounceDelay) {
celsius = false; // Cambia a Fahrenheit
lastSwitchChangeTime = currentTime; // Actualiza el tiempo del último cambio
} else if (currentSwitchState == LOW && (currentTime - lastSwitchChangeTime) > debounceDelay) {
celsius = true; // Cambia a Celsius
lastSwitchChangeTime = currentTime; // Actualiza el tiempo del último cambio
}
// Captura de datos
for (int i = 0; i < NUM_DATOS; i++) {
float tempCelsius = dht.readTemperature(); // Lectura de temperatura en Celsius
float hum = dht.readHumidity(); // Lectura de humedad
// Verificación de fallas en la lectura
if (isnan(tempCelsius) || isnan(hum)) {
Serial.println("Error al leer del sensor DHT22");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Error en sensor");
delay(1000); // Espera antes de intentar leer nuevamente
return;
}
// Convertir la temperatura a Fahrenheit si es necesario
float tempDisplay = celsius ? tempCelsius : tempCelsius * 1.8 + 32;
// Almacenar datos y tiempo
temperaturas[i] = tempCelsius;
humedades[i] = hum;
tiempos[i] = millis();
// Mostrar los datos capturados
Serial.print("Dato ");
Serial.print(i + 1);
Serial.print(": Temp: ");
Serial.print(tempDisplay);
Serial.print(celsius ? " °C" : " °F");
Serial.print(" | Humedad: ");
Serial.print(hum);
Serial.println(" %");
delay(3000); // Espera de 3 segundos entre lecturas
}
// Encontrar los valores máximo y mínimo
float temp_max = temperaturas[0];
float temp_min = temperaturas[0];
float hum_max = humedades[0];
float hum_min = humedades[0];
unsigned long tiempo_temp_max = tiempos[0];
unsigned long tiempo_temp_min = tiempos[0];
unsigned long tiempo_hum_max = tiempos[0];
unsigned long tiempo_hum_min = tiempos[0];
for (int i = 1; i < NUM_DATOS; i++) {
if (temperaturas[i] > temp_max) {
temp_max = temperaturas[i];
tiempo_temp_max = tiempos[i];
}
if (temperaturas[i] < temp_min) {
temp_min = temperaturas[i];
tiempo_temp_min = tiempos[i];
}
if (humedades[i] > hum_max) {
hum_max = humedades[i];
tiempo_hum_max = tiempos[i];
}
if (humedades[i] < hum_min) {
hum_min = humedades[i];
tiempo_hum_min = tiempos[i];
}
}
// Mostrar los valores máximo y mínimo en la pantalla LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp Max: ");
lcd.print(celsius ? temp_max : temp_max * 1.8 + 32);
lcd.print(celsius ? " C" : " F");
lcd.setCursor(0, 1);
lcd.print("Hum Max: ");
lcd.print(hum_max);
lcd.print(" %");
delay(5000); // Pausa antes de mostrar la siguiente información
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp Min: ");
lcd.print(celsius ? temp_min : temp_min * 1.8 + 32);
lcd.print(celsius ? " C" : " F");
lcd.setCursor(0, 1);
lcd.print("Hum Min: ");
lcd.print(hum_min);
lcd.print(" %");
delay(5000); // Pausa antes de iniciar otra ronda de capturas
}