0% found this document useful (0 votes)
8 views3 pages

code with buzzer

Uploaded by

bonjeerlyn
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)
8 views3 pages

code with buzzer

Uploaded by

bonjeerlyn
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/ 3

code with buzzer

#include <Adafruit_Fingerprint.h>
#include "U8glib.h"
#include <SoftwareSerial.h>

// Pin definitions
#define TRIG_PIN 12
#define ECHO_PIN 13
#define LED_Fingerprint_Accepted_PIN 4
#define LED_Fingerprint_Denied_PIN 5
#define Relay_Doorlock_PIN 6
#define BUZZER_PIN 7 // Pin connected to the buzzer

// Initialize OLED display


U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI

// Initialize SoftwareSerial for fingerprint sensor communication


SoftwareSerial mySerial(2, 3); // RX, TX

// Initialize fingerprint sensor


Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

// Variables
int finger_status = -1;
long duration;
int distance;
String lastMessage1 = "";
String lastMessage2 = "";

// Setup function
void setup() {
Serial.begin(9600);
mySerial.begin(57600); // Fingerprint sensor baud rate

pinMode(LED_Fingerprint_Accepted_PIN, OUTPUT);
pinMode(LED_Fingerprint_Denied_PIN, OUTPUT);
pinMode(Relay_Doorlock_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as output

digitalWrite(Relay_Doorlock_PIN, LOW); // Ensure the door is locked initially

if (finger.verifyPassword()) {
Serial.println("Fingerprint sensor detected!");
} else {
Serial.println("Fingerprint sensor not found. Check connections.");
while (1);
}

displayMessage("Initializing...", "Please wait");


delay(500); // Shortened delay for initialization
}

// Display helper function


void displayMessage(String message1, String message2) {
if (message1 != lastMessage1 || message2 != lastMessage2) {
lastMessage1 = message1;
lastMessage2 = message2;
u8g.firstPage();
do {
u8g.setFont(u8g_font_6x10);
u8g.drawStr(0, 20, message1.c_str());
u8g.drawStr(0, 40, message2.c_str());
} while (u8g.nextPage());
}
}

// Function to get distance from the ultrasonic sensor


int getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

duration = pulseIn(ECHO_PIN, HIGH);


distance = duration * 0.034 / 2; // Convert to cm

return distance;
}

// Function to sound a buzzer for security or error alert


void soundBuzzer(int duration) {
for (int i = 0; i < duration; i++) {
digitalWrite(BUZZER_PIN, HIGH); // Turn buzzer on
delay(100); // On for 100ms
digitalWrite(BUZZER_PIN, LOW); // Turn buzzer off
delay(100); // Off for 100ms
}
}

// Function to scan and verify fingerprint


void checkFingerprint() {
finger_status = finger.getImage();

if (finger_status == FINGERPRINT_NOFINGER) {
displayMessage("Waiting for", "fingerprint...");
return;
}

if (finger_status == FINGERPRINT_OK) {
finger_status = finger.image2Tz();

if (finger_status == FINGERPRINT_OK) {
finger_status = finger.fingerFastSearch();

if (finger_status == FINGERPRINT_OK) {
Serial.println("Fingerprint matched!");
digitalWrite(LED_Fingerprint_Accepted_PIN, HIGH);
digitalWrite(Relay_Doorlock_PIN, HIGH); // Unlock door
displayMessage("Access Granted", "Door Unlocked");
delay(3000); // Door remains unlocked for 3 seconds
digitalWrite(Relay_Doorlock_PIN, LOW); // Lock the door
displayMessage("Door Locked", "Ready for next access");
digitalWrite(LED_Fingerprint_Accepted_PIN, LOW);
} else {
Serial.println("Fingerprint not matched.");
digitalWrite(LED_Fingerprint_Denied_PIN, HIGH);
soundBuzzer(10); // Sound the buzzer for error alert
displayMessage("Access Denied", "Invalid Fingerprint");
delay(1000); // Reduced delay
digitalWrite(LED_Fingerprint_Denied_PIN, LOW);
}
} else {
Serial.println("Failed to process image.");
soundBuzzer(10); // Sound the buzzer for error alert
displayMessage("Error", "Try again.");
}
} else {
Serial.println("No valid fingerprint detected.");
soundBuzzer(10); // Sound the buzzer for error alert
displayMessage("No Finger", "Try again.");
}
}

// Main loop
void loop() {
distance = getDistance();

// Check if the distance is within the threshold


if (distance > 0 && distance <= 5) { // Adjust threshold as needed
displayMessage("Proximity Detected", "Unlocking Door...");
delay(500); // Delay before unlocking
digitalWrite(Relay_Doorlock_PIN, HIGH); // Unlock door
delay(3000); // Door remains unlocked for 3 seconds
digitalWrite(Relay_Doorlock_PIN, LOW); // Lock the door
displayMessage("Door Locked", "Ready for next access");
delay(1000); // Delay for display update
} else {
displayMessage("System Active", "Waiting for input...");
}

checkFingerprint();
}

You might also like