#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Initialize LCD with I2C address 0x27 and 16 columns x 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Keypad configuration
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] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const int missileSwitchPin = 12;
const char* armingCode = "1234";
const char* defuseCode = "4321";
char enteredCode[5];
byte codeIndex = 0;
int countdownTime = 600; // 10 minutes (each unit is a second)
bool bombArmed = false;
void setup() {
pinMode(missileSwitchPin, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Bomb Not Armed");
}
void loop() {
// Check if missile switch is toggled on and bomb is not yet armed
if (!bombArmed && digitalRead(missileSwitchPin) == HIGH) {
armBombProcedure();
}
if (bombArmed) {
startCountdown();
}
}
void armBombProcedure() {
lcd.clear();
lcd.print("Enter Arm Code:");
getCode();
if (strcmp(enteredCode, armingCode) == 0) {
bombArmed = true;
} else {
lcd.clear();
lcd.print("Wrong Code");
delay(2000);
lcd.clear();
lcd.print("Bomb Not Armed");
codeIndex = 0;
}
}
void startCountdown() {
while (countdownTime > 0 && bombArmed) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time Left:");
lcd.setCursor(0, 1);
lcd.print(formatTime(countdownTime));
countdownTime--;
delay(1000);
if (digitalRead(missileSwitchPin) == LOW) { // if missile switch is toggled off
lcd.clear();
lcd.print("Enter Defuse Code:");
getCode();
if (strcmp(enteredCode, defuseCode) == 0) {
bombArmed = false;
lcd.clear();
lcd.print("Bomb Defused!");
delay(2000);
resetBomb();
} else {
lcd.clear();
lcd.print("Wrong Defuse Code");
delay(2000);
}
}
}
if (countdownTime <= 0) {
lcd.clear();
lcd.print("BOOM!");
delay(3000);
resetBomb();
}
}
String formatTime(int time) {
int minutes = time / 60;
int seconds = time % 60;
String formattedTime = (minutes < 10 ? "0" : "") + String(minutes) + ":" + (seconds < 10 ? "0" : "") + String(seconds);
return formattedTime;
}
void getCode() {
codeIndex = 0;
while (codeIndex < 4) {
char key = keypad.getKey();
if (key) {
enteredCode[codeIndex] = key;
lcd.setCursor(codeIndex, 1);
lcd.print("*");
codeIndex++;
}
}
enteredCode[codeIndex] = '\0'; // Null terminate the entered code
}
void resetBomb() {
countdownTime = 600;
codeIndex = 0;
bombArmed = false;
lcd.clear();
lcd.print("Bomb Not Armed");
}