To modify the code to add a buzzer alarm sound when an unauthorized finger is detected, you can add an additional case in the getFingerprintID()
function for FINGERPRINT_NOTFOUND
, which will trigger the buzzer to sound:
#include <Adafruit_Fingerprint.h>
//For Arduino UNO use pin #D2 from sensor (GREEN wire)
// pin #D3 is OUT from arduino (WHITE wire)
//For Arduino MEGA use pin #D10 from sensor (GREEN wire)
// pin #D11 is OUT from arduino (WHITE wire)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
//BUZZER pin
const int buzzer = 8;
void setup() {
Serial.begin(9600);
while (!Serial); // For Yun/Leo/Micro/Zero/...
delay(100);
Serial.println("\n\n Finger detect test");
// set the data rate for the sensor serial port
finger.begin(57600);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1) { delay(1); }
}
//BUZZER
pinMode(buzzer, OUTPUT);
finger.getTemplateCount();
Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
Serial.println("Waiting for valid finger...");
}
void loop() // run over and over again
{
getFingerprintIDez();
delay(50); //don't ned to run this at full speed.
}
uint8_t getFingerprintID() {
uint8_t p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println("No finger detected");
digitalWrite(buzzer, HIGH); //UNINDENTIFIED = LOCK
delay(3000);
digitalWrite(buzzer, LOW);
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
break;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
break;
default:
Serial.println("Unknown error");
break;
}
if (p != FINGERPRINT_OK) {
return p;
}
p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
break;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
break;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
break;
default:
Serial.println("Unknown error");
break;
}
if (p != FINGERPRINT_OK) {
return p;
}
p = finger.fingerFastSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Found a print match!");
// Unlock the door
digitalWrite(buzzer, LOW); // turn off buzzer
} else {
Serial.println("Did not find a match");
// Sound the alarm
digitalWrite(buzzer, HIGH); // turn on buzzer
}
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
// Return the finger ID
return finger.fingerID;
}
// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Found a print match!");
// Unlock the door
digitalWrite(buzzer, LOW); // turn off buzzer
} else {
Serial.println("Did not find a match");
// Sound the alarm
digitalWrite(buzzer, HIGH); // turn on buzzer
}
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
// Return the finger ID
return finger.fingerID;
}
I'm not sure if this code will work as I don't have the contents of the "SoftwareSerial.h". But you don't have to post them either. Also, unfortunately I don't know how long the buzzer is then. But you can usually set that higher with the "Delay", which is set to 50. I hope it works now