Untitled
unknown
plain_text
2 years ago
13 kB
8
Indexable
#include <Servo.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Adafruit_Fingerprint.h>
// fingerprint setup
#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);
#else
// On Leonardo/M0/etc, others with hardware serial, use hardware serial!
// #0 is green wire, #1 is white
#define mySerial Serial1
#endif
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
uint8_t id = 0;
// servo
Servo servo;
// lcd
int row = 0;
int col = 0;
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
// keypad
int step = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};
byte pin_rows[ROWS] = {A0, A1, A2, A3};
byte pin_cols[COLS] = {13, 12, 11, 10};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_cols, ROWS, COLS);
// button A,B states
int a = 0;
int b = 0;
// store password
int password = 0;
int correctPassword = 12345;
// door state
int doorOpen = 0;
void setup()
{
Serial.begin(9600);
servo.attach(A4);
lcd.begin(16, 2);
Serial.println("Start");
servo.write(0);
finger.begin(57600);
if (finger.verifyPassword())
{
Serial.println("Found fingerprint sensor!");
}
else
{
Serial.println("Did not find fingerprint sensor :(");
while (1)
{
delay(1);
}
}
}
void loop()
{
// variables
char key = keypad.getKey();
// start
if (doorOpen == 1)
{
servo.write(135);
}
if (key == '*')
{
doorOpen = 0;
step = 0;
password = 0;
servo.write(0);
}
// start
if (step == 0)
{
lcd.setCursor(row, col);
lcd.print("Start ");
lcd.setCursor(row, col + 1);
lcd.print("press D to continue");
if (key == 'D')
{
Serial.println("----------------------------------------------------------------");
Serial.println("A: Password");
Serial.println("B: Fingerprint");
Serial.println("----------------------------------------------------------------");
Serial.println("Fingerprint ");
Serial.println("----------------------------------------------------------------");
Serial.println("Press A to create a new fingerprint");
Serial.println("Press B to save a fingerprint");
Serial.println("Press C to open by fingerprinting");
Serial.println("Press D to delete a fingerprint");
step = 1;
}
}
// select password or fingerprint
if (step == 1)
{
lcd.setCursor(row, col);
lcd.println("A: Password ");
lcd.setCursor(row, col + 1);
lcd.println("B: Fingerprint ");
if (key)
{
switch (key)
{
case 'A': // set dieu kien de nhap password
a = 1;
step = 2;
break;
case 'B': // dieu kien de chay fingerprints
b = 1;
step = 2;
break;
default:
break;
}
}
}
// password
if (a == 1)
{
lcd.setCursor(row, col);
lcd.print("Enter Password ");
lcd.setCursor(row, col + 1);
lcd.print(" ");
password = getPassword();
doorOpen = checkPassword(password, correctPassword);
step = 1;
a = 0;
}
// fingerprint
if (b == 1)
{
lcd.setCursor(row, col);
lcd.println("Fingerprint ");
lcd.setCursor(row, col + 1);
lcd.println("Functions: A,C,D ");
// press keypad
if (key)
{
switch (key)
{
case 'A':
lcd.setCursor(row, col);
lcd.println("Enroll fingerprint");
lcd.setCursor(row, col + 1);
lcd.println(" ");
delay(3000);
// code them van tay
getFingerprintEnroll();
break;
case 'C':
lcd.setCursor(row, col);
lcd.println("Check finger");
lcd.setCursor(row, col + 1);
lcd.println(" ");
delay(3000);
// code nhan van tay
getFingerprintID();
break;
case 'D':
lcd.setCursor(row, col);
lcd.println("Delete ");
lcd.setCursor(row, col + 1);
lcd.print("Finger id ");lcd.print(id);lcd.print(" ");
delay(3000);
// code xoa van tay
deleteFingerprint(id);
break;
case '*':
b = 0;
break;
default:
break;
}
}
}
} // end loop
// functions
int getPassword()
{
String enteredPassword = "";
char key;
int i = 0;
while (true)
{
key = keypad.getKey();
if (key)
{
lcd.setCursor(row + i, col + 1);
lcd.println("*");
i++;
// Check if the entered password is complete
if (key == 'D')
{
break;
}
else if (key == '*')
{
// Clear the entered password if the asterisk key is pressed
enteredPassword = "";
}
else
{
// Append the entered digit to the password
enteredPassword += key;
}
}
}
// Convert the entered password from String to integer
int password = enteredPassword.toInt();
return password;
}
int checkPassword(int password, int correctPassword)
{
if (password == correctPassword)
{
lcd.setCursor(row, col);
lcd.print("Password Correct ");
lcd.setCursor(row, col + 1);
lcd.print(" ");
delay(1000);
return 1; // Password is correct
}
else
{
lcd.setCursor(row, col);
lcd.print("Password Incorrect");
lcd.setCursor(row, col + 1);
lcd.print(" ");
delay(1000);
return 0; // Password is incorrect
}
}
uint8_t getFingerprintEnroll()
{
int p = -1;
Serial.print("Waiting for valid finger to enroll as #");
Serial.println(id);
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");
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");
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!");
}
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;
}
lcd.setCursor(row, col);
lcd.print("Fingerprint Enrolled");
lcd.setCursor(row, col + 1);
lcd.print("Finger id ");
lcd.print(id);
delay(1000);
id++;
return true;
}
uint8_t deleteFingerprint(uint8_t id)
{
uint8_t p = -1;
p = finger.deleteModel(id);
if (p == FINGERPRINT_OK)
{
Serial.println("Deleted!");
}
else if (p == FINGERPRINT_PACKETRECIEVEERR)
{
Serial.println("Communication error");
}
else if (p == FINGERPRINT_BADLOCATION)
{
Serial.println("Could not delete in that location");
}
else if (p == FINGERPRINT_FLASHERR)
{
Serial.println("Error writing to flash");
}
else
{
Serial.print("Unknown error: 0x");
Serial.println(p, HEX);
}
lcd.setCursor(row, col);
lcd.print("Fingerprint deleted ");
lcd.setCursor(row, col + 1);
lcd.print("Finger id ");
lcd.print(id);lcd.print(" ");
delay(1000);
if (id > 0)
{
id--;
}
return p;
}
uint8_t getFingerprintID()
{
uint8_t p = finger.getImage();
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();
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)
{
Serial.println("Found a print match!");
}
else if (p == FINGERPRINT_PACKETRECIEVEERR)
{
Serial.println("Communication error");
return p;
}
else if (p == FINGERPRINT_NOTFOUND)
{
lcd.setCursor(row, col);
lcd.print("Fingerprint not found");
lcd.setCursor(row, col + 1);
lcd.print(" ");
Serial.println("Did not find a match");
delay(1000);
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);
lcd.setCursor(row, col);
lcd.print("Door is opened");
lcd.setCursor(row, col + 1);
lcd.print(" ");
lcd.print(id);
servo.write(135);
delay(1000);
return finger.fingerID;
}Editor is loading...