#include <Keypad.h>
#include <LiquidCrystal.h>
const int led1 = 10; // LED 1
const int led2 = 11; // LED 2
const int led3 = 12; // LED 3
const int buzzer = 13; // Buzzer pin
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // RS, E, D4, D5, D6, D7 for LCD
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {8, 9, A0, A1}; // Connect rows to Arduino pins
byte colPins[COLS] = {A2, A3, A4, A5}; // Connect columns to Arduino pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int countdownTime = 0; // Store countdown time
bool countdownStarted = false;
bool defuseMode = false;
String defuseCode = ""; // To store the entered defuse code
const String secretCode = "1234"; // The correct defuse code
void setup() {
Serial.begin(9600); // Start Serial for debugging
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(buzzer, OUTPUT);
lcd.begin(16, 2); // Start the LCD
lcd.print("Set Timer: ");
}
void loop() {
char key = keypad.getKey(); // Get keypad input once per loop
if (!countdownStarted && !defuseMode) {
if (key) {
if (key >= '0' && key <= '9') { // If a number key is pressed
countdownTime = countdownTime * 10 + (key - '0'); // Add number to countdownTime
lcd.setCursor(11, 0);
lcd.print(countdownTime); // Show the current countdown time
}
if (key == '#') { // '#' starts the countdown
lcd.clear();
lcd.print("Bomb Activated!");
delay(1000); // Small delay before starting the countdown
countdownStarted = true;
}
if (key == '*') { // '*' resets the time
countdownTime = 0;
lcd.setCursor(11, 0);
lcd.print(" "); // Clear previous input
}
}
}
if (countdownStarted && !defuseMode) {
for (int i = countdownTime; i >= 0; i--) {
lcd.clear();
lcd.print("Time left: ");
lcd.print(i); // Display the countdown on the LCD
// LED Logic
if (i > 6) {
digitalWrite(led1, HIGH); // Turn on LED 1
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
} else if (i > 3) {
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH); // Turn on LED 2
digitalWrite(led3, LOW);
} else {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH); // Turn on LED 3
}
// Check if the user presses 'A' to enter the defuse code
if (key == 'D') {
defuseMode = true;
lcd.clear();
lcd.print("Enter code:");
lcd.setCursor(0, 1); // Move cursor to the second line
lcd.print("Code: ");
defuseCode = ""; // Clear previous input
break; // Break to exit the countdown loop
}
delay(1000); // 1-second delay for countdown
// If countdown reaches zero, trigger explosion
if (i == 0 && !defuseMode) {
explode(); // Function to handle the explosion effect
}
// Update key reading for the next iteration
key = keypad.getKey();
}
}
if (defuseMode) {
if (key) {
if (key >= '0' && key <= '9') { // If a number key is pressed
defuseCode += key; // Append key to the defuse code
lcd.setCursor(6, 1); // Move cursor to display entered code
lcd.print(defuseCode); // Display the entered code
}
if (key == '#') { // '#' checks the defuse code
if (defuseCode == secretCode) {
lcd.clear();
lcd.print("Defused!");
countdownStarted = false;
defuseMode = false;
resetSystem();
} else {
lcd.clear();
lcd.print("Wrong Code!");
countdownTime -= 5; // Subtract 5 seconds from countdown
if (countdownTime < 0) {
countdownTime = 0; // Prevent negative countdown
}
delay(2000); // Show error for 2 seconds
lcd.clear();
lcd.print("Enter code:");
lcd.setCursor(0, 1); // Move cursor to the second line
lcd.print("Code: ");
defuseCode = ""; // Allow re-entry of code
defuseMode = false; // Exit defuse mode
}
}
if (key == '*') { // '*' resets the defuse code input
defuseCode = "";
lcd.setCursor(6, 1); // Move cursor to display cleared code
lcd.print(" "); // Clear previous input
}
}
}
}
// Function to handle explosion
void explode() {
lcd.clear();
lcd.print("BOOM!");
// Create a loud and long beep effect
for (int j = 0; j < 3; j++) { // Repeat for a scary effect
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
tone(buzzer, 1500); // Use a higher frequency for a louder sound
delay(1500); // Long duration for the beep (2 seconds)
noTone(buzzer);
// Add some silence to separate sounds
delay(500); // Wait before the next beep
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(200); // Wait before the next cycle
}
countdownStarted = false; // Reset the countdown
defuseMode = false;
countdownTime = 0;
lcd.clear();
lcd.print("Set Timer: "); // Ready for next input
}
// Function to reset the system after defuse
void resetSystem() {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
noTone(buzzer);
countdownTime = 0;
defuseMode = false;
lcd.clear();
lcd.print("Set Timer: "); // Ready for next input
}