//muhammad irsyad bin faris, muhammad khairil zakhwan bin mohd khair
//muhammad danial haiqal bin abd aziz, muhammad iqbal irfan bin amir aiman luganathan
#include <Servo.h> // include the servo library
#include <LiquidCrystal.h> // include the LCD library
#include <Keypad.h> // include the Keypad library
// ultrasonic sensor pins
const int trigPin = 6;
const int echoPin = 5;
// servo motor pins
const int enaPin = 8;
const int servoPin = 9;
// buzzer pin
const int buzzerPin = 11;
// keypad pins
const int numRows = 4;
const int numCols = 3;
char keys[numRows][numCols] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[numRows] = {10, 8, 1, 0};
byte colPins[numCols] = {5, 4, 3};
// LCD screen pins
const int lcdRSPin = 12;
const int lcdENPin = 13;
const int lcdD4Pin = 2;
const int lcdD5Pin = 3;
const int lcdD6Pin = 4;
const int lcdD7Pin = 7;
Servo myServo; // create a servo object
LiquidCrystal lcd(lcdRSPin, lcdENPin, lcdD4Pin, lcdD5Pin, lcdD6Pin, lcdD7Pin); // create an LCD object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, numRows, numCols); // create a keypad object
const char* passcode = "1234"; // define the passcode
int passcodeIndex = 0; // index of the passcode that the user has entered so far
void setup() {
myServo.attach(servoPin); // attach the servo motor to the control pin
pinMode(trigPin, OUTPUT); // set the trig pin as an output
pinMode(echoPin, INPUT); // set the echo pin as an input
pinMode(enaPin, OUTPUT); // set the enable pin as an output
pinMode(buzzerPin, OUTPUT); // set the buzzer pin as an output
lcd.begin(16, 2); // initialize the LCD screen with 16 columns and 2 rows
// initialize the keypad and set the number of columns and rows
keypad.begin(makeKeymap(keys));
}
void loop() {
long duration, distance;
// trigger the ultrasonic sensor by sending a pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// measure the duration of the pulse
duration = pulseIn(echoPin, HIGH);
// convert the duration to a distance
distance = duration / 2 / 29.1;
// if someone is within 50 cm of the sensor, start the password system
if (distance < 50) {
lcd.clear(); // clear the LCD screen
lcd.setCursor(0, 0); // set the cursor to the first column of the first row
lcd.print("Enter passcode:"); // prompt the user to enter the passcode
lcd.setCursor(0, 1); // set the cursor to the first column of the second row
lcd.print("____________"); // display an underscore for each digit of the passcode
lcd.setCursor(0, 1); // set the cursor to the first column of the second row
passcodeIndex = 0; // reset the passcode index
// loop until the user enters the correct passcode or cancels the password system
while (true) {
// check if the user pressed a key
char key = keypad.getKey();
if (key) {
// if the key is a digit, add it to the passcode
if (key >= '0' && key <= '9') {
lcd.print(key); // display the digit on the LCD screen
if (key == passcode[passcodeIndex]) {
// if the digit is correct, move on to the next digit
passcodeIndex++;
if (passcodeIndex == 4) {
// if all digits of the passcode have been entered, open the door and play a melody
digitalWrite(enaPin, LOW); // enable the servo motor
myServo.write(180); // turn the servo motor to 180 degree
delay(3000); // wait for 5 seconds
myServo.write(0); // rotate the servo back to lock the door
lcd.clear(); // clear the LCD screen
lcd.setCursor(0, 0); // set the cursor to the first column of the first row
lcd.print("WELCOME HOME"); // display "WELCOME HOME" on the LCD screen
// play the melody of "You Should Probably Leave" by Chris Stapleton on the buzzer
int tempo = 150; // set the tempo of the melody (in ms)
tone(buzzerPin, 262, tempo);
delay(tempo);
tone(buzzerPin, 262, tempo);
delay(tempo);
tone(buzzerPin, 392, tempo);
delay(tempo);
tone(buzzerPin, 392, tempo);
delay(tempo);
tone(buzzerPin, 440, tempo);
delay(tempo);
tone(buzzerPin, 440, tempo);
delay(tempo);
tone(buzzerPin, 392, tempo);
delay(tempo);
tone(buzzerPin, 349, tempo);
delay(tempo);
tone(buzzerPin, 349,tempo);
delay(tempo);
tone(buzzerPin, 330, tempo);
delay(tempo);
tone(buzzerPin, 330, tempo);
delay(tempo);
tone(buzzerPin, 294, tempo);
delay(tempo);
tone(buzzerPin, 294, tempo);
delay(tempo);
tone(buzzerPin, 262, tempo);
delay(tempo);
tone(buzzerPin, 262, tempo);
delay(tempo);
break; // exit the loop
delay(100);
}
} else {
// if the digit is incorrect, reset the passcode index
passcodeIndex = 0;
lcd.setCursor(0, 1); // set the cursor to the first column of the second row
lcd.print("____________"); // display an underscore for each digit of the passcode
lcd.setCursor(0, 1); // set the cursor to the first column of the second row
}
} else if (key == '*') {
// if the key is '*', cancel the password system and close the door
digitalWrite(enaPin, LOW); // enable the servo motor
myServo.write(0); // turn the servo motor to 0 degrees
delay(1000); // wait for the door to close
digitalWrite(enaPin, HIGH); // disable the servo motor
break; // exit the loop
}
}
}
}
}