I am currently working on a project that will implement the Adafruit fingerprint scanner (PRODUCT ID: 751). I have written code which when no fingerprint is scanned, the LCD writes "Enter a Fingerprint", and when a correct fingering is identified, the LCD writes "Success. Hello Tyler". This part works completely fine. I am having trouble with having the LCD write something when an incorrect scan occurs.
I have the full code below, but I will explain the modifications I have made to the example code provided by Adafruit. In the loop section, I have states, where no fingerprint is scanned, where a correct fingerprint is scanned, and where an incorrect fingerprint is scanned. This is noted by a variable called "attempt" equaling 0, 1, or 2, respectively. The default if for attempt to equal zero, but when a correct scan occurs, I set attempt equal to one, then run the LCD code, then set attempt back to zero.
My question is, where should I set attempt equal to 2? I have tried several places, but none have worked.
Code: Select all
/***************************************************
This is an example sketch for our optical Fingerprint sensor
Designed specifically to work with the Adafruit BMP085 Breakout
----> http://www.adafruit.com/products/751
These displays use TTL Serial to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
// include the library code:
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#include <Adafruit_Fingerprint.h>
// On Leonardo/Micro or others with hardware serial, use those! #0 is green wire, #1 is white
// uncomment this line:
// #define mySerial Serial1
// For UNO and others without hardware serial, we must use software serial...
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino (WHITE wire)
// comment these two lines if using hardware serial
SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
int attempt;
int p;
void setup()
{
Serial.begin(9600);
while (!Serial); // For Yun/Leo/Micro/Zero/...
delay(100);
Serial.println("\n\nAdafruit 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); }
}
finger.getTemplateCount();
Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
Serial.println("Waiting for valid finger...");
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
attempt = 0;
}
void loop() // run over and over again
{
getFingerprintIDez();
delay(50); //don't ned to run this at full speed.
if (attempt == 0) { //user has not entered fingerprint
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter a");
lcd.setCursor(0, 1);
lcd.print("Fingerprint");
}
else if (attempt == 1){ //user has entered a correct fingerprint
if (finger.fingerID == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Success");
lcd.setCursor(0, 1);
lcd.print("Hello Tyler");
delay(1500);
attempt = 0;
}
if (finger.fingerID == 2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Success");
lcd.setCursor(0, 1);
lcd.print("Hello Kaleb");
delay(1500);
attempt = 0;
}
}
else if (attempt == 2) { //user has entered an incorrect fingerprint
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Failure");
lcd.setCursor(0, 1);
lcd.print("Try Again");
delay(1500);
attempt = 0;
}
}
uint8_t getFingerprintID() {
uint8_t p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.print("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.print("No finger detected");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.print("Communication error");
return p;
case FINGERPRINT_IMAGEFAIL:
Serial.print("Imaging error");
return p;
default:
Serial.print("Unknown error");
return p;
}
// OK success!
p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
Serial.print("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.print("Image too messy");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.print("Communication error");
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.print("Could not find fingerprint features");
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.print("Could not find fingerprint features");
return p;
default:
Serial.print("Unknown error");
return p;
}
// OK converted!
p = finger.fingerFastSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Found a print match!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
attempt = 2;
return p;
} else if (p == FINGERPRINT_NOTFOUND) {
Serial.println("Did not find a match");
attempt = 2;
return p;
} else {
Serial.println("Unknown error");
attempt = 2;
return p;
}
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
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) {
return -1;
}
// found a match!
// enter code here which you would like to execute when a patch is found
attempt = 1;
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
-Tyler