#include <Keypad.h>
#include <TM1637Display.h>
const byte ROWS = 4; // Cuatro filas
const byte COLS = 3; // Tres columnas
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {27, 26, 25, 33};
byte colPins[COLS] = {13, 12, 14};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Definir los tiempos asociados a cada combinación de teclas (en segundos)
int tiempos[100]; // Se usará el índice 0 para la tecla '0'
void inicializarTiempos() {
// Asignar los tiempos manualmente para cada combinación de teclas del 0 al 99
tiempos[0] = 58260;
tiempos[1] = 57677;
tiempos[2] = 57094;
tiempos[3] = 56512;
tiempos[4] = 55930;
tiempos[5] = 55347;
tiempos[6] = 54764;
tiempos[7] = 54181;
tiempos[8] = 53599;
tiempos[9] = 53016;
tiempos[10] = 52434;
tiempos[11] = 51851;
tiempos[12] = 51268;
tiempos[13] = 50686;
tiempos[14] = 50103;
tiempos[15] = 49521;
tiempos[16] = 48938;
tiempos[17] = 48355;
tiempos[18] = 47773;
tiempos[19] = 47190;
tiempos[20] = 46608;
tiempos[21] = 46025;
tiempos[22] = 45442;
tiempos[23] = 44860;
tiempos[24] = 44277;
tiempos[25] = 43695;
tiempos[26] = 43112;
tiempos[27] = 42529;
tiempos[28] = 41947;
tiempos[29] = 41364;
tiempos[30] = 40782;
tiempos[31] = 40199;
tiempos[32] = 39616;
tiempos[33] = 39034;
tiempos[34] = 38451;
tiempos[35] = 37869;
tiempos[36] = 37286;
tiempos[37] = 36703;
tiempos[38] = 36121;
tiempos[39] = 35538;
tiempos[40] = 34956;
tiempos[41] = 34373;
tiempos[42] = 33790;
tiempos[43] = 33208;
tiempos[44] = 32625;
tiempos[45] = 32043;
tiempos[46] = 31460;
tiempos[47] = 30877;
tiempos[48] = 30295;
tiempos[49] = 29712;
tiempos[50] = 29130;
tiempos[51] = 28547;
tiempos[52] = 27964;
tiempos[53] = 27382;
tiempos[54] = 26799;
tiempos[55] = 26217;
tiempos[56] = 25634;
tiempos[57] = 25051;
tiempos[58] = 24469;
tiempos[59] = 23886;
tiempos[60] = 23304;
tiempos[61] = 22721;
tiempos[62] = 22138;
tiempos[63] = 21556;
tiempos[64] = 20973;
tiempos[65] = 20391;
tiempos[66] = 19808;
tiempos[67] = 19225;
tiempos[68] = 18643;
tiempos[69] = 18060;
tiempos[70] = 17478;
tiempos[71] = 16895;
tiempos[72] = 16312;
tiempos[73] = 15730;
tiempos[74] = 15147;
tiempos[75] = 14565;
tiempos[76] = 13982;
tiempos[77] = 13399;
tiempos[78] = 12817;
tiempos[79] = 12234;
tiempos[80] = 11652;
tiempos[81] = 11069;
tiempos[82] = 10486;
tiempos[83] = 9904;
tiempos[84] = 9312;
tiempos[85] = 8730;
tiempos[86] = 8147;
tiempos[87] = 7564;
tiempos[88] = 6982;
tiempos[89] = 6399;
tiempos[90] = 5817;
tiempos[91] = 5234;
tiempos[92] = 4651;
tiempos[93] = 4069;
tiempos[94] = 3486;
tiempos[95] = 2904;
tiempos[96] = 2321;
tiempos[97] = 1738;
tiempos[98] = 1156;
tiempos[99] = 0;
}
int tiempoSeleccionado = 0; // Tiempo inicial seleccionado
int currentInput = 0; // Valor actual ingresado
const int pinLED = 2; // Pin para el LED
bool ledState = false; // Estado del LED
bool timerActive = false; // Estado del temporizador
// Configuración para el display TM1637
#define CLK 19 // Pin de reloj para el display
#define DIO 18 // Pin de datos para el display
TM1637Display display(CLK, DIO);
void setup() {
Serial.begin(9600);
inicializarTiempos(); // Inicializar los tiempos
pinMode(pinLED, OUTPUT); // Configurar el pin del LED como salida
digitalWrite(pinLED, LOW); // Apagar el LED al inicio
display.setBrightness(0x09); // Establece brillo al 60% para el display
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key >= '0' && key <= '9') {
int valorTecla = key - '0';
currentInput = currentInput * 10 + valorTecla;
if (currentInput > 99) { // Si el valor excede 99, sobreescribe
currentInput = valorTecla; // Solo el dígito recién ingresado
}
display.showNumberDec(currentInput); // Mostrar la combinación actual en el display
} else if (key == '*') {
// Verificar si la combinación de teclas ingresada es válida
if (currentInput >= 0 && currentInput < 100) {
tiempoSeleccionado = tiempos[currentInput];
Serial.print("Tiempo seleccionado: ");
Serial.print(tiempoSeleccionado);
Serial.println(" segundos");
// Iniciar temporizador
if (tiempoSeleccionado > 0) {
Serial.print("Temporizador iniciado para ");
Serial.print(tiempoSeleccionado);
Serial.println(" segundos");
digitalWrite(pinLED, HIGH); // Encender el LED
timerActive = true;
unsigned long startTime = millis();
while (millis() - startTime < tiempoSeleccionado * 1000) {
char checkKey = keypad.getKey();
if (checkKey == '#') {
// Apagar el LED y salir del bucle del temporizador
digitalWrite(pinLED, LOW);
Serial.println("LED apagado por el usuario");
timerActive = false;
break;
}
}
if (timerActive) {
digitalWrite(pinLED, LOW); // Apagar el LED al finalizar el temporizador
Serial.println("¡Tiempo terminado!");
}
display.clear(); // Limpiar el display al finalizar el temporizador
tiempoSeleccionado = 0; // Reiniciar tiempo seleccionado
timerActive = false;
} else {
Serial.println("No se ha seleccionado ningún tiempo.");
}
} else {
Serial.println("Combinación de teclas no válida.");
}
currentInput = 0; // Reiniciar la combinación actual
display.showNumberDec(currentInput); // Mostrar 0 en el display
} else if (key == '#') {
// Alternar el estado del LED si no está activo el temporizador
if (!timerActive) {
ledState = !ledState;
digitalWrite(pinLED, ledState ? HIGH : LOW);
Serial.print("LED ");
Serial.println(ledState ? "encendido" : "apagado");
}
}
}
}