Hey guys . in my previous post which i had no response i hard a challenge with my project where i used a both an RFID and a FINGERPRINT module to open and Electromagnetic lock. the idea is to be able to enroll(fingerprint), delete(fingerprint) and authenticate fingerprint and rfid card.
i ended up using switch case with with 4 cases.
without the codes for for authentication, enrollment and deletion, the void loop() function runs completely but when i add the authentication function to the loop it does not run. at the start of the int authenticate() function i added a message to be displayed in the LCD and it does but does not go beyond that. please here is the code bellow i really need help figuring this out as i am running out of time.
please go easy on me, i am only a beginner.
when i place a lcd message after the functions the message is displayed without execution of loop.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Fingerprint.h>
#include <MFRC522.h>
#include <Keypad.h>
#include <SoftwareSerial.h>
// Initialize the fingerprint module
SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
// Initialize the RFID module
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
// Initialize the LCD i2c
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Initialize the keypad
const byte ROWS = 4;
const byte COLS = 3;
char hexakeys[ROWS][COLS] =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte pinRowPins[ROWS] = {8, 7, 6, 5};
byte pinColPins[COLS] = {A0, A1, A2};
Keypad Mykeys = Keypad(makeKeymap(hexakeys), pinRowPins, pinColPins, ROWS, COLS);
// Constants
const int PIN_LENGTH = 5;
const int AUTH_OPTION = 1;
const int ENROLL_OPTION = 2;
const int DELETE_OPTION = 3;
// Variables
int menuOption = 0;
int subMenuOption = 0;
String pinInput = "";
int state;
int EMLock =A0;
int FPrint =A1;
void setup()
{
pinMode(EMLock, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
// Initialize the LCD i2c
lcd.init();lcd.begin(16, 2);
lcd.backlight();
//Initialize the RFID module
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init();
Serial.begin(9600);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Auth 2. Enroll, ");
lcd.setCursor(0, 1);
lcd.print("3. Delete ");
Serial.begin(9600); // Initiate a serial communication
while (!Serial); // For Yun/Leo/Micro/Zero/...
delay(100);
Serial.println("fingertest");
// set the data rate for the sensor serial port
finger.begin(57600);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1) { delay(1); }
}
finger.getTemplateCount();
Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
Serial.println("Waiting for valid finger...");
}
void loop()
{ // Check for keypad input
char getkey = Mykeys.getKey();
if (getkey)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(getkey);
switch (getkey)
{
case '1': //case for authentiication which does not execute
menuOption = AUTH_OPTION;
authenticate();
delay(2000);
resetMenu();
break;
case '2':
menuOption = ENROLL_OPTION;
showSubMenu();
break;
case '3':
menuOption = DELETE_OPTION;
showSubMenu();
break;
case '*':
resetMenu();
break;
default:
if (subMenuOption != 0)
{// Collect Pin
if (pinInput.length()< PIN_LENGTH)
{ lcd.setCursor(pinInput.length(), 1);
lcd.print('*'); delay(200);
pinInput += getkey;
}
//Check if PIN input
if (pinInput.length()==PIN_LENGTH)
{
processPINInput();
}
}
break;
}
}
}
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.fingerFastSearch();
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) {
Serial.println("Did not find a match");
lcd.clear();
lcd.setCursor(2,0);
lcd.println("INVALID BIO-ID ");
lcd.setCursor(2,1);
lcd.println("TRY AGAIN ");
state = HIGH;
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() //function which test for fingerprint
{
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!
lcd.clear();
digitalWrite(FPrint, HIGH);
lcd.setCursor(2,0);
lcd.println("VALID BIO-ID ");
lcd.setCursor(2,1);
lcd.println("SCAN RFID ");
state = HIGH;
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
void ScanRFIDCard() //function which checks the RFID card
{
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
//Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat( String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message: ");
content.toUpperCase();
if ((content.substring(1) == "A3 38 D7 0A") && (state==HIGH))
{
digitalWrite(EMLock, HIGH);
lcd.clear();
lcd.setCursor(1,0);
lcd.println("ACCESS GRANTED ");
lcd.setCursor(2,1);
lcd.println("WELCOME JINUI ");
delay(5000);
digitalWrite(EMLock, LOW);
digitalWrite(5, LOW);
lcd.clear();
lcd.setCursor(1,0);
lcd.println(" TIME OUT! ");
lcd.setCursor(2,1);
lcd.println("START OVER ");
state=LOW;
}
else if ((content.substring(1) == "A3 38 D7 0A") && (state==LOW))
{
digitalWrite(EMLock, LOW);
lcd.clear();
lcd.setCursor(1,0);
lcd.println("INVALID ORDER ");
lcd.setCursor(1,1);
lcd.println(" FINGER FIRST ");
}
else if ((content.substring(1) != "A3 38 D7 0A") && (state==LOW))
{
digitalWrite(EMLock, LOW);
lcd.clear();
lcd.setCursor(1,0);
lcd.println("INVALID ORDER ");
lcd.setCursor(1,1);
lcd.println(" FINGER FIRST ");
}
else if ((content.substring(1) != "A3 38 D7 0A") && (state==HIGH))
{
digitalWrite(EMLock, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.println("UNAUTHORIZED ID ");
lcd.setCursor(1,1);
lcd.println("ACCESS DENIED! ");
}
}
char waitForUserInput()
{
while (true)
{
char getkey = Mykeys.getKey();
if (getkey)
{
return getkey;
}
}
}
void authenticate() //the two functions are combined here before being moved to the case
{// Display appropriate messages on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("hope on this one");
getFingerprintIDez();
delay(50); //don't ned to run this at full speed.
ScanRFIDCard();
delay(2000);
resetMenu();
}
void enroll()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("enrolling");
lcd.setCursor(0, 1);
lcd.print("yk");
delay(2000);
resetMenu();
}
void deleteFingerprint()
{
digitalWrite(A2,HIGH);
delay(200);
digitalWrite(A2,LOW);
delay(2000);
resetMenu();
}
void showSubMenu()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("INPUT PIN: ");
subMenuOption = menuOption;
menuOption = 0;
pinInput = "";
}
void resetMenu()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Auth, 2. Enroll,");
lcd.setCursor(0, 1);
lcd.print("3. Delete ");
menuOption = 0;
subMenuOption = 0;
pinInput = "";
}
void processPINInput()
{ //Check PIN for respective options
if (subMenuOption == ENROLL_OPTION)
{
if (pinInput == "45456")
{
enroll();
}
else
{
lcd.clear();
lcd.print("ACCESS DENIED!");
delay(2000);
resetMenu();
}
}
else if (subMenuOption == DELETE_OPTION)
{
if (pinInput == "56564")
{
deleteFingerprint();
}
else
{
lcd.clear();
lcd.print("ACESS DENIED!");
delay(2000);
resetMenu();
}
}
}