#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const int buzzerPin = 1; // Connect to pin 12
const int redLed = 13;
const int grnLed = 11;
const int armSwitchPin = A0; // Connect the arm key switch to pin 10
const int armButtonPin = A2; // Connect the arm button to pin 11
const int timerButtonPin = A3; // Connect the timer button to pin A0
const int timerDuration = 300000; // Initial timer duration in milliseconds (5 minutes)
const byte ROW_NUM = 4; // four rows
const byte COLUMN_NUM = 4; // four columns
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}; // connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27, 20 columns and 4 rows
int defuseCode[] = {1, 2, 3, 4}; // Set your 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.begin(20, 4);
lcd.backlight(); // If your LCD has backlight control
pinMode(buzzerPin, OUTPUT);
pinMode(grnLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(armSwitchPin, INPUT_PULLUP); // Use INPUT_PULLUP to avoid external pull-up resistor
pinMode(armButtonPin, INPUT_PULLUP); // Use INPUT_PULLUP to avoid external pull-up resistor
pinMode(timerButtonPin, INPUT_PULLUP);
}
void loop() {
if (!armed) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System Safe");
if (digitalRead(timerButtonPin) == LOW) {
setTimer();
}
} else {
checkArming(); // Check if the key switch is turned and the arm button is pressed
updateCountdown();
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '#') {
// Check the entered code when '#' is pressed
checkCode();
} else if (key == '*') {
// Enter timer set mode when '*' is pressed
enterTimerSetMode();
} 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(armButtonPin);
if (digitalRead(armSwitchPin) == LOW && currentArmButtonState == LOW && lastArmButtonState == HIGH) {
// Key switch 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 the alarm
deactivateAlarm();
}
void activateAlarm() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ALARM ACTIVATED");
digitalWrite(redLed, HIGH);
tone(buzzerPin, 1000); // Sound the alarm
delay(5000); // Alarm duration, change as needed
resetSystem();
}
void deactivateAlarm() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ALARM DEACTIVATED");
digitalWrite(grnLed, LOW);
noTone(buzzerPin); // Stop the alarm sound
delay(2000); // Display the 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 the entered digit to the timer value
timerValue = timerValue * 10 + (key - '0');
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the 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, 2);
lcd.print("Countdown: ");
lcd.print(remainingTime);
lcd.print("s");
} else {
// Countdown finished, activate the alarm
activateAlarm();
}
}
}
void setTimer() {
timerSetMode = true;
enterTimerSetMode();
}