#include <Keypad.h>
// Pin declaration
const int ROWS = 4;
const int COLS = 4;
int doorPin = 23;
int buzzerPin = 32;
int checkingFlag = 0;
int DISENABLE_PIN=34;
int DISENABLEFLAG;
boolean DIGITAL_DISENABLE=false;
const char DIGITAL_DISENABLECODE[5]="0987";
const char restartcode[5]="0369";
char enteredPasscode[5];
int passcodeIndex = 0;
const char passcode[] = "1234";
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {15, 2, 0, 4}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {16, 17, 5, 18}; // Connect to the cols input of the keypad
// Object declaration of keypad
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// State of door and alarm
boolean doorOpen = false;
boolean alarmOn = false;
// Times
unsigned long doorOpenTime = 0;
const unsigned long maxTime = 20000;
void setup() {
pinMode(doorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(DISENABLE_PIN,INPUT);
Serial.begin(9600);
/*pinMode(buzzerPin, HIGH);
delay(500);
pinMode(buzzerPin,LOW);
delay(500);
//door colse time
*/
// delay(10000);
Serial.println("ALARAM STARTED");
}
void loop() {
//Serial.println("ALARAM STARTED");
DISENABLEFLAG=digitalRead(DISENABLE_PIN);
doorOpen = digitalRead(doorPin) == HIGH;
checkingFlag = 0;
if(DISENABLEFLAG==HIGH){
Serial.println("System Hard Disenable...");
delay(5000);
}
if (doorOpen&&DISENABLEFLAG==LOW && DIGITAL_DISENABLE==false) {
checkingFlag = 1;
Serial.println("Enter The Key");
doorOpenTime = millis(); // Start the clock
}
// Looping
while (checkingFlag == 1 && millis() - doorOpenTime < maxTime) {
char key = myKeypad.getKey();
if (key != NO_KEY) {
if (key == '#') {
// Check if the entered passcode matches
if (strcmp(enteredPasscode, passcode) == 0) {
Serial.println("Access Granted");
digitalWrite(buzzerPin,HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
// Turn off the buzzer
digitalWrite(buzzerPin, LOW);
checkingFlag = 0; // Exit loop
}
else if(strcmp(enteredPasscode,restartcode)==0){
Serial.println("System Get Reset....");
ESP.restart();
}
//digital enable
else if(strcmp(enteredPasscode,DIGITAL_DISENABLECODE)==0){
Serial.println("System In DisEnble State....");
DIGITAL_DISENABLE=true;
} else {
Serial.println("Access Denied");
}
//Restart funtion
// Reset entered passcode
passcodeIndex = 0;
memset(enteredPasscode, 0, sizeof(enteredPasscode));
} else if (key == '*') {
// Clear entered passcode
passcodeIndex = 0;
memset(enteredPasscode, 0, sizeof(enteredPasscode));
} else {
// Add key to entered passcode
if (passcodeIndex < 4) { // Avoid buffer overflow
enteredPasscode[passcodeIndex++] = key;
Serial.print("Entered Passcode: ");
Serial.println(enteredPasscode);
}
}
}
}
while (DIGITAL_DISENABLE==true) {
char key = myKeypad.getKey();
if (key != NO_KEY) {
if (key == '#') {
// Check if the entered passcode matches
if(strcmp(enteredPasscode,DIGITAL_DISENABLECODE)==0){
DIGITAL_DISENABLE=false;
Serial.println("Alaram Enabled");
ESP.restart();
}
// Reset entered passcode
passcodeIndex = 0;
memset(enteredPasscode, 0, sizeof(enteredPasscode));
} else if (key == '*') {
// Clear entered passcode
passcodeIndex = 0;
memset(enteredPasscode, 0, sizeof(enteredPasscode));
} else {
// Add key to entered passcode
if (passcodeIndex < 4) { // Avoid buffer overflow
enteredPasscode[passcodeIndex++] = key;
// Serial.println("ENTER KEY FOR ENABLE THE ALARM SYSTEM..");
Serial.print("Entered Key: ");
Serial.println(enteredPasscode);
}
}
}
}
// Alarm activated if door open for too long
if (millis() - doorOpenTime >= maxTime && checkingFlag==1&&DIGITAL_DISENABLE==false) {
while(true){
alarmOn = true;
Serial.println("Alarm activated.");
digitalWrite(buzzerPin, HIGH);
}
}
}