Skip to content

Instantly share code, notes, and snippets.

@battletag1
Last active August 14, 2020 20:28
Show Gist options
  • Save battletag1/ba2e89e7556601ada62bc84755832504 to your computer and use it in GitHub Desktop.
Save battletag1/ba2e89e7556601ada62bc84755832504 to your computer and use it in GitHub Desktop.
// if you are a bluestamp student wanting to use my code for your own project, I challenge you to not do that and make your own code instead. If you don't you are a big dweeb for eternity.
#include <Password.h>
#include <Key.h>
#include <Keypad.h>
#include <Adafruit_Fingerprint.h>
#include <Servo.h>
#define fingerLED 11 // the green LED for fingerprint sensor
#define codeLED 12 // the white LED for passcode
#define servoLED A0 // the blue LED to confirm both have been "verified"
Servo lock; // the name of the servo
boolean fingerprint = false; // the boolean statement for the fingerprint sensor
boolean passcode = false; // the boolean statement for the passcode matrix
SoftwareSerial mySerial(10, 13); // the pins for the fingerprint sensor are 10 and 13
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
String newPasswordString; //hold the new password
char newPassword[4]; //charater string of newPasswordString
Password password = Password( "6969" ); // can edit this password
byte maxPasswordLength = 6;
byte currentPasswordLength = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
//Define the keymap
char keys[ROWS][COLS] = {
{'D', '#', '0', '*'},
{'C', '9', '8', '7'},
{'B', '6', '5', '4'},
{'A', '3', '2', '1'}
};
//// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to row pinouts
// Connect keypad COL0, COL1, COL2 and COL3 to these Arduino pins.
byte colPins[COLS] = {2, 3, 4, 5}; //connect to column pinouts
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{ Serial.begin(9600);
while (!Serial); // For Yun/Leo/Micro/Zero/...
delay(100);
Serial.println("\n\nAdafruit finger detect test");
lock.attach(A1); // the servo pin is analog 1
lock.write(88); // set the servo lock to 0 degrees
// set the data rate for the sensor serial port
finger.begin(57600);
delay(5);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
// Serial.println("Did not find fingerprint sensor :(");
Serial.println("Did not find fingerprint sensor");
while (1) {
delay(1);
}
}
digitalWrite (codeLED, LOW);
finger.getTemplateCount();
if (finger.templateCount == 0) {
Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example.");
}
else {
Serial.println("Waiting for valid finger...");
Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
}
pinMode (fingerLED, OUTPUT);
pinMode (codeLED, OUTPUT);
pinMode (servoLED, OUTPUT);
}
void loop()
{
getFingerprintIDez();
delay(50); //don't ned to run this at full speed.
char key = keypad.getKey();
if (key != NO_KEY) {
delay(60);
switch (key) {
case 'A': break;
case 'B': break;
case 'C': break;
// case 'D': changePassword(); break; // change the password
case '#': checkPassword(); break; // check the password
case '*': resetPassword(); break; // reset the password
default: processNumberKey(key);
}
}
}
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;
}
// 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;
if (p == FINGERPRINT_OK) {
digitalWrite(fingerLED, HIGH); // set the finger LED on
delay (1000); // wait for a second
fingerprint = !fingerprint; // turn boolean fingerprint to TRUE
delay(1000); // wait for a second
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
else {
Serial.print ( "Did not find anything" );
// digitalWrite(fingerLED, HIGH); // set the finger LED on to flash
delay(1000); // wait for a second
// digitalWrite(fingerLED, LOW); // end the finger LED flash
}
}
void processNumberKey(char key) {
Serial.print(key);
currentPasswordLength++;
password.append(key);
if (currentPasswordLength == maxPasswordLength) {
checkPassword();
}
}
void checkPassword() {
if (password.evaluate()) {
Serial.println(" haha nice ");
passcode = !passcode; // turn the passcode boolean to true
digitalWrite (codeLED, HIGH); // turn the code LED on
delay (1000); // wait for a second
if (fingerprint == true && passcode == true) {
delay(1000);
digitalWrite (servoLED, HIGH); // turns the servo LED on
delay(3000); // wait 3 seconds
lock.write (230); // turn the servo "x" amount of degrees
digitalWrite (fingerLED, LOW); // turn off finger LED
delay(2000); // wait 2 seconds
digitalWrite (codeLED, LOW); // turn off code LED
delay(1000); // wait a second
digitalWrite (servoLED, LOW); // turn off servo LED
}
else {
Serial.println(" access denied ");
digitalWrite (codeLED, HIGH); // turn the code LED on to flash
digitalWrite (fingerLED, HIGH); // turn the finger LED on to flash
delay (1000); // wait for a second
digitalWrite(codeLED, LOW); // end the code LED flash
digitalWrite(fingerLED, LOW); // end the finger LED flash
// }
// else if {
// Serial.println(" Wrong password nerd ");
// digitalWrite (codeLED, HIGH); // turn the code LED on to flash
// delay (1000); // wait for a second
// digitalWrite (codeLED, LOW); // end the code LED flash
}
}
}
void resetPassword() {
password.reset();
digitalWrite (codeLED, LOW);
digitalWrite (fingerLED, LOW);
digitalWrite (servoLED, LOW);
lock.write (88);
boolean fingerprint = false;
boolean passcode = false;
currentPasswordLength = 0;
}
void changePassword() {
newPasswordString = "123";
newPasswordString.toCharArray(newPassword, newPasswordString.length() + 1); //convert string to char array
password.set(newPassword);
resetPassword();
Serial.print("Password changed to ");
Serial.println(newPasswordString);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment