#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include<EEPROM.h>
int hrs = 0;
int Min = 0;
int sec = 0;
unsigned int check_val = 50;
int add_chk = 0;
int add_hrs = 1;
int add_min = 2;
bool RUN = true;
bool complete = false;
bool min_flag = true;
bool hrs_flag = true;
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentM;
const unsigned long period = 1000; //the value is a number of milliseconds
uint8_t state;
uint8_t valIndex;
uint8_t cursorPos;
char entered_value [6];
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
const String password = "123456"; // change your password here
String input_password;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 13, 12, 14, 27 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 4, 0, 2, 15 }; // Pins connected to R1, R2, R3, R4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
// put your setup code here, to run once:
startMillis = millis(); //initial start time
if (EEPROM.read(add_chk) != check_val) {
EEPROM.write(add_chk, check_val);
EEPROM.write(add_hrs, 0);
EEPROM.write(add_min, 10);
} else {
hrs = EEPROM.read(add_hrs);
Min = EEPROM.read(add_min);
}
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Wire.begin(18, 17);
lcd.init();
lcd.backlight();
}
void loop() {
if(RUN) lcd.print("Defuse The Bomb!");
while (RUN) {
currentM = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentM - startMillis >= period) //test whether the period has elapsed
{
sec = sec - 1;
startMillis = currentM; //IMPORTANT to save the start time of the current LED state.
if (sec == -1) {
sec = 59;
Min = Min - 1;
}
if (Min == -1){
Min = 59;
hrs = hrs - 1;
}
if (hrs == -1) hrs = 0;
lcd.setCursor(4, 1);
if (hrs <= 9) {
lcd.print('0');
}
lcd.print(hrs);
lcd.print(':');
if (Min <= 9){
lcd.print('0');
}
lcd.print(Min);
lcd.print(':');
if (sec <= 9) {
lcd.print('0');
}
lcd.print(sec);
lcd.setCursor(0, 2);
lcd.print("Enter Code:");
if(complete == true) {
lcd.clear();
lcd.print("Bomb Defused!!!");
RUN = !RUN;
lcd.setCursor(0,1);
lcd.print("Restart via UI");
delay(1000);
break;
}
if (hrs == 0 && Min == 0 && sec == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" TIMES UP ");
RUN = false;
INIT();
}
}
getInput();
}
}
void nextChar(char key)
{
if (valIndex < 6)
{
entered_value[valIndex] = key;
lcd.setCursor(0, 3);
lcd.print(entered_value);
Serial.println(entered_value);
cursorPos++;
valIndex++;
input_password += key;
}
}
void eraseChar()
{
if (valIndex > 0 )
{
valIndex--;
cursorPos--;
entered_value[valIndex] = '\0';
}
lcd.setCursor(cursorPos, 3);
lcd.print(' ');
lcd.setCursor(cursorPos, 3);
}
void eraseAll()
{
while(valIndex > 0 )
{
valIndex--;
cursorPos--;
entered_value[valIndex] = '\0';
lcd.setCursor(cursorPos, 3);
lcd.print(' ');
lcd.setCursor(cursorPos, 3);
}
input_password = "";
}
void getInput() {
char key = keypad.getKey();
switch(key)
{
case 'A':
RUN = true;
INIT();
break;
case '#':
checkCode();
break;
case '*':
eraseChar();
break;
default:
if (isDigit(key))
{
nextChar(key);
}
break;
}
}
void checkCode() {
if(input_password == password) {
Serial.println("Riktig KODE!!!");
complete = true;
}
else {
Serial.println("Feil kode!!!");
lcd.setCursor(0, 2);
lcd.print("Wrong Code!");
eraseAll();
delay(1000);
}
}
void INIT() {
hrs = EEPROM.read(add_hrs);
Min = EEPROM.read(add_min);
sec = 0;
lcd.setCursor(4, 0);
if (hrs <= 9) {
lcd.print('0');
}
lcd.print(hrs);
lcd.print(':');
if (Min <= 9) {
lcd.print('0');
}
lcd.print(Min);
lcd.print(':');
if (sec <= 9) {
lcd.print('0');
}
lcd.print(sec);
min_flag = true;
hrs_flag = true;
delay(50);
}