#include <Keypad.h>
#include <EEPROM.h>
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] = {2, 3, 4, 5}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const int relayPin = 10;
String inputCode = "";
const int maxCodes = 5;
String authorizedCodes[maxCodes] = {"1001", "2002"}; // Default codes with IDs
int currentCodeIndex = 2; // Number of currently stored codes
String masterCode = "9999"; // Master code for adding/deleting codes
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
// Read the stored codes from EEPROM
for (int i = 0; i < maxCodes; i++) {
String code = "";
for (int j = 0; j < 4; j++) {
char c = EEPROM.read(i * 4 + j);
if (c >= '0' && c <= '9') {
code += c;
}
}
if (code.length() == 4) {
authorizedCodes[i] = code;
}
}
Serial.println("Setup complete. Ready to receive input.");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print("Key pressed: ");
Serial.println(key);
inputCode += key;
Serial.print("Current input code: ");
Serial.println(inputCode);
if (inputCode.length() == 4) {
Serial.print("Final input code: ");
Serial.println(inputCode);
if (isAuthorized(inputCode)) {
Serial.println("Access Granted");
digitalWrite(relayPin, HIGH); // Activate relay
delay(5000); // Keep relay on for 5 seconds
digitalWrite(relayPin, LOW); // Deactivate relay
} else if (inputCode == masterCode) {
Serial.println("Master Code Entered");
inputCode = ""; // Reset input code
processMasterMode();
} else {
Serial.println("Access Denied");
}
inputCode = ""; // Reset input code
}
}
}
void processMasterMode() {
Serial.println("Press * to add a code or # to delete a code");
while (true) {
char key = keypad.getKey();
if (key) {
Serial.print("Master mode key pressed: ");
Serial.println(key);
if (key == '*') {
addCode();
break;
} else if (key == '#') {
deleteCode();
break;
}
}
}
}
bool isAuthorized(String code) {
for (int i = 0; i < maxCodes; i++) {
if (authorizedCodes[i] == code) {
return true;
}
}
return false;
}
void addCode() {
if (currentCodeIndex >= maxCodes) {
Serial.println("Max number of codes reached.");
return;
}
Serial.println("Enter ID Number:");
String id = "";
while (id.length() < 1) {
char key = keypad.getKey();
if (key) {
Serial.print("ID key pressed: ");
Serial.println(key);
id += key;
}
}
Serial.println("Enter New 4-Digit Code:");
String newCode = "";
while (newCode.length() < 4) {
char key = keypad.getKey();
if (key) {
Serial.print("New code key pressed: ");
Serial.println(key);
newCode += key;
}
}
String codeWithID = id + newCode;
authorizedCodes[currentCodeIndex] = codeWithID;
currentCodeIndex++;
// Save the new code to EEPROM
for (int i = 0; i < 4; i++) {
EEPROM.write((currentCodeIndex - 1) * 4 + i, codeWithID[i]);
}
Serial.println("New Code Set");
}
void deleteCode() {
Serial.println("Enter ID to Delete:");
String id = "";
while (id.length() < 1) {
char key = keypad.getKey();
if (key) {
Serial.print("Delete ID key pressed: ");
Serial.println(key);
id += key;
}
}
for (int i = 0; i < maxCodes; i++) {
if (authorizedCodes[i].startsWith(id)) {
authorizedCodes[i] = "";
for (int j = 0; j < 4; j++) {
EEPROM.write(i * 4 + j, 0);
}
Serial.println("Code Deleted");
return;
}
}
Serial.println("ID not found");
}