#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>
// Define keypad parameters
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'}
};
// Verify that these pin numbers match your hardware connections
byte rowPins[ROWS] = {A0, A1, A2, A3}; // Row pin numbers
byte colPins[COLS] = {8, 7, 6, 5}; // Column pin numbers
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define servo motor parameters
Servo servo;
const int servoPin = 9;
// Define LCD parameters
LiquidCrystal lcd(12, 11, 4, 3, 2, 13);
// Define buzzer parameters
const int buzzerPin = 10; // Change to pin 10
#define NOTE_E5 659
#define NOTE_D5 587
#define NOTE_FS4 370
#define NOTE_GS4 415
#define NOTE_CS5 554
#define NOTE_B4 494
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_A4 440
#define NOTE_CS4 277
// Change this to make the song slower or faster
int tempo = 280;
// Notes of the melody followed by the duration
int melody[] = {
NOTE_E5, 8, NOTE_D5, 8, NOTE_FS4, 4, NOTE_GS4, 4,
NOTE_CS5, 8, NOTE_B4, 8, NOTE_D4, 4, NOTE_E4, 4,
NOTE_B4, 8, NOTE_A4, 8, NOTE_CS4, 4, NOTE_E4, 4,
NOTE_A4, 2,
};
int notes = sizeof(melody) / sizeof(melody[0]) / 2;
int wholenote = (60000 * 4) / tempo;
// Define correct passcode and variables
String enteredPasscode = "";
String correctPasscode = "";
// EEPROM address to store the passcode
const int passcodeAddress = 0;
// Function to read the passcode from EEPROM
String readPasscodeFromEEPROM() {
String passcode = "";
char c;
for (int i = 0; i < 4; i++) {
c = EEPROM.read(passcodeAddress + i);
if (c == 0 || c == 255) break;
passcode += c;
}
return passcode;
}
// Function to save the passcode to EEPROM
void savePasscodeToEEPROM(String passcode) {
for (int i = 0; i < 4; i++) {
EEPROM.write(passcodeAddress + i, passcode[i]);
}
}
// Define the DoorControl struct
struct DoorControl {
Servo* servo;
LiquidCrystal* lcd;
String* correctPasscode;
String* enteredPasscode;
int* melody;
int* notes;
int buzzerPin;
int wholenote;
};
// Function prototypes
void processKey(DoorControl* dc, char key);
void unlockDoor(DoorControl* dc);
void playMelody(DoorControl* dc);
void setup() {
Serial.begin(9600);
servo.attach(servoPin);
lcd.begin(16, 2);
lcd.print("Enter Passcode:");
pinMode(buzzerPin, OUTPUT);
correctPasscode = readPasscodeFromEEPROM();
if (correctPasscode == "") {
correctPasscode = "1234"; // Default passcode if not set
}
}
void loop() {
DoorControl doorControl = { &servo, &lcd, &correctPasscode, &enteredPasscode, melody, ¬es, buzzerPin, wholenote };
char key = keypad.getKey();
if (key) {
processKey(&doorControl, key);
}
}
void processKey(DoorControl* dc, char key) {
if (key == 'C') {
*(dc->enteredPasscode) = ""; // Clear the entered passcode
dc->lcd->setCursor(0, 1);
dc->lcd->print(" "); // Clear previous input
} else if (key == '#') {
if (*(dc->enteredPasscode) == *(dc->correctPasscode)) {
unlockDoor(dc);
} else {
dc->lcd->clear();
dc->lcd->print("Incorrect Passcode!");
delay(2000);
dc->lcd->clear();
dc->lcd->print("Enter Passcode:");
}
*(dc->enteredPasscode) = ""; // Reset the entered passcode
} else if (key == 'A') {
// Enter new passcode
dc->lcd->clear();
dc->lcd->print("New Passcode:");
delay(1000);
String newPasscode = "";
while (newPasscode.length() < 4) {
char newKey = keypad.getKey();
if (newKey) {
newPasscode += newKey;
dc->lcd->setCursor(0, 1);
dc->lcd->print(newPasscode);
}
}
*(dc->correctPasscode) = newPasscode;
savePasscodeToEEPROM(newPasscode);
dc->lcd->clear();
dc->lcd->print("Passcode Saved");
delay(2000);
dc->lcd->clear();
dc->lcd->print("Enter Passcode:");
} else {
*(dc->enteredPasscode) += key;
dc->lcd->setCursor(0, 1);
dc->lcd->print(" "); // Clear previous input
dc->lcd->setCursor(0, 1);
dc->lcd->print(*(dc->enteredPasscode));
}
}
void unlockDoor(DoorControl* dc) {
dc->lcd->clear();
dc->lcd->print("Door Unlocked");
dc->servo->write(0);
playMelody(dc);
delay(3000);
dc->servo->write(90);
delay(1000);
dc->lcd->clear();
dc->lcd->print("Enter Passcode:");
}
void playMelody(DoorControl* dc) {
for (int thisNote = 0; thisNote < *(dc->notes) * 2; thisNote += 2) {
int divider = dc->melody[thisNote + 1];
int noteDuration = 0;
if (divider > 0) {
noteDuration = (dc->wholenote) / divider;
} else if (divider < 0) {
noteDuration = (dc->wholenote) / abs(divider);
noteDuration *= 1.5;
}
tone(dc->buzzerPin, dc->melody[thisNote], noteDuration * 0.9);
delay(noteDuration);
noTone(dc->buzzerPin);
}
}