#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Definir pines para los botones y LEDs
const int boton1Pin = 22; // Acumular crédito
const int boton2Pin = 24; // Encender LED1
const int boton3Pin = 26; // Encender LED2
const int boton4Pin = 28; // Encender LED3
const int boton5Pin = 30; // Encender LED4
const int boton6Pin = 32; // Encender LED5
const int boton7Pin = 34; // Encender LED6
const int led1Pin = 23; // LED1
const int led2Pin = 25; // LED2
const int led3Pin = 27; // LED3
const int led4Pin = 29; // LED4
const int led5Pin = 31; // LED5
const int led6Pin = 33; // LED6
// Variables para el tiempo
unsigned long tiempoCredito = 0; // Crédito total acumulado
unsigned long tiempoInicio[6] = {0, 0, 0, 0, 0, 0}; // Tiempo cuando se encendió cada LED
unsigned long tiempoEncendido[6] = {0, 0, 0, 0, 0, 0}; // Tiempo que debe estar encendido cada LED
// Estado de los LEDs
enum EstadoLED { NINGUNO, ENCENDIDO };
EstadoLED estadoLED[6] = {NINGUNO, NINGUNO, NINGUNO, NINGUNO, NINGUNO, NINGUNO}; // Estados de los LEDs
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
// Inicializar pines
pinMode(boton1Pin, INPUT_PULLUP); // Botón para acumular crédito
pinMode(boton2Pin, INPUT_PULLUP); // Botón para encender LED1
pinMode(boton3Pin, INPUT_PULLUP); // Botón para encender LED2
pinMode(boton4Pin, INPUT_PULLUP); // Botón para encender LED3
pinMode(boton5Pin, INPUT_PULLUP); // Botón para encender LED4
pinMode(boton6Pin, INPUT_PULLUP); // Botón para encender LED5
pinMode(boton7Pin, INPUT_PULLUP); // Botón para encender LED6
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(led5Pin, OUTPUT);
pinMode(led6Pin, OUTPUT);
// Inicializar LEDs apagados
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
digitalWrite(led5Pin, LOW);
digitalWrite(led6Pin, LOW);
}
void loop() {
// Leer el estado de los botones
bool boton1Estado = digitalRead(boton1Pin) == LOW; // Estado del botón 1
bool boton2Estado = digitalRead(boton2Pin) == LOW; // Estado del botón 2
bool boton3Estado = digitalRead(boton3Pin) == LOW; // Estado del botón 3
bool boton4Estado = digitalRead(boton4Pin) == LOW; // Estado del botón 4
bool boton5Estado = digitalRead(boton5Pin) == LOW; // Estado del botón 5
bool boton6Estado = digitalRead(boton6Pin) == LOW; // Estado del botón 6
bool boton7Estado = digitalRead(boton7Pin) == LOW; // Estado del botón 7
// Acumulación de crédito
if (boton1Estado) {
// Añadir 10 minutos (600000 milisegundos) al crédito acumulado
tiempoCredito += 600000;
actualizarPantallaCredito(); // Actualizar la pantalla con el nuevo crédito
delay(200); // Debounce para evitar múltiples acumulaciones
}
// Encender LEDs
for (int i = 0; i < 6; i++) {
if (digitalRead(boton2Pin + i * 2) == LOW && tiempoCredito > 0 && estadoLED[i] == NINGUNO) {
tiempoInicio[i] = millis();
tiempoEncendido[i] = tiempoCredito;
digitalWrite(led1Pin + i * 2, HIGH);
estadoLED[i] = ENCENDIDO;
tiempoCredito = 0; // Restablecer el crédito acumulado
delay(200); // Debounce para el botón correspondiente
}
}
// Controlar el tiempo de encendido de cada LED
for (int i = 0; i < 6; i++) {
if (estadoLED[i] == ENCENDIDO && millis() - tiempoInicio[i] >= tiempoEncendido[i]) {
// Apagar el LED si el tiempo de crédito ha expirado
digitalWrite(led1Pin + i * 2, LOW);
estadoLED[i] = NINGUNO; // Restablecer el estado del LED
}
}
// Mostrar el crédito restante en la pantalla LCD
lcd.clear(); // Limpia la pantalla LCD antes de mostrar los nuevos datos
// Mostrar tiempos restantes en la primera fila
lcd.setCursor(0, 0);
for (int i = 0; i < 3; i++) {
if (estadoLED[i] == ENCENDIDO) {
unsigned long tiempoRestante = (tiempoEncendido[i] - (millis() - tiempoInicio[i])) / 1000; // Tiempo en segundos
unsigned long minutos = tiempoRestante / 60;
unsigned long segundos = tiempoRestante % 60;
lcd.print(i + 1);
lcd.print(":");
if (minutos < 10) lcd.print("0"); // Mostrar 0 si los minutos son menores de 10
lcd.print(minutos);
lcd.print(" ");
} else {
lcd.print(i + 1);
lcd.print(":-- ");
}
}
// Mostrar tiempos restantes en la segunda fila
lcd.setCursor(0, 1);
for (int i = 3; i < 6; i++) {
if (estadoLED[i] == ENCENDIDO) {
unsigned long tiempoRestante = (tiempoEncendido[i] - (millis() - tiempoInicio[i])) / 1000; // Tiempo en segundos
unsigned long minutos = tiempoRestante / 60;
unsigned long segundos = tiempoRestante % 60;
lcd.print(i + 1);
lcd.print(":");
if (minutos < 10) lcd.print("0"); // Mostrar 0 si los minutos son menores de 10
lcd.print(minutos);
lcd.print(" ");
} else {
lcd.print(i + 1);
lcd.print(":-- ");
}
}
delay(500); // Espera de 0.5 segundos para actualizar la pantalla
}
// Función para mostrar el crédito en el LCD
void mostrarCredito(unsigned long credito) {
unsigned long minutos = credito / 60000;
unsigned long segundos = (credito % 60000) / 1000;
lcd.setCursor(0, 1);
if (minutos < 10) lcd.print("0"); // Mostrar 0 si los minutos son menores de 10
lcd.print(minutos);
lcd.print(":");
if (segundos < 10) lcd.print("0"); // Mostrar 0 si los segundos son menores de 10
lcd.print(segundos);
}
// Función para actualizar la pantalla con el crédito actual
void actualizarPantallaCredito() {
lcd.clear(); // Limpia la pantalla LCD
lcd.setCursor(0, 0);
lcd.print("Credito: ");
mostrarCredito(tiempoCredito);
}