0% found this document useful (0 votes)
10 views7 pages

Sketch Dec17b

This document contains code for a fingerprint sensor library. It includes functions for initializing the sensor, enrolling fingerprints, searching for matches, and emptying the database. The code displays status messages on an LCD screen during enrollment and authentication.

Uploaded by

konrado.koziel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Sketch Dec17b

This document contains code for a fingerprint sensor library. It includes functions for initializing the sensor, enrolling fingerprints, searching for matches, and emptying the database. The code displays status messages on an LCD screen during enrollment and authentication.

Uploaded by

konrado.koziel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <Adafruit_Fingerprint.

h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)


// 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)
// Set up the serial port to use softwareserial..
SoftwareSerial mySerial(2, 3);
LiquidCrystal_I2C lcd(0x27,20,4);

#else
// On Leonardo/M0/etc, others with hardware serial, use hardware serial!
// #0 is green wire, #1 is white
#define mySerial Serial1

#endif

uint8_t id;

//button long press


const int button = 13;
long buttonTimer = 0;
long longPressTime = 5000;

boolean buttonActive = false;


boolean longPressActive = false;
//
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()
{
pinMode(button, INPUT);
Serial.begin(9600);
while (!Serial); // For Yun/Leo/Micro/Zero/...
delay(100);

lcd.init();
finger.begin(57600);
delay(5);
if (finger.verifyPassword()) {
Serial.println("Znaleziono czytnik lini papilarnych!");
} else {
Serial.println("Nie znaleciono czytnika");
while (1) { delay(1); }
}

finger.getTemplateCount();
if (finger.templateCount == 0) {
Serial.print("Brak odciskow w bazie!!");
}
else {
Serial.print("W bazie znajduje sie: "); Serial.print(finger.templateCount);
Serial.println(" odciskow palcow");
}
}
void loop() // run over and over again
{

if (digitalRead(button) == HIGH) {

if (buttonActive == false) {

buttonActive = true;
buttonTimer = millis();

if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {

longPressActive = true;
finger.emptyDatabase();
Serial.println("Database is empty");
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Reset ok");
delay(5000);
lcd.setCursor(1,0);
lcd.clear();
lcd.noBacklight();
delay(700);

} else {

if (buttonActive == true) {

if (longPressActive == true) {

longPressActive = false;

} else {

Serial.println("Ready to enroll a fingerprint!");


Serial.println("Please type in the ID # (from 1 to 127) you want to save this
finger as...");
finger.getTemplateCount();
int lastIdOfFinger = finger.templateCount;//read from file how many fingers have
lastIdOfFinger++;
id = lastIdOfFinger;

if (id == 0) {// ID #0 not allowed, try again!


return;
}
Serial.print("Enrolling ID #");
Serial.println(id);

while (! getFingerprintEnroll() )
delay(700);
}

buttonActive = false;
}

}
getFingerprintID();
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");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
return p;
default:
Serial.println("Unknown error");
return p;
}

// OK success!

p = finger.image2Tz();
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!
p = finger.fingerSearch();
if (p == FINGERPRINT_OK) {
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Odcisk poprawny");
delay(1000);
lcd.setCursor(1,0);
lcd.clear();
lcd.noBacklight();
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_NOTFOUND) {
lcd.setCursor(1,0);
lcd.print("Odcisk niepoprawny ");
delay(1000);
lcd.clear();
return p;
} else {
Serial.println("Unknown error");
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!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}

//add new fingerprint


uint8_t getFingerprintEnroll() {

int p = -1;
Serial.print("Waiting for valid finger to enroll as #"); Serial.println(id);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Dodawanie odcisku");
delay(500);
lcd.setCursor(0,0);
lcd.print("Przyloz palec");
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println(".");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
break;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
break;
default:
Serial.println("Unknown error");
break;
}
}

// OK success!

p = finger.image2Tz(1);
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;
}

Serial.println("Remove finger");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Podnies palec");
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();
lcd.setCursor(0,0);
lcd.print("Przyloz palec");
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");
break;
}
}

// 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");
return p;
} else if (p == FINGERPRINT_ENROLLMISMATCH) {
Serial.println("Fingerprints did not match");
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();
lcd.setCursor(0,0);
lcd.print("Dodano!");
} 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;
}

return true;
}

You might also like