#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const int buzzerPin = 10;
const int redLed = 11;
const int yelLed = 12;
const int grnLed = 13;
const int armSwitch = A0;
const int armButton = A2;
const int timerButton = A4;
const int timerDuration = 300000;
const byte ROW_NUM = 4;
const byte COLUMN_NUM = 4;
char keys[ROW_NUM] [COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
LiquidCrystal_I2C lcd(0x27, 20, 4);
int defuseCode[] = {1, 2, 3, 4}; // Set defuse code here
int defuseCodeLength = 4;
int enteredCode[4];
int codeIndex = 0;
bool armed = false;
unsigned long countdownStartTime = 0;
unsigned long countdownDuration = timerDuration;
bool timerSetMode = false;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(buzzerPin, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(yelLed, OUTPUT);
pinMode(grnLed, OUTPUT);
pinMode(armSwitch, INPUT_PULLUP);
pinMode(armButton, INPUT_PULLUP);
pinMode(timerButton, INPUT_PULLUP);
}
void loop() {
if (!armed) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System Safe");
delay(500);
if (digitalRead(timerButton) == LOW) {
setTimer();
}
}
else {
checkArming(); // Check if key is turned and arm button is pressed
updateCountdown();
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '#') {
checkCode(); // Check entered code when # is pressed
} else if (key == '*') {
enterTimerSetMode(); // Enter timer set mode when * is pressed
} else {
//Append the entered digit to the code.
enteredCode[codeIndex] = key - '0';
codeIndex = (codeIndex + 1) % defuseCodeLength;
}
}
}
}
void checkArming() {
static bool lastArmButtonState = HIGH; // Store the last state of the arm button
bool currentArmButtonState = digitalRead(armButton);
if (digitalRead(armSwitch) == LOW && currentArmButtonState == LOW && lastArmButtonState == HIGH) {
// Key turned and arm button pressed
armed = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System Armed!!");
countdownStartTime = millis();
delay(2000);
lcd.clear();
}
lastArmButtonState = currentArmButtonState;
}
void checkCode() {
for (int i = 0; i < defuseCodeLength; i++) {
if (enteredCode[i] != defuseCode[i]) {
//Incorrect code, sound the alarm
activateAlarm();
return;
}
}
//Correct code, deactivate alarm
deactivateAlarm();
}
void activateAlarm() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ALARM ACTIVATED!!");
digitalWrite(redLed, HIGH);
digitalWrite(yelLed, LOW);
digitalWrite(grnLed, LOW);
tone(buzzerPin, 1000); // Sound alarm
delay(5000); // Alarm duration
resetSystem();
}
void deactivateAlarm() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ALARM DEACTIVATED");
digitalWrite(grnLed, HIGH);
digitalWrite(yelLed, LOW);
digitalWrite(redLed, LOW);
noTone(buzzerPin); // Stop alarm
delay(2000); // Display deactivated message for 2 seconds
resetSystem();
}
void resetSystem() {
lcd.clear();
codeIndex = 0; // Reset code entry
armed = false; // Disarm the system
timerSetMode = false; // Exit timer set mode
countdownDuration = timerDuration; // Reset countdown duration
}
void enterTimerSetMode() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Timer (s): ");
lcd.setCursor(0, 1);
lcd.print("Press # to confirm");
int timerValue = 0;
while (timerSetMode) {
char key = keypad.getKey();
if (key) {
if (key == '#') {
//Confirm timer value
timerSetMode = false;
countdownDuration = timerValue * 1000; // Convert seconds to milliseconds
} else if (key == '*') {
// Exit timer set mode
timerSetMode = false;
} else {
//Append entered digit to the timer value
timerValue = timerValue * 10 + (key - '0');
lcd.setCursor(0, 1);
lcd.print(" "); // Clear previous timer value
lcd.setCursor(0, 1);
lcd.print(timerValue);
}
}
}
}
void updateCountdown() {
if (armed && !timerSetMode) {
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - countdownStartTime;
if (elapsedTime < countdownDuration) {
unsigned long remainingTime = (countdownDuration - elapsedTime) / 1000; // Convert milliseconds to seconds
lcd.setCursor(0, 0);
lcd.print("Countdown: ");
lcd.setCursor(0, 1);
lcd.print(remainingTime);
}
}
}
void setTimer() {
timerSetMode = true;
enterTimerSetMode();
}