Skip to content

Instantly share code, notes, and snippets.

@taylor224
Last active June 1, 2016 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save taylor224/a0f776eddb741757476d to your computer and use it in GitHub Desktop.
Save taylor224/a0f776eddb741757476d to your computer and use it in GitHub Desktop.
Arduino Fingerprint System
/*************************************************
* Public Constants
*************************************************/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
/***************************************************
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 <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include "pitches.h"
void playSound();
void playStartSound();
void playSuccessSound();
void playErrorSound();
int getFingerprintEnroll(uint8_t id);
int getFingerprintID();
int getFingerprintIDez();
int deleteFingerprint(uint8_t id);
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino (WHITE wire)
SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
void setup()
{
Serial.begin(9600);
Serial.println("[INFO] System Starting...");
// set the data rate for the sensor serial port
finger.begin(57600);
if (finger.verifyPassword()) {
Serial.println("[ALERT] Fingerprint Ready");
playStartSound();
} else {
Serial.println("[ERROR] Fingerprint Sensor ERROR");
while (1);
}
}
void loop() // run over and over again
{
Serial.println("Enter Code Number (1 - Enroll / 2 - CheckID / 9 - Delete Fingerprint) : ");
uint8_t num = -1;
while (true) {
while (! Serial.available());
char c;
while(! isdigit(c))
{
c = Serial.read();
char atoichar[1] = {c};
num = atoi(atoichar);
}
//id *= 10;
//id += c - '0';
if (num == 1)
{
int idnum = -1;
Serial.println("Type ID to enroll fingerprint : ");
while(true)
{
char idchar = Serial.read();
if(! isdigit(idchar))
{
continue;
}
char idchararray[1] = {idchar};
idnum = atoi(idchararray);
if (idnum >= 0)
break;
}
Serial.print(idnum);
if (idnum != -1)
{
while (! getFingerprintEnroll(idnum) );
playSuccessSound();
break;
}
}
if (num == 2)
{
Serial.println("Put your finger to sensor");
while (true)
{
int fingerid = -1;
while(fingerid == -1)
fingerid = getFingerprintID();
if (fingerid >= 0)
{
Serial.print("[ALERT] Finger ID : "); Serial.println(fingerid);
playSuccessSound();
break;
}
else
{
switch(fingerid)
{
case -11:
Serial.println("[ERROR] Fingerprint Not Found ");
playErrorSound();
break;
}
break;
}
}
break;
}
if (num == 9)
{
int idnum = -1;
Serial.println("Type ID to delete fingerprint : ");
while(true)
{
char idchar = Serial.read();
if(! isdigit(idchar))
{
continue;
}
char idchararray[1] = {idchar};
idnum = atoi(idchararray);
if (idnum >= 0)
break;
}
if (idnum != -1)
{
deleteFingerprint(idnum);
break;
}
}
}
}
void playSound() {
tone(8, NOTE_G7, 1000/6);
int pauseBetweenNotes = 1000 / 6 * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
void playStartSound() {
tone(8, NOTE_G6, 1000/6);
delay(1000/4 * 1.30);
tone(8, NOTE_FS6, 1000/6);
delay(1000/6 * 1.30);
tone(8, NOTE_D6, 1000/6);
int pauseBetweenNotes = 1000 / 6 * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
void playSuccessSound() {
tone(8, NOTE_C6, 1000/6);
delay(1000/6 * 1.30);
tone(8, NOTE_E6, 1000/6);
int pauseBetweenNotes = 1000 / 6 * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
void playErrorSound() {
tone(8, NOTE_F6, 1000/6);
delay(1000/6 * 1.30);
tone(8, NOTE_F6, 1000/6);
int pauseBetweenNotes = 1000 / 6 * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
int enrollid;
int getFingerprintEnroll(uint8_t idint) {
enrollid = idint;
Serial.print("Enroll ID : ");
Serial.print(enrollid);
Serial.println("");
uint8_t p = -1;
Serial.println("Waiting for valid finger to enroll");
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
playSound();
Serial.println("[INFO] Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.print(".");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("[ERROR] Communication error");
return -1;
break;
case FINGERPRINT_IMAGEFAIL:
Serial.println("[ERROR] Imaging error");
return -2;
break;
default:
Serial.println("[ERROR] Unknown error");
return -3;
break;
}
}
// OK success!
p = finger.image2Tz(1);
switch (p) {
case FINGERPRINT_OK:
Serial.println("[INFO] Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("[ERROR] Image too messy");
return -4;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("[ERROR] Communication error");
return -5;
case FINGERPRINT_FEATUREFAIL:
Serial.println("[ERROR] Could not find fingerprint features");
return -6;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("[ERROR] Could not find fingerprint features");
return -7;
default:
Serial.println("[ERROR] Unknown error");
return -8;
}
Serial.println("Remove finger");
delay(2000);
p = 0;
while (p != FINGERPRINT_NOFINGER) {
p = finger.getImage();
}
p = -1;
Serial.println("Place same finger again");
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
playSound();
Serial.println("[INFO] Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.print(".");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("[ERROR] Communication error");
return -1;
case FINGERPRINT_IMAGEFAIL:
Serial.println("[ERROR] Imaging error");
return -2;
default:
Serial.println("[ERROR] Unknown error");
return -3;
}
}
// OK success!
p = finger.image2Tz(2);
switch (p) {
case FINGERPRINT_OK:
Serial.println("[INFO] Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("[ERROR] Image too messy");
return -4;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("[ERROR] Communication error");
return -5;
case FINGERPRINT_FEATUREFAIL:
Serial.println("[ERROR] Could not find fingerprint features");
return -6;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("[ERROR] Could not find fingerprint features");
return -7;
default:
Serial.println("[ERROR] Unknown error");
return -8;
}
// OK converted!
p = finger.createModel();
if (p == FINGERPRINT_OK) {
Serial.println("[INFO] Prints matched!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("[ERROR] Communication error");
return -9;
} else if (p == FINGERPRINT_ENROLLMISMATCH) {
Serial.println("[ERROR] Fingerprints did not match");
return -10;
} else {
Serial.println("[ERROR] Unknown error");
return -11;
}
Serial.println("[ALERT] Enroll ID : ");
Serial.print(enrollid);
Serial.println("");
p = finger.storeModel(enrollid);
if (p == FINGERPRINT_OK) {
Serial.println("[ALERT] Stored!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("[ERROR] Communication error");
return -12;
} else if (p == FINGERPRINT_BADLOCATION) {
Serial.println("[ERROR] Could not store in that location");
return -13;
} else if (p == FINGERPRINT_FLASHERR) {
Serial.println("[ERROR] Error writing to flash");
return -14;
} else {
Serial.println("[ERROR] Unknown error");
return -15;
}
}
int getFingerprintID() {
uint8_t p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("[INFO] Image taken");
playSound();
break;
case FINGERPRINT_NOFINGER:
//Serial.println("No finger detected");
return -1;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("[ERROR] Communication error");
return -2;
case FINGERPRINT_IMAGEFAIL:
Serial.println("[ERROR] Imaging error");
return -3;
default:
Serial.println("[ERROR] Unknown error");
return -4;
}
// OK success!
p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
Serial.println("[INFO] Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("[ERROR] Image too messy");
return -5;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("[ERROR] Communication error");
return -6;
case FINGERPRINT_FEATUREFAIL:
Serial.println("[ERROR] Could not find fingerprint features");
return -7;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("[ERROR] Could not find fingerprint features");
return -8;
default:
Serial.println("[ERROR] Unknown error");
return -9;
}
// OK converted!
p = finger.fingerFastSearch();
if (p == FINGERPRINT_OK) {
Serial.println("[INFO] Found a print match!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("[ERROR] Communication error");
return -10;
} else if (p == FINGERPRINT_NOTFOUND) {
Serial.println("[ERROR] Did not find a match");
return -11;
} else {
Serial.println("[ERROR] Unknown error");
return -12;
}
// found a match!
Serial.print("[ALERT] 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;
playSound();
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
int deleteFingerprint(uint8_t id) {
uint8_t p = -1;
Serial.print("[ALERT] Deleting Fingerprint id "); Serial.println(id);
p = finger.deleteModel(id);
if (p == FINGERPRINT_OK) {
Serial.println("[INFO] Deleted!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("[ERROR] Communication error");
return -1;
} else if (p == FINGERPRINT_BADLOCATION) {
Serial.println("[ERROR] Could not delete in that location");
return -2;
} else if (p == FINGERPRINT_FLASHERR) {
Serial.println("[ERROR] Error writing to flash");
return -3;
} else {
Serial.print("[ERROR] Unknown error: 0x"); Serial.println(p, HEX);
return -4;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment