#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Dirección I2C del LCD 16x2
#define I2C_ADDR 0x27
// Tamaño del LCD (columnas x filas)
#define LCD_COLS 16
#define LCD_ROWS 2
// Inicializar el objeto LCD
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);
// Definición de pines para los botones de inicio y finalización
const int btnInicioPin = 2; // Pin para el botón de inicio
const int btnFinalizarPin = 3; // Pin para el botón de finalización
const int buzzerPin = 4; // Pin para el buzzer
// Variables para el conteo de tiempo
unsigned long tiempoInicio = 0;
unsigned long tiempoTranscurrido = 0;
bool conteoActivo = false;
void setup() {
// Inicialización de pines de entrada para los botones
pinMode(btnInicioPin, INPUT_PULLUP);
pinMode(btnFinalizarPin, INPUT_PULLUP);
// Inicialización del pin del buzzer como salida
pinMode(buzzerPin, OUTPUT);
// Inicializar comunicación I2C y el LCD
Wire.begin();
lcd.init();
lcd.backlight(); // Activar retroiluminación del LCD
// Mostrar mensaje inicial en el LCD
lcd.setCursor(0, 0);
lcd.print("Inicio:");
// Mostrar hora inicial (00:00:00) en el LCD
lcd.setCursor(0, 1);
lcd.print("00:00:00 ");
}
void loop() {
// Verificar si el botón de inicio fue presionado y el conteo no está activo
if (digitalRead(btnInicioPin) == LOW && !conteoActivo) {
tiempoInicio = millis(); // Guardar el tiempo de inicio
conteoActivo = true; // Activar el conteo
}
// Actualizar el tiempo en el LCD si el conteo está activo
if (conteoActivo) {
tiempoTranscurrido = millis() - tiempoInicio; // Calcular el tiempo transcurrido
mostrarTiempoLCD(tiempoTranscurrido); // Mostrar el tiempo en el LCD
}
// Verificar si el botón de finalización fue presionado y el conteo está activo
if (digitalRead(btnFinalizarPin) == LOW && conteoActivo) {
conteoActivo = false; // Desactivar el conteo
lcd.setCursor(0, 0);
lcd.print(" "); // Limpiar el título "Inicio"
lcd.setCursor(0, 0);
lcd.print("Tiempo final");
sonarBuzzer(); // Activar el buzzer
}
delay(100); // Pequeña pausa para estabilidad
}
void mostrarTiempoLCD(unsigned long tiempo) {
// Convertir el tiempo en horas, minutos y segundos
unsigned long segundos = tiempo / 1000; // Convertir a segundos (1000 ms = 1 segundo)
unsigned long minutos = segundos / 60; // 60 segundos = 1 minuto
unsigned long horas = minutos / 60; // 60 minutos = 1 hora
// Calcular los segundos, minutos y horas restantes después de los cálculos anteriores
segundos = segundos % 60;
minutos = minutos % 60;
horas = horas % 24; // Para evitar que las horas excedan 24
// Formatear el tiempo como cadena (hh:mm:ss)
String tiempoFormateado = String(horas) + ":" + (minutos < 10 ? "0" : "") + String(minutos) + ":" + (segundos < 10 ? "0" : "") + String(segundos);
// Mostrar el tiempo en el LCD
lcd.setCursor(0, 1);
lcd.print(tiempoFormateado);
}
void sonarBuzzer() {
// Hacer sonar el buzzer por 3 segundos intermitentemente
for (int i = 0; i < 3; i++) {
digitalWrite(buzzerPin, HIGH);
delay(500); // Encender el buzzer durante 0.5 segundos
digitalWrite(buzzerPin, LOW);
delay(500); // Apagar el buzzer durante 0.5 segundos
}
}