#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 rtc;
int boton = 6;
int rele = 9;
int valor = 0;
int presionados = 0;
unsigned long lastPressTime = 0;
unsigned long debounceDelay = 200;
const int alarmas[7][2] = {
{8, 0}, {9, 20}, {10, 50},
{13, 55}, {16, 20}, {17, 0},
{18, 50}
};
void setup() {
pinMode(boton, INPUT);
pinMode(rele, OUTPUT);
lcd.init();
lcd.backlight();
if (!rtc.begin()) {
lcd.print("Error RTC");
while (1);
}
// rtc.adjust(DateTime(2024, 5, 8, 14, 11, 0)); // Ajustar la hora si es necesario
}
void loop() {
valor = digitalRead(boton);
if (valor == LOW && (millis() - lastPressTime) > debounceDelay) {
lastPressTime = millis();
presionados++;
if (presionados == 2) {
mostrarTiempoRestante();
presionados = 0;
}
}
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print("Hora: ");
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
if ((now.hour() == 8 && now.minute() == 0 && now.second() < 10) ||
(now.hour() == 9 && now.minute() == 20 && now.second() < 10) ||
(now.hour() == 10 && now.minute() == 50 && now.second() < 10) ||
(now.hour() == 13 && now.minute() == 50 && now.second() < 10) ||
(now.hour() == 16 && now.minute() == 20 && now.second() < 10) ||
(now.hour() == 17 && now.minute() == 0 && now.second() < 10) ||
(now.hour() == 18 && now.minute() == 50 && now.second() < 10)) {
digitalWrite(rele, HIGH); // Activa el relé durante 10 segundos
lcd.setCursor(0, 1);
lcd.print("Alarma Activa");
} else {
digitalWrite(rele, LOW); // Desactiva el relé fuera de los 10 segundos
lcd.setCursor(0, 1);
lcd.print("Alarma Inactiva");
}
delay(1000); // Espera 1 segundo antes de actualizar la pantalla
}
void mostrarTiempoRestante() {
DateTime now = rtc.now();
int segundosRestantes = 0;
for (int i = 0; i < 7; i++) {
int horaAlarma = alarmas[i][0];
int minutoAlarma = alarmas[i][1];
if ((now.hour() < horaAlarma) ||
(now.hour() == horaAlarma && now.minute() < minutoAlarma)) {
segundosRestantes = (horaAlarma - now.hour()) * 3600 +
(minutoAlarma - now.minute()) * 60 -
now.second();
int horasRestantes = segundosRestantes / 3600;
int minutosRestantes = (segundosRestantes % 3600) / 60;
int segundosRestantesDisplay = segundosRestantes % 60;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Suena En:");
lcd.setCursor(0, 1);
lcd.print(horasRestantes);
lcd.print("h ");
lcd.print(minutosRestantes);
lcd.print("m ");
lcd.print(segundosRestantesDisplay);
lcd.print("s");
delay(5000); // Mostrar por 5 segundos
return;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No hay alarmas");
lcd.setCursor(0, 1);
lcd.print("pendientes hoy");
delay(5000);
}