#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Tone.h>
Tone tone1;
int Scount = 10; // Cambia esto a la cantidad de segundos para empezar
int Mcount = 5; // Cambia esto a la cantidad de minutos para empezar
int Hcount = 0; // Contar horas
int DefuseTimer = 0; // Configurar temporizador a 0
long secMillis = 0; // Guardar la última vez para agregar segundo
long interval = 1000; // Intervalo para segundos
char password[4]; // Número de caracteres en la contraseña
int currentLength = 0; // Define el número que se está escribiendo actualmente
char entered[4];
int ledPin = 13; // LED rojo
int ledPin2 = 3; // LED amarillo
int ledPin3 = 2; // LED verde
// Los pines que usamos en el LCD
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
const byte ROWS = 4; // Cuatro filas
const byte COLS = 4; // Cuatro columnas
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
// Conexiones del keypad para Arduino Nano
byte rowPins[ROWS] = { 11, 10, 9, 8 }; // Pines conectados a las filas del keypad (R1, R2, R3, R4)
byte colPins[COLS] = { 7, 6, 5, 4 }; // Pines conectados a las columnas del keypad (C1, C2, C3, C4)
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
pinMode(ledPin, OUTPUT); // Configura el pin digital como salida
pinMode(ledPin2, OUTPUT); // Configura el pin digital como salida
pinMode(ledPin3, OUTPUT); // Configura el pin digital como salida
tone1.begin(12);
lcd.begin(16, 2);
Serial.begin(9600);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Code: ");
while (currentLength < 4) {
lcd.setCursor(currentLength + 6, 1);
lcd.cursor();
char key = keypad.getKey();
if (key != NO_KEY) {
if (key != '*' && key != '#') {
lcd.print(key);
password[currentLength] = key;
currentLength++;
tone1.play(NOTE_C6, 200);
}
}
}
if (currentLength == 4) {
delay(500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("You've Entered: ");
lcd.setCursor(6, 1);
for (int i = 0; i < 4; i++) {
lcd.print(password[i]);
}
tone1.play(NOTE_E6, 200);
delay(3000);
lcd.clear();
currentLength = 0;
}
}
void loop() {
timer();
char key2 = keypad.getKey(); // Obtener la tecla
if (key2 == '*') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
while (currentLength < 4) {
timer();
char key2 = keypad.getKey();
if (key2 == '#') {
currentLength = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
} else if (key2 != NO_KEY) {
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
lcd.print(key2);
entered[currentLength] = key2;
currentLength++;
tone1.play(NOTE_C6, 200);
delay(100);
lcd.noCursor();
lcd.setCursor(currentLength + 6, 0);
lcd.print("*");
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
}
}
if (currentLength == 4) {
if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3]) {
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Bomb Defused");
currentLength = 0;
digitalWrite(ledPin3, HIGH);
delay(2500);
lcd.setCursor(0, 1);
lcd.print("Reset the Bomb");
delay(1000000);
} else {
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Wrong Password!");
if (Hcount > 0) {
Hcount = Hcount - 1;
}
if (Mcount > 0) {
Mcount = Mcount - 59;
}
if (Scount > 0) {
Scount = Scount - 59;
}
delay(1500);
currentLength = 0;
}
}
}
}
void timer() {
Serial.print(Scount);
Serial.println();
if (Hcount <= 0) { // Si el temporizador llega a 0, el LCD muestra la explosión
if (Mcount < 0) {
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("The Bomb Has ");
lcd.setCursor(0, 1);
lcd.print("Exploded!");
while (Mcount < 0) {
digitalWrite(ledPin, HIGH); // Enciende el LED
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin, LOW); // Apaga el LED
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, HIGH); // Enciende el LED
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, LOW); // Apaga el LED
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, HIGH); // Enciende el LED
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, LOW); // Apaga el LED
tone1.play(NOTE_A2, 90);
delay(100);
}
}
}
lcd.setCursor(0, 1); // Establece el cursor en la 2ª línea
lcd.print("Timer:");
if (Hcount >= 10) {
lcd.setCursor(7, 1);
lcd.print(Hcount);
}
if (Hcount < 10) {
lcd.setCursor(7, 1);
lcd.write("0");
lcd.setCursor(8, 1);
lcd.print(Hcount);
}
lcd.print(":");
if (Mcount >= 10) {
lcd.setCursor(10, 1);
lcd.print(Mcount);
}
if (Mcount < 10) {
lcd.setCursor(10, 1);
lcd.write("0");
lcd.setCursor(11, 1);
lcd.print(Mcount);
}
lcd.print(":");
if (Scount >= 10) {
lcd.setCursor(13, 1);
lcd.print(Scount);
}
if (Scount < 10) {
lcd.setCursor(13, 1);
lcd.write("0");
lcd.setCursor(14, 1);
lcd.print(Scount);
}
if (Hcount < 0) {
Hcount = 0;
}
if (Mcount < 0) {
Hcount--;
Mcount = 59;
}
if (Scount < 1) { // Si es 60, realiza esta operación
Mcount--; // Añade 1 a Mcount
Scount = 59; // Reinicia Scount
}
if (Scount > 0) { // Realiza esta operación 59 veces
unsigned long currentMillis = millis();
if (currentMillis - secMillis > interval) {
tone1.play(NOTE_G5, 200);
secMillis = currentMillis;
Scount--; // Añade 1 a Scount
digitalWrite(ledPin2, HIGH); // Enciende el LED
delay(10); // Espera un segundo
digitalWrite(ledPin2, LOW); // Apaga el LED
delay(10); // Espera un segundo
}
}
}