#include <LiquidCrystal_I2C.h>
#define RELAY_1_PIN 2
#define RELAY_2_PIN 3
#define BUTTON_PIN 4
#define SESSION_1_LENGTH 480000 // 8 minutes in milliseconds
#define SESSION_2_LENGTH 300000 // 5 minutes in milliseconds
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the LCD
void setup() {
pinMode(RELAY_1_PIN, OUTPUT); // Set relay pins as output
pinMode(RELAY_2_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the LCD backlight
lcd.setCursor(3, 0);
lcd.print("Pulse boton");
lcd.setCursor(2, 1);
lcd.print("Para iniciar");
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) { // If the button is pressed
lcd.clear(); // Clear the LCD
lcd.print("SESION 8 MINUTOS");
unsigned long sessionStartTime = millis(); // Get the current time
digitalWrite(RELAY_1_PIN, HIGH); // Turn on relay 1
while (millis() - sessionStartTime < SESSION_1_LENGTH) { // While the session is running
lcd.setCursor(0, 1);
lcd.setCursor(7, 1);
int remainingSeconds = (SESSION_1_LENGTH - (millis() - sessionStartTime)) / 1000; // Calcula los segundos restantes
int minutes = remainingSeconds / 60; // Calcula los minutos restantes
int seconds = remainingSeconds % 60; // Calcula los segundos restantes
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0"); // Añade un cero para los segundos menores de 10
}
lcd.print(seconds);
lcd.print(" ");
}
digitalWrite(RELAY_1_PIN, LOW); // Turn off relay 1
delay(2000); // Wait for 2 seconds
lcd.clear(); // Clear the LCD
lcd.print("Ventilacion");
sessionStartTime = millis(); // Get the current time
digitalWrite(RELAY_2_PIN, HIGH); // Turn on relay 2
while (millis() - sessionStartTime < SESSION_2_LENGTH) { // While the session is running
lcd.setCursor(0, 1);
int timeRemaining = (SESSION_2_LENGTH - (millis() - sessionStartTime)) / 1000;
int minutes = timeRemaining / 60;
int seconds = timeRemaining % 60;
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
lcd.print(" ");
delay(500); // Wait for half a second
}
digitalWrite(RELAY_2_PIN, LOW); // Turn off relay 2
lcd.clear(); // Clear the LCD
lcd.print("Pulse boton para");
lcd.setCursor(0, 1);
lcd.print("iniciar session");
}
}