Hello, I am trying to make a two authentication system (RFID and Fingerprint); the problem is when I scanned my fingerprint it keep showing "Fingerprint not recognized" even though I enrolled my fingerprint. Spoiler I am yet a beginner in coding, so pretty much I tried using chatGPT on creting this code.
-------Code--------
#include <Adafruit_Fingerprint.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#include <RTClib.h>
#define SS_PIN 10
#define RST_PIN 9
String UID = "F9 FC F4 14";
byte lock = 0;
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 rfid(SS_PIN, RST_PIN);
RTC_DS1307 rtc; // RTC object
// Define fingerprint sensor
SoftwareSerial mySerial(2, 3); // RX, TX
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
bool fingerprintScanned = false;
void setup() {
Serial.begin(9600);
mySerial.begin(57600); // Make sure this baud rate matches your fingerprint sensor module
Serial.println("Initializing fingerprint sensor...");
finger.begin(57600); // No need to check the return value
servo.write(70);
lcd.init();
lcd.backlight();
servo.attach(3);
SPI.begin();
rfid.PCD_Init();
Wire.begin();
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
rtc.adjust(DateTime(__DATE__, __TIME__));
lcd.setCursor(0, 0);
lcd.print("Welcome!");
lcd.setCursor(0, 1);
lcd.print("Scan Your Card");
}
void loop() {
if (!rfid.PICC_IsNewCardPresent())
return;
if (!rfid.PICC_ReadCardSerial())
return;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Scanning RFID");
Serial.print("NUID tag is :");
String ID = "";
for (byte i = 0; i < rfid.uid.size; i++) {
lcd.print(".");
ID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
ID.concat(String(rfid.uid.uidByte[i], HEX));
delay(300);
}
ID.toUpperCase();
// Check if fingerprint scanning is in progress
if (fingerprintScanned) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please wait...");
delay(2000); // Adjust the delay as needed
return;
}
if (ID.substring(1) == UID && lock == 0) {
servo.write(70);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door is locked");
delay(1500);
lcd.clear();
lock = 1;
} else if (ID.substring(1) == UID && lock == 1) {
// Trigger fingerprint scanning
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Place finger...");
delay(1500); // Adjust the delay as needed
fingerprintScanned = checkFingerprint();
if (fingerprintScanned) {
servo.write(160);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Granted!");
delay(1500);
lcd.clear();
lock = 0;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Fingerprint not recognized!");
delay(1500);
lcd.clear();
}
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Denied Access!");
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome!");
lcd.setCursor(0, 1);
lcd.print("Scan Your Card");
}
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(rtc.now().hour());
lcd.print(":");
lcd.print(rtc.now().minute());
lcd.print(":");
lcd.print(rtc.now().second());
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(rtc.now().year(), DEC);
lcd.print("-");
lcd.print(rtc.now().month(), DEC);
lcd.print("-");
lcd.print(rtc.now().day(), DEC);
delay(5000);
}
bool checkFingerprint() {
Serial.println("Getting fingerprint image...");
uint8_t p = finger.getImage();
Serial.println("Image capture result: " + String(p));
if (p == FINGERPRINT_OK) {
Serial.println("Converting image to template...");
p = finger.image2Tz();
Serial.println("Conversion result: " + String(p));
if (p == FINGERPRINT_OK) {
Serial.println("Searching for fingerprint...");
p = finger.fingerFastSearch();
Serial.println("Search result: " + String(p));
if (p == FINGERPRINT_OK) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Fingerprint recognized!");
delay(1500);
lcd.clear();
fingerprintScanned = true; // Set flag to true
return true;
} else {
Serial.println("Finger search failed. Error code: " + String(p));
}
} else {
Serial.println("Image to template conversion failed. Error code: " + String(p));
}
} else {
Serial.println("Error capturing fingerprint image. Error code: " + String(p));
}
Serial.println("Fingerprint not recognized!");
return false;
}