0% found this document useful (0 votes)
15 views2 pages

Final Code

The document describes an Arduino project that uses a keypad, servo motor, ultrasonic sensor and buzzer to control access through a gate. It defines constants, variables, functions for the keypad input, servo control, distance measurement and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Final Code

The document describes an Arduino project that uses a keypad, servo motor, ultrasonic sensor and buzzer to control access through a gate. It defines constants, variables, functions for the keypad input, servo control, distance measurement and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Keypad.

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

Servo servo; // Servo motor


char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2};
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM
);
const String password_1 = "555";
const String password_2 = "123";
const String password_3 = "999";
const String password_4 = "111";
String input_password;
int angle = 0; // Current angle of the servo motor
unsigned long lastTime;

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);
}

input_password = ""; // Reset the input password


} else {
input_password += key; // Append new character to input password string
}
}

}
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);

unsigned long duration = pulseIn(ECHO_PIN, HIGH);


int distance = duration * 0.034 / 2;

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
}

You might also like