this is all my code
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <Wire.h>
// #include <RTClib.h>
// RTC_DS1307 rtc;
// #include <LiquidCrystal_I2C.h>
// LiquidCrystal_I2C lcd(0x27, 16, 2);
#define rly_1 50
#define rly_2 52
#define power1 22
#define power2 24
// Define LED color values
#define FINGERPRINT_LED_RED 1
#define FINGERPRINT_LED_BLUE 2
#define FINGERPRINT_LED_PURPLE 3
#define FINGERPRINT_LED_GREEN 4
#define FINGERPRINT_LED_CYAN 6
#define FINGERPRINT_LED_YELLOW 5
#define FINGERPRINT_LED_WHITE 7
// // Define software serial pins for fingerprint sensor
// SoftwareSerial mySerial1(10, 11);
// SoftwareSerial mySerial3(12, 13);
// Initialize fingerprint sensor
Adafruit_Fingerprint finger2 = Adafruit_Fingerprint(&Serial1);
Adafruit_Fingerprint finger1 = Adafruit_Fingerprint(&Serial3);
String data, datas;
int id = 1;
void setup() {
Serial.begin(9600); // Start the serial communication with the PC
pinMode(power1, OUTPUT); digitalWrite(power1, LOW);
pinMode(power2, OUTPUT); digitalWrite(power2, LOW);
delay(1000);
Serial2.begin(57600);
delay(1000);
finger1.begin(57600);
delay(1000);
finger2.begin(57600);
delay(1000);
// lcd.begin(16,2);
// lcd.backlight();
// lcd.clear();
pinMode(rly_1, OUTPUT); digitalWrite(rly_1, 1);
pinMode(rly_2, OUTPUT); digitalWrite(rly_2, 1);
// //Initialize RTC
// if (!rtc.begin()) {
// Serial.println("Couldn't find RTC");
// lcd.setCursor(0, 0);
// lcd.print("Couldn't find RTC");
// while (1);
// }
// if (!rtc.isrunning()) {
// Serial.println("RTC is NOT running!");
// // Set the date and time to the date and time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// }
// Initialize the fingerprint sensor
// mySerial1.listen();
if (finger1.verifyPassword()) {
Serial.println("Fingerprint sensor 1 initialized.");
} else {
Serial.println("Fingerprint sensor 1 initialization failed.");
while (1); // Halt the program if sensor initialization fails
}
// mySerial3.listen();
if (finger2.verifyPassword()) {
Serial.println("Fingerprint sensor 2 initialized.");
} else {
Serial.println("Fingerprint sensor 2 initialization failed.");
while (1); // Halt the program if sensor initialization fails
}
updateTemplateCount();
// finger1.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_CYAN, 0);
// finger2.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_CYAN, 0);
}
void loop() {
// DateTime now = rtc.now();
// //Display the time
// lcd.setCursor(1, 0);
// lcd.print("Time: ");
// if (now.hour() < 10) lcd.print('0'); // Add leading zero if needed
// lcd.print(now.hour());
// lcd.print(':');
// if (now.minute() < 10) lcd.print('0'); // Add leading zero if needed
// lcd.print(now.minute());
// lcd.print(':');
// if (now.second() < 10) lcd.print('0'); // Add leading zero if needed
// lcd.print(now.second());
// delay(500);
if (Serial.available() > 0) {
int command = Serial.parseInt(); // Read the incoming integer
if (command == 1) {
setRegisterMode();
} else if (command == 2) {
deleteFingerprint();
}
}
else {
processFingerprints();
}
if (Serial2.available() > 0){
String serial = Serial2.readStringUntil('\n');
serial.trim();
Serial.println(serial);
if (serial == "TimeIn") {
// lcd.setCursor(1,1);
// lcd.print(" Access Granted! ");
finger1.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_GREEN, 0); // Set LED to flashing green
delay(3000);
finger1.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_CYAN, 0); // Turn off the LED
// lcd.setCursor(1,1);
// lcd.print(" ");
digitalWrite(rly_1, 0);
digitalWrite(rly_2, 0);
Serial.println("Door Open");
// lcd.setCursor(1,1);
// lcd.print(" ");
// lcd.setCursor(1,1);
// lcd.print(" Time In ");
// delay(5000);
// lcd.setCursor(1,1);
// lcd.print(" ");
}
if (serial == "TimeOut"){
finger1.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_GREEN, 0); // Set LED to flashing green
delay(3000);
finger1.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_CYAN, 0); // Turn off the LED
// lcd.setCursor(1,1);
// lcd.print(" ");
// lcd.setCursor(1,1);
// lcd.print(" Time Out ");
digitalWrite(rly_1, 1);
digitalWrite(rly_2, 1);
Serial.println("Door Close");
delay(5000);
// lcd.setCursor(1,1);
// lcd.print(" ");
}
if (serial == "register"){
setRegisterMode();
}
}
delay(500);
}
void updateTemplateCount() {
finger1.getTemplateCount();
finger2.getTemplateCount();
if (finger1.templateCount == 0) {
Serial.println("No templates found on Finger 1.");
} else {
Serial.print("Templates available on Finger 1: ");
Serial.println(finger1.templateCount);
}
if (finger2.templateCount == 0) {
Serial.println("No templates found on Finger 2.");
} else {
Serial.print("Templates available on Finger 2: ");
Serial.println(finger2.templateCount);
}
}
void setAccessMode(Adafruit_Fingerprint &finger, const String &fingerName) {
// int p = finger.getImage();
// if (p == FINGERPRINT_NOFINGER) {
// return; // No finger detected
// } else if (p != FINGERPRINT_OK) {
// Serial.println("Error capturing image from " + fingerName + ". Code: " + String(p));
// return;
// }
// p = finger.image2Tz();
// if (p != FINGERPRINT_OK) {
// Serial.println("Error processing the fingerprint from " + fingerName + ". Code: " + String(p));
// return;
// }
// p = finger.fingerSearch();
// if (p == FINGERPRINT_OK) {
// Serial.println("Access Granted for " + fingerName);
// Serial.print("Found ID #");
// Serial.println(finger.fingerID);
// String data = "!/fingerprint/checkAccess.php";
// data += "?fingerprint_id=";
// data += String(finger.fingerID);
// data += "@";
// Serial2.println(data);
// Serial.println(data);
// Serial.print("Found a match! " + fingerName + " ID: "); Serial.println(finger.fingerID);
// finger.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_PURPLE, 0);
// // lcd.setCursor(1, 1);
// // lcd.print("Processing ....");
// } else if (p == FINGERPRINT_NOTFOUND) {
// Serial.println("Access Denied. " + fingerName + " fingerprint not found.");
// finger.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_RED, 0); // Set LED to flashing red
// delay(2000);
// finger.LEDcontrol(FINGERPRINT_LED_OFF, 0, 0, 0); // Turn off the LED
// finger.getImage(); // Optionally, reset the fingerprint sensor state
// } else {
// Serial.println("Access Denied for " + fingerName + ". Code: " + String(p));
// finger.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_RED, 0); // Set LED to flashing red
// delay(2000);
// finger.LEDcontrol(FINGERPRINT_LED_OFF, 0, 0, 0); // Turn off the LED
// finger.getImage(); // Optionally, reset the fingerprint sensor state
// }
int p = finger.getImage();
if (p == FINGERPRINT_NOFINGER) {
return; // No finger detected
} else if (p != FINGERPRINT_OK) {
Serial.println("Error capturing image from " + fingerName + ". Code: " + String(p));
return;
}
p = finger.image2Tz();
if (p != FINGERPRINT_OK) {
Serial.println("Error processing the fingerprint from " + fingerName + ". Code: " + String(p));
return;
}
p = finger.fingerSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Access Granted for " + fingerName);
Serial.print("Found ID #");
Serial.println(finger.fingerID);
// Handle access granted logic here
} else if (p == FINGERPRINT_NOTFOUND) {
Serial.println("Access Denied. " + fingerName + " fingerprint not found.");
// Handle access denied logic here
} else {
Serial.println("Access Denied for " + fingerName + ". Code: " + String(p));
}
}
void processFingerprints() {
// Check for access on both fingerprints
setAccessMode(finger1, "Finger1");
setAccessMode(finger2, "Finger2");
}
void setRegisterMode() {
Serial.println("Switching to register mode.");
// lcd.setCursor(1, 1);
// lcd.print(" Registering ");
while (true) {
updateTemplateCount();
int id = fingerEnroll();
if (id != -1) {
Serial.print("Fingerprint enrolled successfully with ID: ");
Serial.println(id);
// Transfer model from finger1 to finger2
if (transferFingerprint(id)) {
Serial.println("Fingerprint successfully transferred to second sensor.");
} else {
Serial.println("Error transferring fingerprint to second sensor.");
}
// datas = "!/scheduler/add_fingerprint.php";
// datas += "?fingerprintId=";
// datas += String(id);
// datas += "@";
// Serial2.println(datas);
// Serial.println(datas);
updateTemplateCount();
// lcd.setCursor(1, 1);
// lcd.print("Register Success");
finger1.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_GREEN, 0); // Set LED to flashing green
delay(2000);
// lcd.setCursor(1, 1);
// lcd.print(" ");
finger1.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_CYAN, 0); // Set LED to flashing green
Serial.print("Templates after enrollment: ");
Serial.println(finger1.templateCount);
break; // Exit the loop if enrollment is successful
} else {
Serial.println("Fingerprint enrollment failed. Trying again...");
}
}
}
int fingerEnroll() {
int p = -1;
// lcd.setCursor(1, 1);
// lcd.print(" Place Finger ");
Serial.println("Place your finger on the sensor.");
while (p != FINGERPRINT_OK) {
p = finger1.getImage();
if (p != FINGERPRINT_OK && p != FINGERPRINT_NOFINGER) {
Serial.println("Error capturing image. Code: " + String(p));
// lcd.setCursor(1, 1);
// lcd.print(" Error ");
return -1;
}
}
p = finger1.image2Tz(1);
if (p != FINGERPRINT_OK) {
// lcd.setCursor(1, 1);
// lcd.print(" Error ");
Serial.println("Error processing the fingerprint. Code: " + String(p));
return -1;
}
// lcd.setCursor(1, 1);
// lcd.print(" Remove Finger ");
Serial.println("Remove your finger.");
delay(2000);
p = 0;
while (p != FINGERPRINT_NOFINGER) {
p = finger1.getImage();
}
// lcd.setCursor(1, 1);
// lcd.print(" Place Again ");
Serial.println("Place the same finger again.");
p = -1;
while (p != FINGERPRINT_OK) {
p = finger1.getImage();
if (p != FINGERPRINT_OK && p != FINGERPRINT_NOFINGER) {
Serial.println("Error capturing image. Code: " + String(p));
return -1;
}
}
p = finger1.image2Tz(2);
if (p != FINGERPRINT_OK) {
Serial.println("Error processing the fingerprint. Code: " + String(p));
// lcd.setCursor(1, 1);
// lcd.print(" Error ");
return -1;
}
p = finger1.createModel();
if (p != FINGERPRINT_OK) {
Serial.println("Error creating model. Code: " + String(p));
// lcd.setCursor(1, 1);
// lcd.print(" Error ");
return -1;
}
// After storing the fingerprint model
int id = finger1.templateCount + 1; // ID should be based on current count
p = finger1.storeModel(id); // Store with the new ID
if (p != FINGERPRINT_OK) {
Serial.println("Error storing model. Code: " + String(p));
return -1;
}
// Add this line to see the stored ID
Serial.print("Stored fingerprint with ID: ");
Serial.println(id);
return id; // Return the new ID
}
void deleteFingerprint() {
uint8_t p = finger1.emptyDatabase();
if (p != FINGERPRINT_OK) {
Serial.print("Failed to delete templates from Finger1. Error: ");
Serial.println(p);
}
else{
Serial.println("All fingerprint deleted successfully");
}
p = finger2.emptyDatabase();
if (p != FINGERPRINT_OK) {
Serial.print("Failed to delete templates from Finger2. Error: ");
Serial.println(p);
}
else{
Serial.println("All fingerprint deleted successfully");
}
updateTemplateCount();
}
bool transferFingerprint(int id) {
Serial.print("Transferring fingerprint ID: ");
Serial.println(id);
// Load the model from the first sensor
int p = finger1.loadModel(id);
if (p != FINGERPRINT_OK) {
Serial.print("Error loading model from sensor 1. Code: ");
Serial.println(p);
return false; // Return false if loading fails
}
// Read the loaded fingerprint from the first sensor
// If the library provides a method to read the fingerprint data,
// you would use it here. Otherwise, proceed to store it in the second sensor.
// Retry mechanism for storing fingerprint on the second sensor
int retries = 5;
bool success = false;
while (retries > 0 && !success) {
Serial.println("Attempting to store model on sensor 2...");
// Store the model directly on the second sensor
p = finger2.storeModel(id);
if (p == FINGERPRINT_OK) {
Serial.println("Fingerprint transferred and stored successfully on sensor 2.");
success = true;
} else {
Serial.print("Error storing model into sensor 2. Code: ");
Serial.println(p);
Serial.println("Retrying...");
retries--;
delay(2000); // Wait before retrying
}
}
if (!success) {
Serial.println("Failed to store fingerprint on sensor 2 after 5 retries.");
}
return success;
}