#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12, 14, 27};
byte colPins[COLS] = {26, 25, 33, 32};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const int bzPins[] = {21, 19, 18, 5};
const int ledPins[] = {2, 4, 15, 17};
const int nTonos[] = {3, 4, 5, 6};
const String colores[] = {"ROJO", "VERDE", "AZUL", "AMARILLO"};
void setup() {
// Cambiamos a 9600 que es más estable para ver mensajes iniciales
Serial.begin(9600);
// Forzamos el encendido de la consola con un bucle inicial
for(int i = 0; i < 3; i++) {
Serial.println(">>> CONSOLA CONECTADA - ESPERANDO TECLA <<<");
delay(500);
}
for (int i = 0; i < 4; i++) {
pinMode(bzPins[i], OUTPUT);
pinMode(ledPins[i], OUTPUT);
}
}
void ejecutarAlerta(int idx, char tecla) {
Serial.println("\n------------------------------------");
Serial.print("TECLA PRESIONADA: "); Serial.println(tecla);
Serial.print("ALERTA: "); Serial.println(idx + 1);
Serial.print("LED ENCENDIDO: "); Serial.println(colores[idx]);
digitalWrite(ledPins[idx], HIGH);
for (int t = 0; t < nTonos[idx]; t++) {
tone(bzPins[idx], 500 + (t * 100), 200);
delay(300);
noTone(bzPins[idx]);
}
digitalWrite(ledPins[idx], LOW);
Serial.println("SISTEMA LISTO NUEVAMENTE.");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '1') { ejecutarAlerta(0, key); }
else if (key == '2') { ejecutarAlerta(1, key); }
else if (key == '3') { ejecutarAlerta(2, key); }
else if (key == '4') { ejecutarAlerta(3, key); }
else {
// ESTE ES EL MENSAJE QUE VERÁS SI PRESIONAS CUALQUIER OTRA COSA
Serial.print("AVISO: Tecla '");
Serial.print(key);
Serial.println("' NO PERMITIDA. Sin accion en LEDs o Buzzers.");
}
}
}