I want to create a system that allows me to register and check if my fingerprint is stored on the fingerprint scanner. I have been able to make it work, but I need to uncomment each of the options in the loop code to be able to use it. If I enable both options, I am not able to press any buttons. How can I fix this issue? I'm only using one button btw
loop
void loop() {
// put your main code here, to run repeatedly:
pushButton.tick();
int result = getFingerprintIDez(); -- I need to uncomment this to make the button works
if (result > 0) {
return;
}
delay(10);
}
Full Code:
#include <Arduino.h>
#include <Adafruit_Fingerprint.h>
#include "OneButton.h"
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include "uRTCLib.h"
LiquidCrystal_I2C lcd(0x27, 16, 2);
OneButton pushButton(A3, true);
SoftwareSerial fingerSerial(2, 3);
uRTCLib rtc(0x68);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&fingerSerial);
int idCount = 1;
bool regMode = false;
char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
uint8_t id;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.init();
lcd.backlight();
centerText(0, "Initializing");
centerText(1, "Data");
while (!Serial)
;
delay(100);
Serial.println("\n\nAdafruit Fingerprint sensor enrollment");
lcd.clear();
finger.begin(57600);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
centerText(0, "Fingerprint");
centerText(1, "Found");
} else {
Serial.println("Did not find fingerprint sensor :(");
centerText(0, "Fingerprint");
centerText(1, "not Found");
while (1) { delay(1); }
}
finger.getTemplateCount();
if (finger.templateCount == 0) {
Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example.");
} else {
Serial.println("Waiting for valid finger...");
Serial.print("Sensor contains ");
Serial.print(finger.templateCount);
Serial.println(" templates");
}
delay(1000);
lcd.clear();
centerText(0, "Fingerprint");
lcd.setCursor(0, 1);
lcd.print("Attedance System");
URTCLIB_WIRE.begin();
// rtc.set(0, 28, 11, 6, 29, 3, 24);
// rtc.set(second, minute, hour, dayOfWeek, dayOfMonth, month, year)
// set day of week (1=Sunday, 7=Saturday)
pushButton.attachClick(incrementID);
pushButton.attachDoubleClick(decrementID);
pushButton.attachMultiClick(multiClick);
}
void loop() {
// put your main code here, to run repeatedly:
pushButton.tick();
int result = getFingerprintIDez();
if (result > 0) {
return;
}
delay(10);
}
void centerText(int Yaxis, String text) {
int totalColumns = 16;
int textLength = text.length();
int startingColumn = (totalColumns - textLength) / 2;
lcd.setCursor(startingColumn, Yaxis);
lcd.print(text);
}
void incrementID() {
if (regMode) {
idCount++;
if (idCount > 127) {
idCount = 1;
}
lcd.clear();
centerText(0, "Enter your ID:");
centerText(1, String(idCount));
}
delay(100);
}
void decrementID() {
if (regMode) {
idCount--;
if (idCount < 1) {
idCount = 1;
}
lcd.clear();
centerText(0, "Enter your ID:");
centerText(1, String(idCount));
}
delay(100);
}
void multiClick() {
int n = pushButton.getNumberClicks();
if (n == 3) {
// Set regMode to false when the button is clicked 3 times
Serial.print("3");
if (regMode) {
home();
}
} else if (n == 4) {
// Enroll the user when the button is clicked 4 times
if (!regMode) {
enroll();
}
} else if (n == 5) {
if (regMode) {
enrollnow();
}
} else {
Serial.print("multiClick(");
Serial.print(n);
Serial.println(") detected.");
}
}
void home() {
regMode = false;
idCount = 1;
lcd.clear();
centerText(0, "Fingerprint");
lcd.setCursor(0, 1);
lcd.print("Attedance System");
delay(100);
}
void enroll() {
lcd.clear();
centerText(0, "Enter your ID:");
regMode = true;
while (regMode) {
centerText(1, String(idCount));
pushButton.tick();
}
Serial.print("Exited enroll function.");
}
void enrollnow() {
id = idCount;
getFingerprintEnroll();
for (int i = 0; i < 127; i++)
;
{
// read the data on sd card
// if (read) {
// write
// break;
// }
regMode = false;
Serial.print(regMode);
}
return;
}
void checkFingerData() {
if (!regMode) {
if (finger.fingerID == 1) {
if (finger.confidence > 60) {
lcd.clear();
centerText(0, "Good Morning");
centerText(1, "User 1");
delay(1000);
lcd.clear();
getTime();
delay(1000);
lcd.clear();
centerText(0, "Fingerprint");
lcd.setCursor(0, 1);
lcd.print("Attendance System");
}
}
}
}
void getTime() {
rtc.refresh();
lcd.setCursor(0, 0);
lcd.print("Time: ");
int hour12 = rtc.hour() % 12;
if (hour12 == 0) {
hour12 = 12;
}
lcd.print(hour12);
lcd.print(":");
if (rtc.minute() < 10) {
lcd.print("0");
}
lcd.print(rtc.minute());
if (rtc.hour() < 12) {
lcd.print(" AM");
} else {
lcd.print(" PM");
}
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(rtc.month());
lcd.print("/");
lcd.print(rtc.day());
lcd.print("/");
lcd.print(rtc.year());
delay(1000);
}
uint8_t getFingerprintEnroll() {
int p = -1;
lcd.clear();
centerText(0, "Your ID: " + String(id));
centerText(1, "Place finger");
delay(2000);
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
lcd.clear();
centerText(0, "Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println("No Finger");
lcd.clear();
centerText(0, "No Finger Found");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
lcd.clear();
centerText(0, "Communication");
centerText(1, "Error");
break;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
lcd.clear();
centerText(0, "Imaging Error");
break;
default:
Serial.println("Unknown error");
lcd.clear();
centerText(0, "Unknown error");
break;
}
}
// OK success!
p = finger.image2Tz(1);
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
lcd.clear();
centerText(0, "Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
lcd.clear();
centerText(0, "Image too messy");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
lcd.clear();
centerText(0, "Communication");
centerText(1, "Error");
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
lcd.clear();
centerText(0, "Feature");
centerText(1, "Not Found 1");
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
lcd.clear();
centerText(0, "Feature");
centerText(1, "Not Found 2");
return p;
default:
Serial.println("Unknown error");
lcd.clear();
centerText(0, "Unknown error");
return p;
}
Serial.println("Remove finger");
lcd.clear();
centerText(0, "Remove");
centerText(1, "your Finger");
delay(2000);
p = 0;
while (p != FINGERPRINT_NOFINGER) {
p = finger.getImage();
}
Serial.print("ID ");
Serial.println(id);
p = -1;
Serial.println("Place same finger again");
lcd.clear();
centerText(0, "Place your");
centerText(1, "Finger Again");
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.print(".");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
break;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
break;
default:
Serial.println("Unknown error");
return;
}
}
// OK success!
p = finger.image2Tz(2);
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
return p;
default:
Serial.println("Unknown error");
return p;
}
// OK converted!
Serial.print("Creating model for #");
Serial.println(id);
p = finger.createModel();
if (p == FINGERPRINT_OK) {
Serial.println("Prints matched!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
lcd.clear();
centerText(0, "Communication");
centerText(1, "Error");
delay(500);
home();
return p;
} else if (p == FINGERPRINT_ENROLLMISMATCH) {
Serial.println("Fingerprints did not match");
lcd.clear();
centerText(0, "Try Again");
delay(500);
home();
return p;
} else {
Serial.println("Unknown error");
return p;
}
Serial.print("ID ");
Serial.println(id);
p = finger.storeModel(id);
if (p == FINGERPRINT_OK) {
Serial.println("Stored!");
lcd.clear();
centerText(0, "Your ID");
centerText(1, "is Stored");
delay(2000);
id = 1;
idCount = 1;
lcd.clear();
centerText(0, "Fingerprint");
lcd.setCursor(0, 1);
lcd.print("Attedance System");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_BADLOCATION) {
Serial.println("Could not store in that location");
return p;
} else if (p == FINGERPRINT_FLASHERR) {
Serial.println("Error writing to flash");
return p;
} else {
Serial.println("Unknown error");
return p;
}
}
// 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) return -1;
// found a match!
Serial.print("Found ID #");
Serial.print(finger.fingerID);
Serial.print(" with confidence of ");
Serial.println(finger.confidence);
checkFingerData();
return finger.fingerID;
}