Final Code
Final Code
h>
#include <Servo.h>
bool gatepass=false;
int count=0;
#define ROW_NUM 4
#define COLUMN_NUM 4
#define SERVO_PIN A0
#define TRIG_PIN 12 // Ultrasonic sensor trigger pin
#define ECHO_PIN 11 // Ultrasonic sensor echo pin
#define BUZZER_PIN 13
void setup() {
Serial.begin(115200);
input_password.reserve(32);
servo.attach(SERVO_PIN);
servo.write(0);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
lastTime = millis();
}
void loop() {
if(gatepass){
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '*') {
input_password = ""; // Reset the input password
} else if (key == '#') {
if (input_password == password_1 || input_password == password_2 ||
input_password == password_3 || input_password == password_4) {
Serial.println("The password is correct, rotating Servo Motor to 90°");
angle = 90;
servo.write(angle);
lastTime = millis();
gatepass=false;
} else {
Serial.println("The password is incorrect, try again");
delay(1000);
}
}
else{
if (angle == 90 && (millis() - lastTime) > 5000) {
angle = 0;
servo.write(angle);
Serial.println("Rotating Servo Motor to 0°");
}
// Ultrasonic sensor and buzzer functionality
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance < 5) {
digitalWrite(BUZZER_PIN, HIGH); // Activate the buzzer if the distance is
below the threshold
delay(1000);
digitalWrite(BUZZER_PIN, LOW);
gatepass=true;
} else {
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer if the distance is
above the threshold
}
}
delay(100); // Wait for a short delay before the next measurement
}