Skip to content

Instantly share code, notes, and snippets.

@tyzhou05
Created July 1, 2020 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyzhou05/65e01be6ae01fad7a55b68ce1a02dd62 to your computer and use it in GitHub Desktop.
Save tyzhou05/65e01be6ae01fad7a55b68ce1a02dd62 to your computer and use it in GitHub Desktop.
/*Tony Z.'s BlueStamp 2020 Fingerprint ID With Safe code */
#include "Adafruit_Keypad.h"
#include <Password.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <Adafruit_Fingerprint.h>
#define KEYPAD_PID3844
#define R1 2
#define R2 3
#define R3 4
#define R4 5
#define C1 8
#define C2 9
#define C3 10
#define C4 7
#include "keypad_config.h"
Adafruit_Keypad customKeypad = Adafruit_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
#define LOCKED 2
#define PASSWORD_OK 1
#define UNLOCKED 0
int LockState = LOCKED;
long StartTime = 0;
/// Variables for Password code
Password password = Password("1234");
int counter = 0;
Servo servo1;
boolean passwordJustEntered = false;
boolean passwordWasWrong = false;
boolean fingerprintWasWrong = false;
boolean safeIsUnlocked = false;
int getFingerprintIDez();
SoftwareSerial mySerial(11, 12);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
void setup() {
Serial.begin(9600);
customKeypad.begin();
servo1.attach(6);
// Initialize state and communications
setLockState(LOCKED);
Serial.begin(9600);
finger.begin(57600);
//servo1.write(270);
// Connect to the sensor
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor :)");
}
else {
Serial.println("Did not find fingerprint sensor :(");
}
}
void loop() {
customKeypad.tick();
while(customKeypad.available()){
keypadEvent e = customKeypad.read();
if((e.bit.EVENT) == KEY_JUST_PRESSED) {
if(passwordJustEntered == false){
Serial.println((char)e.bit.KEY);
password.append((char)e.bit.KEY);
counter++;
} else if((passwordJustEntered == true)) {
if((char)(e.bit.KEY) == '*' && safeIsUnlocked == true) {
Serial.println((char)e.bit.KEY);
Serial.println("The safe has been relocked because you pressed *.");
servo1.write(90);
setLockState(LOCKED);
password.reset();
counter = 0;
safeIsUnlocked = false;
}
if(passwordWasWrong = true) {
passwordWasWrong = false;
}
if(fingerprintWasWrong = true) {
fingerprintWasWrong = false;
}
passwordJustEntered = false;
}
}
//a password is entered
if(counter == 4) {
passwordJustEntered = true;
if((password.evaluate()?"true":"false") == "true") {
setLockState(PASSWORD_OK);
Serial.println("got into true password loop");
if(LockState == PASSWORD_OK) {
Serial.println("got into LockState == passwordok loop, 5 second delay until fingerprint scan");
delay(5000);
if(getFingerprintIDez() != -1) {
// Valid fingerprint - advance state to UNLOCKED
setLockState(UNLOCKED);
Serial.println("The fingerprint did match, the safe is now unlocked");
safeIsUnlocked = true;
servo1.write(0);
} else if(getFingerprintIDez() == -1){
// Invalid fingerprint
fingerprintWasWrong = true;
Serial.println("The fingerprint did not match");
setLockState(LOCKED);
}
if (millis () - StartTime > 5000) {
setLockState(LOCKED); // Time-out - go back to the LOCKED state
}
}
} else {
passwordWasWrong = true;
Serial.println("The password was incorrect.");
setLockState(LOCKED);
}
counter = 0;
password.reset();
}
delay(10);
} // end of while loop
} // end of void loop() loop
//methods from safeTutorial
//scans fingerprint and returns ID and confidence #
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;
}
void setLockState(int state) {
LockState = state;
StartTime = millis();
if (state == LOCKED) {
Serial.println("Safe is currently locked.");
} else if (state == PASSWORD_OK) {
Serial.println("Password is ok!");
} else if (state == UNLOCKED) {
Serial.println("Password and Keypad are ok, Safe is now unlocked!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment