// === Pin Definitions ===
int rowPins[4] = {2, 3, 4, 5};
int colPins[4] = {6, 7, 8, 9};
char keys[4][4] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
int servoPin = 10;
int buzzerPin = 11;
int greenLED = 12;
int redLED = 13;
int trigPin = A2;
int echoPin = A3;
String password = "73737*A";
String entered = "";
int pos = 0;
void setup() {
Serial.begin(9600);
for (int i = 0; i < 4; i++) pinMode(rowPins[i], OUTPUT);
for (int j = 0; j < 4; j++) pinMode(colPins[j], INPUT_PULLUP);
pinMode(servoPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lock(); // Start locked
}
void loop() {
// Ultrasonic Distance Check
digitalWrite(trigPin, LOW); delayMicroseconds(2);
digitalWrite(trigPin, HIGH); delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance < 80) {
char key = getKey();
if (key != ' ') {
beepKey();
Serial.print("Key: ");
Serial.println(key);
if (key == '#') {
if (entered == password) {
Serial.println("✅ Access Granted");
ledSuccess(); // Green LED + melody (5 sec)
unlock(); // Unlock servo
delay(3000);
lock(); // Re-lock
} else {
Serial.println("❌ Access Denied");
ledErrorWithAlarm(); // 12-sec red LED & buzzer
}
entered = "";
} else {
entered += key;
}
}
}
delay(300);
}
// === Keypad Scanning ===
char getKey() {
for (int r = 0; r < 4; r++) {
digitalWrite(rowPins[r], LOW);
for (int c = 0; c < 4; c++) {
if (digitalRead(colPins[c]) == LOW) {
delay(200);
while (digitalRead(colPins[c]) == LOW);
digitalWrite(rowPins[r], HIGH);
return keys[r][c];
}
}
digitalWrite(rowPins[r], HIGH);
}
return ' ';
}
// === Servo Motor Control ===
void unlock() {
for (pos = 0; pos <= 90; pos += 5) {
int pwm = map(pos, 0, 180, 544, 2400);
analogWriteServo(servoPin, pwm);
delay(15);
}
}
void lock() {
for (pos = 90; pos >= 0; pos -= 5) {
int pwm = map(pos, 0, 180, 544, 2400);
analogWriteServo(servoPin, pwm);
delay(15);
}
}
void analogWriteServo(int pin, int pulseWidth) {
for (int i = 0; i < 10; i++) {
digitalWrite(pin, HIGH);
delayMicroseconds(pulseWidth);
digitalWrite(pin, LOW);
delay(20 - pulseWidth / 1000);
}
}
// === Key Press Feedback ===
void beepKey() {
tone(buzzerPin, 1000, 100);
delay(150);
}
// ✅ Feel-good Melody + Green LED blink (5 seconds)
void ledSuccess() {
int melody[] = {523, 587, 659, 698, 784, 880, 988, 1047};
int durations[] = {300, 300, 300, 300, 300, 300, 300, 500};
for (int i = 0; i < 8; i++) {
digitalWrite(greenLED, HIGH);
tone(buzzerPin, melody[i]);
delay(durations[i]);
digitalWrite(greenLED, LOW);
noTone(buzzerPin);
delay(100);
}
}
// ❌ Red LED + Buzzer for 12 seconds
void ledErrorWithAlarm() {
unsigned long startTime = millis();
while (millis() - startTime < 12000) {
tone(buzzerPin, 2000);
digitalWrite(redLED, HIGH);
delay(300);
digitalWrite(redLED, LOW);
delay(300);
}
noTone(buzzerPin);
}