#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <stdlib.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define Password_Length 4
int buzzer = 13;
char Data[Password_Length];
char Master[Password_Length];
byte data_count = 0;
byte master_count = 0;
char customKey;
bool Boom = false;
bool Stop = false;
bool isSet = false;
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
long previousTime;
long interval = 1000;
unsigned long startTime = 0;
unsigned long elapsedTime;
unsigned long currentTimee;
long hour = 0, minute = 0, second = 10;
long countdown_time = (hour * 3600) + (minute * 60) + second;
long countdown_sec = 0;
long countdown_hour = 0;
long countdown_minute = 0;
void setup() {
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
pinMode(12, OUTPUT);
//pinMode(1, OUTPUT); reset
lcd.init();
lcd.backlight();
digitalWrite(11, HIGH);
//lcd.print("HH:MM:SS");
}
void loop() {
while (!isSet)
{
customKey = customKeypad.getKey();
lcd.setCursor(1, 0);
lcd.print("Set Password:)");
if (customKey) {
if (customKey == 'D')
{
startTime = millis();
lcd.clear();
Serial.println(2);
isSet = true;
return;
}
else {
Master[master_count] = customKey;
lcd.setCursor(master_count + 4, 1);
lcd.print(Master[master_count]);
master_count++;
}
}
}
if (!Stop && isSet)
{
currentTimee = millis();
elapsedTime = currentTimee - startTime;
customKey = customKeypad.getKey();
long countdowntime_seconds = countdown_time - (elapsedTime / 1000);
if (!Stop)
{
if (countdowntime_seconds >= 0) {
countdown_hour = countdowntime_seconds / 3600;
countdown_minute = ((countdowntime_seconds / 60) % 60);
countdown_sec = countdowntime_seconds % 60;
lcd.setCursor(2, 0);
if (countdown_hour < 10) {
lcd.print("0");
}
lcd.print(countdown_hour);
lcd.print(":");
if (countdown_minute < 10) {
lcd.print("0");
}
lcd.print(countdown_minute);
lcd.print(":");
if (countdown_sec < 10) {
lcd.print("0");
}
lcd.print(countdown_sec);
lcd.print(":");
lcd.print((elapsedTime / 100) % 10);
lcd.print((elapsedTime / 10) % 10);
if (countdown_hour == 0 && countdown_minute == 0 && countdown_sec == 0)
{
lcd.clear();
lcd.print("BOOM");
Stop = true;
Boom = true;
}
}
}
if (!Stop)
{
if (customKey) {
Data[data_count] = customKey;
lcd.setCursor(data_count + 4, 1);
lcd.print(Data[data_count]);
data_count++;
}
if (data_count == master_count) {
lcd.clear();
if (!strcmp(Data, Master)) {
lcd.setCursor(2, 0);
lcd.print("Bomb Defused");
Stop = true;
return;
}
else {
lcd.print("Incorrect");
delay(1000);
}
lcd.clear();
clearData();
}
}
long currentTime = elapsedTime;
if (!Stop)
{
if (currentTime - previousTime >= interval) {
if (countdown_hour == 0 && countdown_minute == 0 && countdown_sec < 40 && countdown_sec >= 20)
{
interval -= 20 ; //* 1.000999
}
else if (countdown_sec < 20)
{
interval -= 5;
}
//Serial.println(interval);
previousTime = currentTime;
digitalWrite(12, HIGH);
tone(buzzer, 4200, 100);
}
digitalWrite(12, LOW);
}
while (Boom)
{
if (elapsedTime <= 17000)
{
currentTimee = millis();
elapsedTime = currentTimee - startTime;
Serial.println(elapsedTime);
digitalWrite(10, HIGH);
}
else {
digitalWrite(10, LOW);
}
}
}
}
void clearData() {
while (data_count != 0) {
Data[data_count--] = 0;
}
return;
}