Program code for solenoid lock with relay and buzzer alarm code

Good day everyone i've been trouble in this code and i need someone help right now in this code he use servo motor to open the door and now i want to use the solenoid lock with relay and i don't know what code should i use so that it would not be error again and also i want tot put buzzer to this project can anyone share their knowledge so that i can fix it already. i need your help thank u for your time and for the help

here's my code below
type or paste code here

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);  //sometimes the adress is not 0x3f. Change to 0x27 if it dosn't work.
//Fingerprint libraries
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
int getFingerprintIDez();
SoftwareSerial mySerial(10, 11);  // pin #2 is IN from sensor (GREEN wire)/ pin #3 is OUT from arduino (WHITE wire)
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
//Real time clock libraries
#include <DS3231.h>
DS3231  rtc(SDA, SCL);// Init the DS3231 using the hardware interface
//SD card read/write libraries
#include <SPI.h>
#include <SD.h>
File myFile;
//servo library
#include <Servo.h>
Servo miServo;


/////////////////////////////////////Input and outputs////////////////////////////////////////////////
int scan_pin = 13;      //Pin for the scan push button
int add_id_pin = 12;    //Pin for the add new ID push button
int close_door = 9;     //Pin to close the door button
int green_led = 8;      //Extra LEDs for open or close door labels
int red_led = 7;
int servo = 6;        //Pin for the Servo PWM signal

//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////Editable variables//////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
int main_user_ID = 1;                 //Change this value if you want a different main user
int door_open_degrees = 180;
int door_close_degrees = 0;
String file_name_to_save = "blackBox.txt";
//////////////////////////////////////////////////////////////////////////////////////////////////////

//Other used variables in the code. Do not change!
bool scanning = false;
int counter = 0;
int id_ad_counter = 0;
bool id_ad = false;
uint8_t num = 1;
bool id_selected = false;
uint8_t id;
bool first_read = false;
bool main_user = false;
bool add_new_id = false;
bool door_locked = true;



void setup() {
  Serial.begin(57600);        //Start serial communication for fingerprint TX RX data.
  rtc.begin();                //Start the real time clock (remember to set time before this code)
  SD.begin(53);               //Start SD card module with CS pin connected to D53 of the Arduino MEGA. The other pins are SPI pins

  /*Now we open the new created file, write the data, and close it back*/
  myFile = SD.open(file_name_to_save, FILE_WRITE);   //Create a new file on the SD card named before
  myFile.print("Door lock system started at ");
  myFile.print(rtc.getTimeStr()); myFile.print(" and day ");  myFile.print(rtc.getDateStr());
  myFile.println(" ");myFile.println(" ");
  myFile.close(); 
   
  lcd.init();                     //Init the LCD with i2c communication and print text
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("   Press SCAN   ");
  lcd.setCursor(0,1);
  lcd.print(" -door  locked- ");

  //Define pins as outputs or inputs
  pinMode(scan_pin,INPUT);  
  pinMode(add_id_pin,INPUT);  
  pinMode(close_door,INPUT);  
  miServo.attach(servo);
  miServo.write(door_close_degrees);  //Door close
  digitalWrite(red_led,HIGH);         //Red LED turned ON, shows door CLOSED
  digitalWrite(green_led,LOW);        //Green LED turned OFF
  finger.begin(57600);                // set the data rate for the sensor serial port
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////VOID LOOP////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////


void loop() {
/////////////////////////////////////////DOOR CLOSE BUTTON PRESSED///////////////////////////////////
  if(digitalRead(close_door))
  {
    door_locked = true;
    miServo.write(door_close_degrees); //Door close
    digitalWrite(red_led,HIGH);       //Red LED turned ON, shows door CLOSED
    digitalWrite(green_led,LOW);       //Green LED turned OFF
    lcd.setCursor(0,0);
    lcd.print("  Door closed!  ");
    lcd.setCursor(0,1);
    lcd.print("                "); 
    delay(2000);     
    lcd.setCursor(0,0);
    lcd.print("   Press SCAN   ");
    lcd.setCursor(0,1);
    lcd.print(" -door  locked- ");
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.println(" -- Door was closed by a user");
    myFile.close(); 
  }


////////////////////////////////Scan button pressed/////////////////////////////////////////////////
 if(digitalRead(scan_pin) && !id_ad)
 {
  myFile = SD.open(file_name_to_save, FILE_WRITE);
  myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
  myFile.println(" -- Attempt of openning door");
  myFile.close(); 
  scanning = true;
  lcd.setCursor(0,0);
  lcd.print("  Place finger  ");
  lcd.setCursor(0,1);
  lcd.print("SCANNING--------");
 }
  
 while(scanning && counter <= 60)
 {
  getFingerprintID();
  delay(100); 
  counter = counter + 1;
  if(counter == 10)
  {
    lcd.setCursor(0,0);
    lcd.print("  Place finger  ");
    lcd.setCursor(0,1);
    lcd.print("SCANNING  ------");
  }

  if(counter == 20)
  {
    lcd.setCursor(0,0);
    lcd.print("  Place finger  ");
    lcd.setCursor(0,1);
    lcd.print("SCANNING    ----");
  }

  if(counter == 40)
  {
    lcd.setCursor(0,0);
    lcd.print("  Place finger  ");
    lcd.setCursor(0,1);
    lcd.print("SCANNING      --");
  }

  if(counter == 50)
  {
    lcd.setCursor(0,0);
    lcd.print("  Place finger  ");
    lcd.setCursor(0,1);
    lcd.print("SCANNING        ");
  }
  if(counter == 59)
  {
    lcd.setCursor(0,0);
    lcd.print("    Timeout!    ");
    lcd.setCursor(0,1);
    lcd.print("   Try again!   ");
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.println(" -- Scanning timeout!");
    myFile.close(); 
    delay(2000);
    if(door_locked)
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print(" -door  locked- ");   
    }
    else
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print("  -door  open-  ");  
    }
   }
   
 }
 scanning = false;
 counter = 0;
///////////////////////////////END WITH SCANNING PART



//////////////////////////////////////////Add new button pressed///////////////////////////////////////////
if(digitalRead(add_id_pin) && !id_ad)
 {
  myFile = SD.open(file_name_to_save, FILE_WRITE);
  myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
  myFile.println(" -- Add new user attempt. Require identification!");
  myFile.close(); 

  add_new_id = true;

  lcd.setCursor(0,0);
  lcd.print(" Scan main user ");
  lcd.setCursor(0,1);
  lcd.print("  finger first! ");  
  
  while (id_ad_counter < 40 && !main_user)
  {
    getFingerprintID();
    delay(100);  
    id_ad_counter = id_ad_counter+1;
    if(!add_new_id)
    {
      id_ad_counter = 40;
    }
  }
  id_ad_counter = 0;
  add_new_id = false;

  
  if(main_user)
  {
    lcd.setCursor(0,0);
    lcd.print(" Add new ID# to ");
    lcd.setCursor(0,1);
    lcd.print("  the database  ");  
    delay(1500);
    print_num(num);  
    id_ad = true;
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.println(" -- Add new user permission granted");
    myFile.close(); 
  }
  else
  {
    lcd.setCursor(0,0);
    lcd.print("ERROR! Only main");
    lcd.setCursor(0,1);
    lcd.print("user can add IDs");  
    delay(1500); 
    if(door_locked)
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print(" -door  locked- ");   
    }
    else
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print("  -door  open-  ");  
    }
    id_ad = false;
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.println(" -- The user does not have permission to add new user");
    myFile.close(); 
  }
 }

if(digitalRead(scan_pin) && id_ad)
 {  
  
  id=num;
  while (!  getFingerprintEnroll() );
  id_ad = false;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("  New ID saved  ");
  lcd.setCursor(4,1);
  lcd.print("as ID #"); 
  lcd.setCursor(11,1);
  lcd.print(id);  
  delay(3000);
  if(door_locked)
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print(" -door  locked- ");   
    }
    else
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print("  -door  open-  ");  
    }
  add_new_id = false;
  main_user = false;
  id_ad = false;
 
  
 }

if(digitalRead(add_id_pin) && id_ad)
 {
  num = num + 1;
  if(num > 16)
  {
    num=1;
  }
  print_num(num);  
 }







}//end of void


//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////


/*This function will print the ID numbers when adding new ID*/
void print_num(uint8_t)
{
  if (num == 1)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(">1  2   3   4   ");  
    delay(500);
  }
  if (num == 2)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 1 >2   3   4   ");  
    delay(500);
  }
  if (num == 3)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 1  2  >3   4   ");  
    delay(500);
  }
  if (num == 4)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 1  2   3  >4   ");  
    delay(500);
  }
  if (num == 5)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(">5  6   7   8   ");  
    delay(500);
  }
  if (num == 6)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 5 >6   7   8   ");  
    delay(500);
  }
  if (num == 7)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 5  6  >7   8   ");   
    delay(500);
  }
  if (num == 8)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 5  6   7  >8   ");   
    delay(500);
  }
  if (num == 9)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(">9  10  11  12  ");  
    delay(500);
  }
  if (num == 10)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 9 >10  11  12  ");  
    delay(500);
  }
  if (num == 11)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 9  10 >11  12  ");  
    delay(500);
  }
  if (num == 12)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 9  10  11 >12  ");  
    delay(500);
  }
  if (num == 13)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(">13  14  15  16 ");  
    delay(500);
  }
  if (num == 14)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 13 >14  15  16 ");  
    delay(500);
  }
  if (num == 15)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 13  14 >15  16 ");  
    delay(500);
  }
  if (num == 16)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 13  14  15 >16 ");  
    delay(500);
  }
}









/*This function will read the fingerprint placed on the sensor*/
uint8_t getFingerprintID()
{
  uint8_t p = finger.getImage();
  switch (p)
  {
    case FINGERPRINT_OK:
    break;
    case FINGERPRINT_NOFINGER: return p;
    case FINGERPRINT_PACKETRECIEVEERR: return p;
    case FINGERPRINT_IMAGEFAIL: return p;
    default: return p; 
  }
// OK success!

  p = finger.image2Tz();
  switch (p) 
  {
    case FINGERPRINT_OK: break;    
    case FINGERPRINT_IMAGEMESS: return p;    
    case FINGERPRINT_PACKETRECIEVEERR: return p;  
    case FINGERPRINT_FEATUREFAIL: return p;  
    case FINGERPRINT_INVALIDIMAGE: return p;    
    default: return p;
  }
// OK converted!

p = finger.fingerFastSearch();

if (p == FINGERPRINT_OK)
{
  scanning = false;
  counter = 0;
  if(add_new_id)
  {
    if(finger.fingerID == main_user_ID)
    {
      main_user = true;
      id_ad = false;
    }
    else
    {
      add_new_id = false;
      main_user = false;
      id_ad = false;
    }
    
  }
  else
  {
  miServo.write(door_open_degrees); //degrees so door is open
  digitalWrite(red_led,LOW);       //Red LED turned OFF
  digitalWrite(green_led,HIGH);    //Green LED turned ON, shows door OPEN
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("   User match   ");
  
  lcd.setCursor(0,1);
  lcd.print(" ID: #");
  
  lcd.setCursor(6,1);
  lcd.print(finger.fingerID);

  lcd.setCursor(9,1);
  lcd.print("%: ");

  lcd.setCursor(12,1);
  lcd.print(finger.confidence);

  myFile = SD.open(file_name_to_save, FILE_WRITE);
  myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
  myFile.print(" -- User match for ID# "); myFile.print(finger.fingerID);
  myFile.print(" with confidence: ");   myFile.print(finger.confidence); myFile.println(" - door open");
  myFile.close(); 
  door_locked = false;
  delay(4000);
  lcd.setCursor(0,0);
  lcd.print("   Press SCAN   ");
  lcd.setCursor(0,1);
  lcd.print("  -door  open-  ");
  delay(50);
  }
}//end finger OK

else if(p == FINGERPRINT_NOTFOUND)
{
  scanning = false;
  id_ad = false;
  counter = 0;
  lcd.setCursor(0,0);
  lcd.print("    No match    ");
  lcd.setCursor(0,1);
  lcd.print("   Try again!   ");
  add_new_id = false;
  main_user = false;  

  myFile = SD.open(file_name_to_save, FILE_WRITE);
  myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
  myFile.print(" -- No match for any ID. Door status still the same"); 
  myFile.close(); 
  delay(2000);
  if(door_locked)
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print(" -door  locked- ");   
    }
    else
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print("  -door  open-  ");  
    }
  delay(2);
  return p;
}//end finger error
}// 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;
// found a match!
return finger.fingerID;
}









//////////////////////////////////////////










/*This function will add new ID to the database*/
uint8_t getFingerprintEnroll() {

  int p = -1;
  if(!first_read)
  {
  lcd.setCursor(0,0);
  lcd.print("Add new as ID#  ");
  lcd.setCursor(14,0);
  lcd.print(id);
  lcd.setCursor(0,1);
  lcd.print("  Place finger  ");  
  }
  
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      lcd.setCursor(0,0);
      lcd.print("  Image taken!  ");
      lcd.setCursor(0,1);
      lcd.print("                ");  
      delay(100);
      first_read = true;
      break;
    case FINGERPRINT_NOFINGER:
      lcd.setCursor(0,0);
      lcd.print("Add new as ID#  ");
      lcd.setCursor(14,0);
      lcd.print(id);
      lcd.setCursor(0,1);
      lcd.print("  Place finger  "); 
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      lcd.setCursor(0,0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      break;
    case FINGERPRINT_IMAGEFAIL:
      lcd.setCursor(0,0);
      lcd.print("     -Image     ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      break;
    default:
      lcd.setCursor(0,0);
      lcd.print("    -Unknown    ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      break;
    }
  }

  // OK success!

  p = finger.image2Tz(1);
  switch (p) {
    case FINGERPRINT_OK:
      lcd.setCursor(0,0);
      lcd.print("Image converted!");
      lcd.setCursor(0,1);
      lcd.print("                ");
      break;
    case FINGERPRINT_IMAGEMESS:
      lcd.setCursor(0,0);
      lcd.print("Image too messy!");
      lcd.setCursor(0,1);
      lcd.print("                ");
      delay(1000);
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      lcd.setCursor(0,0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
    case FINGERPRINT_FEATUREFAIL:
      lcd.setCursor(0,0);
      lcd.print(" No fingerprint ");
      lcd.setCursor(0,1);
      lcd.print("features found  ");
      delay(1000);
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      lcd.setCursor(0,0);
      lcd.print(" No fingerprint ");
      lcd.setCursor(0,1);
      lcd.print("features found  ");
      delay(1000);
      return p;
    default:
      lcd.setCursor(0,0);
      lcd.print("    -Unknown    ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
  }
  
  lcd.setCursor(0,0);
  lcd.print(" Remove finger! ");
  lcd.setCursor(0,1);
  lcd.print("                ");
  delay(2000);
  p = 0;
  while (p != FINGERPRINT_NOFINGER) {
    p = finger.getImage();
  }
  lcd.setCursor(0,1);
  lcd.print("ID# ");
  lcd.setCursor(4,1);
  lcd.print(id);
  p = -1;
  lcd.setCursor(0,0);
  lcd.print("Place again the ");
  lcd.setCursor(0,1);
  lcd.print("same finger     ");
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      lcd.setCursor(0,0);
      lcd.print("  Image taken!  ");
      lcd.setCursor(0,1);
      lcd.print("                "); 
      break;
    case FINGERPRINT_NOFINGER:
      lcd.setCursor(0,0);
      lcd.print("Place again the ");
      lcd.setCursor(0,1);
      lcd.print("same finger     ");
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      lcd.setCursor(0,0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      break;
    case FINGERPRINT_IMAGEFAIL:
      lcd.setCursor(0,0);
      lcd.print("     -Image     ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      break;
    default:
      lcd.setCursor(0,0);
      lcd.print("    -Unknown    ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      break;
    }
  }

  // OK success!

  p = finger.image2Tz(2);
  switch (p) {
    case FINGERPRINT_OK:
      lcd.setCursor(0,0);
      lcd.print("Image converted!");
      lcd.setCursor(0,1);
      lcd.print("                ");
      break;
    case FINGERPRINT_IMAGEMESS:
      lcd.setCursor(0,0);
      lcd.print("Image too messy!");
      lcd.setCursor(0,1);
      lcd.print("                ");
      delay(1000);
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      lcd.setCursor(0,0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
    case FINGERPRINT_FEATUREFAIL:
      lcd.setCursor(0,0);
      lcd.print(" No fingerprint ");
      lcd.setCursor(0,1);
      lcd.print("features found  ");
      delay(1000);
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      lcd.setCursor(0,0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
    default:
      lcd.setCursor(0,0);
      lcd.print("    -Unknown    ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
  }
  
  // OK converted!
 lcd.setCursor(0,0);
 lcd.print(" Creating model ");
 lcd.setCursor(0,1);
 lcd.print("for ID# ");
 lcd.setCursor(8,1);
 lcd.print(id);
  
  p = finger.createModel();
  if (p == FINGERPRINT_OK) {
    lcd.setCursor(0,0);
    lcd.print(" Print matched! ");
    lcd.setCursor(0,1);
    lcd.print("                ");
    delay(1000);
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    lcd.setCursor(0,0);
    lcd.print("  Comunication  ");
    lcd.setCursor(0,1);
    lcd.print("     ERROR!     ");
    delay(1000);
    return p;
  } else if (p == FINGERPRINT_ENROLLMISMATCH) {
    lcd.setCursor(0,0);
    lcd.print("Fingerprint did ");
    lcd.setCursor(0,1);
    lcd.print("not match       ");
    delay(1000);
    return p;
  } else {
    lcd.setCursor(0,0);
    lcd.print("    -Unknown    ");
    lcd.setCursor(0,1);
    lcd.print("     ERROR!     ");
    delay(1000);
    return p;
  }   
  
  lcd.setCursor(0,1);
  lcd.print("ID# ");
  lcd.setCursor(4,1);
  lcd.print(id);
  p = finger.storeModel(id);
  if (p == FINGERPRINT_OK) {
    lcd.setCursor(0,0);
    lcd.print("     Stored     ");
    lcd.setCursor(0,1);
    lcd.print("                ");
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.print(" -- New fingerprint stored for ID# "); myFile.println(id);
    myFile.close(); 
    delay(1000);
    first_read = false;
    id_ad = false;
    
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    lcd.setCursor(0,0);
    lcd.print("  Comunication  ");
    lcd.setCursor(0,1);
    lcd.print("     ERROR!     ");
    delay(1000);
    return p;
  } else if (p == FINGERPRINT_BADLOCATION) {
    lcd.setCursor(0,0);
    lcd.print("Could not store ");
    lcd.setCursor(0,1);
    lcd.print("in that location");
    delay(1000);
    return p;
  } else if (p == FINGERPRINT_FLASHERR) {
    lcd.setCursor(0,0);
    lcd.print("Error writing to");
    lcd.setCursor(0,1);
    lcd.print("flash           ");
    delay(1000);
    return p;
  } else {
    lcd.setCursor(0,0);
    lcd.print("    -Unknown    ");
    lcd.setCursor(0,1);
    lcd.print("     ERROR!     ");    
    delay(1000);
    return p;
  }   
}

Hello @markcleo - Also post a wire diagram. A photo of hand-drawn diagram is good. I can see you have two "SD.h" includes... remove one.

You should make a sketch for each of your devices.

  1. LCD
  2. Fingerprint
  3. Serial I/O
  4. RTC
  5. SD files
  6. Relay
  7. Servo
    (this will probably be inaccurate, as you are changing the code you posted... but it's a start)

... and then combine the sketches.

1 Like

i edit already sir okay sir thank u i would

i want to change the servo sir into relay with solenoid locks and also i want to put also buzzer alarm on it but i dont know to deal with the codes to use

Okay... you need to add to your drawing...
A servo with VCC, GND and one signal pin.
A RELAY, a SOLENOID, and a BUZZER with VCC x 3, GND x 3 and SIGNAL x 2 (the relay must use logic levels for the solenoid can be actuated using the relay).

Draw a diagram and post it here.

Adding to your seven steps above...
8. Relay
9. Solenoid
10. Buzzer

1 Like

heres my drawing sir

how can i change the code that i posted the correct code for that instead of servo it is now relay with solenoid lock and also the buzzer.

Your sketch is opening and closing the door with the servo miServo.write(door_open_degrees);.

If all you want to do with the relay is enable the relay to pass the power required to actuate the solenoid, connect the Arduino-compatible relay to the servo pin and send the relay pin a HIGH or LOW (depending on how you connect the relay).

Again. Test all your components on individual sketches. Then combine the components and sketches one at a time. Looks like you don't like my suggestion.

1 Like

i appreciate it sir and i like your suggestion thanks for it and also can i ask one more time i want also to change the opening and closing the door. i want to use the relay only no closing button i adjust it into opening the push buttons the process goes like this when the fingerprint detected it will open directly and by the help of relay it will close in specific time that i put. not the use of push buttons to close it but it will automatically close.

That is possible... if the fingerprint passes, then the relay is activated to make the solenoid unlock until a timer, probably using millis(), releases the relay which releases the solenoid which makes the door lock again. You could do that with your RTC if you do not want to use millis().

1 Like

what code should i put in the buzzer sir ?

You would want to choose if the buzzer means GOOD or the buzzer means BAD.

If the buzzer means GOOD, you would turn the buzzer on during the time the solenoid is energized, indicating to the person that they can pass through the door.

If the buzzer means BAD, you would turn the buzzer on if the fingerprint failed... but I think that would not be right.

Would you "uncheck" the "solved" box? This is only started, and not solved. There is much more to come.

1 Like

thanks a lot for your time and i appreciate of your help sir '

I am looking at your complete sketch. There are many adjustments to be made. Looking now.

1 Like

that was my sketch sir that i send on you my problem is about of arduino codes how to function the fingerprint, buzzer alarm and relay using those codes , that codes are correct but i want to change the servo motor into relay with solenoid lock and then add buzzer alarm on it

Yes, it is your code. About the buzzer... I think you should have the buzzer sound when the fingerprint is good, and signal the door/solenoid is open, and stop buzzing when the door/solenoid locks.

1 Like

thanks a lots sir first i just program first the fingerprint sensor and the buzzer so that when it comes to that sketch maybe it work ill just try for it

1 Like

To change the "servo" action to a "relay and solenoid", you must use a relay that the Arduino can signal to close/open, and then make the "miServo()" code be a digitalWrite() either ON or OFF (1/0, true/false), then use the servo pin for the relay to digitalWrite(door_locked, HIGH) or digitalWrite(door_locked, LOW).

As far as the rest of your code, I can see it was copy/pastde from various sketches, and correcting every line would be more than I am willing to do. I suggest you make individual sketches to test each device alone. and put that code in function modules. When all devices have been tested, combine them in a logical order.

I started making comments in your code and arrange it to work, until I saw how much it needs to be re-written. The body of the code is original... and the original was never going to compile.

/*
  1. LCD - https://components101.com/sites/default/files/component_datasheet/16x2%20LCD%20Datasheet.pdf
  2. Fingerprint - https://electropeak.com/learn/interfacing-r305-optical-fingerprint-module-with-arduino/
  3. Serial I/O - https://wiki-content.arduino.cc/en/Tutorial/LibraryExamples/SoftwareSerialExample
  4. RTC - https://datasheet.lcsc.com/lcsc/1810071015_Maxim-Integrated-DS3231SN-T-R_C9866.pdf
  5. SD (files) - https://reference.arduino.cc/reference/en/libraries/sd/
  6. Relay - http://www.songle-relay.com/admin/uppic/200711239462347274.pdf
  7. Servo - https://github.com/arduino-libraries/Servo
  8. Relay - https://microcontrollerslab.com/5v-single-channel-relay-module-pinout-working-interfacing-applications-datasheet/
  9. Solenoid - https://www.makerguides.com/control-a-solenoid-with-arduino/
  10. Buzzer - two wire, active buzzer (GND and SIG)
  11. Mega2560 - https://micro-pi.ru/wp-content/uploads/2018/11/%D0%9E%D0%BF%D0%B8%D1%81%D0%B0%D0%BD%D0%B8%D0%B5-%D0%BF%D0%B8%D0%BD%D0%BE%D0%B2-%D0%A0%D0%B0%D1%81%D0%BF%D0%B8%D0%BD%D0%BE%D0%B2%D0%BA%D0%B0-Arduino-Mega-2560.png
*/

//**************************************************
// Communication
//**************************************************
#include <Wire.h>
#include <SPI.h>
#include <SoftwareSerial.h> // MUST BE DEFINED BEFORE Adafruit_Fingerprint.h
SoftwareSerial mySerial(10, 11);  // pin #2 IN from sensor (GREEN wire) pin #3 OUT from arduino (WHITE wire)

//**************************************************
// Fingerprint
//**************************************************
#include <Adafruit_Fingerprint.h>
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial); // create Fingerprint instance
// int getFingerprintIDez();  // THIS SHOULD NOT BE HERE... IT IS A FUNCTION
// REFER TO THIS WEB SITE AND CODE:
// https://maker.pro/arduino/projects/how-to-make-a-fingerprint-based-attendance-system-with-arduino-and-r305

//**************************************************
// Real time clock
//**************************************************
#include <DS3231.h>
DS3231 rtc(SDA, SCL); // SOME DS3231.h LIBRARIES DO NOT USE (SDA, SCL) ARGUMENTS

//**************************************************
// SD card
//**************************************************
// see https://wokwi.com/projects/356546798628722689
#include <SD.h>
File myFile;

//**************************************************
// Servo
//**************************************************
#include <Servo.h>
Servo miServo;
int servo      =  6; // Servo PWM PIN

//**************************************************
// LCD
//**************************************************
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); //sometimes the adress is not 0x3f. Change to 0x27 if it dosn't work.

//**************************************************
// Button pins
//**************************************************
int scan_pin   = 13; // scan push button
int add_id_pin = 12; // add new ID push button
int close_door =  9; // close the door button

//**************************************************
// Serial pins
//**************************************************
int txSerial   = 11; // serial tx
int rxSerial   = 10; // serial rx

//**************************************************
// LED assignemts
//**************************************************
int red_led    =  7; // fingerprint fail
int green_led  =  8; // fingerprint good, open door

//**************************************************
// 
//**************************************************
int main_user_ID = 1;                 //Change this value if you want a different main user
int door_open_degrees = 180;
int door_close_degrees = 0;
String file_name_to_save = "blackBox.txt";

int counter = 0;
int id_ad_counter = 0;

uint8_t num = 1;
uint8_t id;

bool add_new_id = false;
bool id_ad = false;
bool id_selected = false;
bool first_read = false;
bool main_user = false;
bool door_locked = true;
bool scanning = false;

void setup() {
  Serial.begin(57600);        // Start serial communication for fingerprint TX RX data.
  rtc.begin();                // Start the real time clock (remember to set time before this code)
  SD.begin(53);               // Start SD card module with CS pin connected to D53 of the Arduino MEGA.

//**************************************************
// SD card - create file
//**************************************************
  myFile = SD.open(file_name_to_save, FILE_WRITE); // new file name
  myFile.print("Door lock system started at ");
  myFile.print(rtc.getTimeStr());
  myFile.print(" and day ");
  myFile.print(rtc.getDateStr());
  myFile.println(" ");
  myFile.close();

//**************************************************
// LCD
//**************************************************
  lcd.init();                     //Init the LCD with i2c communication and print text
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("   Press SCAN   ");
  lcd.setCursor(0, 1);
  lcd.print(" -door  locked- ");

//**************************************************
// Define pins as outputs or inputs
//**************************************************
  pinMode(scan_pin, INPUT);
  pinMode(add_id_pin, INPUT);
  pinMode(close_door, INPUT);

//**************************************************
// SERVO
//**************************************************
  miServo.attach(servo);
  miServo.write(door_close_degrees);  //Door close

//**************************************************
// LEDs
//**************************************************
  digitalWrite(red_led, HIGH);        //Red LED turned ON, shows door CLOSED
  digitalWrite(green_led, LOW);       //Green LED turned OFF

//**************************************************
// Fingerprint reader initialize
//**************************************************
  finger.begin(57600);                // set the data rate for the sensor serial port
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////VOID LOOP////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////


void loop() {
  /////////////////////////////////////////DOOR CLOSE BUTTON PRESSED///////////////////////////////////
  if (digitalRead(close_door))
  {
    door_locked = true;
    miServo.write(door_close_degrees); //Door close
    digitalWrite(red_led, HIGH);      //Red LED turned ON, shows door CLOSED
    digitalWrite(green_led, LOW);      //Green LED turned OFF
    lcd.setCursor(0, 0);
    lcd.print("  Door closed!  ");
    lcd.setCursor(0, 1);
    lcd.print("                ");
    delay(2000);
    lcd.setCursor(0, 0);
    lcd.print("   Press SCAN   ");
    lcd.setCursor(0, 1);
    lcd.print(" -door  locked- ");
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.println(" -- Door was closed by a user");
    myFile.close();
  }

  ////////////////////////////////Scan button pressed/////////////////////////////////////////////////
  if (digitalRead(scan_pin) && !id_ad)
  {
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.println(" -- Attempt of openning door");
    myFile.close();
    scanning = true;
    lcd.setCursor(0, 0);
    lcd.print("  Place finger  ");
    lcd.setCursor(0, 1);
    lcd.print("SCANNING--------");
  }

  while (scanning && counter <= 60)
  {
    getFingerprintID();
    delay(100);
    counter = counter + 1;
    if (counter == 10)
    {
      lcd.setCursor(0, 0);
      lcd.print("  Place finger  ");
      lcd.setCursor(0, 1);
      lcd.print("SCANNING  ------");
    }

    if (counter == 20)
    {
      lcd.setCursor(0, 0);
      lcd.print("  Place finger  ");
      lcd.setCursor(0, 1);
      lcd.print("SCANNING    ----");
    }

    if (counter == 40)
    {
      lcd.setCursor(0, 0);
      lcd.print("  Place finger  ");
      lcd.setCursor(0, 1);
      lcd.print("SCANNING      --");
    }

    if (counter == 50)
    {
      lcd.setCursor(0, 0);
      lcd.print("  Place finger  ");
      lcd.setCursor(0, 1);
      lcd.print("SCANNING        ");
    }

    if (counter == 59)
    {
      lcd.setCursor(0, 0);
      lcd.print("    Timeout!    ");
      lcd.setCursor(0, 1);
      lcd.print("   Try again!   ");
      myFile = SD.open(file_name_to_save, FILE_WRITE);
      myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
      myFile.println(" -- Scanning timeout!");
      myFile.close();
      delay(2000);

      if (door_locked)
      {
        lcd.setCursor(0, 0);
        lcd.print("   Press SCAN   ");
        lcd.setCursor(0, 1);
        lcd.print(" -door  locked- ");
      }
      else
      {
        lcd.setCursor(0, 0);
        lcd.print("   Press SCAN   ");
        lcd.setCursor(0, 1);
        lcd.print("  -door  open-  ");
      }
    }
  }
  scanning = false;
  counter = 0;
  ///////////////////////////////END WITH SCANNING PART



  //////////////////////////////////////////Add new button pressed///////////////////////////////////////////
  if (digitalRead(add_id_pin) && !id_ad)
  {
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.println(" -- Add new user attempt. Require identification!");
    myFile.close();

    add_new_id = true;

    lcd.setCursor(0, 0);
    lcd.print(" Scan main user ");
    lcd.setCursor(0, 1);
    lcd.print("  finger first! ");

    while (id_ad_counter < 40 && !main_user)
    {
      getFingerprintID();
      delay(100);
      id_ad_counter = id_ad_counter + 1;
      if (!add_new_id)
      {
        id_ad_counter = 40;
      }
    }
    id_ad_counter = 0;
    add_new_id = false;


    if (main_user)
    {
      lcd.setCursor(0, 0);
      lcd.print(" Add new ID# to ");
      lcd.setCursor(0, 1);
      lcd.print("  the database  ");
      delay(1500);
      print_num(num);
      id_ad = true;
      myFile = SD.open(file_name_to_save, FILE_WRITE);
      myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
      myFile.println(" -- Add new user permission granted");
      myFile.close();
    }
    else
    {
      lcd.setCursor(0, 0);
      lcd.print("ERROR! Only main");
      lcd.setCursor(0, 1);
      lcd.print("user can add IDs");
      delay(1500);
      if (door_locked)
      {
        lcd.setCursor(0, 0);
        lcd.print("   Press SCAN   ");
        lcd.setCursor(0, 1);
        lcd.print(" -door  locked- ");
      }
      else
      {
        lcd.setCursor(0, 0);
        lcd.print("   Press SCAN   ");
        lcd.setCursor(0, 1);
        lcd.print("  -door  open-  ");
      }
      id_ad = false;
      myFile = SD.open(file_name_to_save, FILE_WRITE);
      myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
      myFile.println(" -- The user does not have permission to add new user");
      myFile.close();
    }
  }

  if (digitalRead(scan_pin) && id_ad)
  {

    id = num;
    while (!  getFingerprintEnroll() );
    id_ad = false;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("  New ID saved  ");
    lcd.setCursor(4, 1);
    lcd.print("as ID #");
    lcd.setCursor(11, 1);
    lcd.print(id);
    delay(3000);
    if (door_locked)
    {
      lcd.setCursor(0, 0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0, 1);
      lcd.print(" -door  locked- ");
    }
    else
    {
      lcd.setCursor(0, 0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0, 1);
      lcd.print("  -door  open-  ");
    }
    add_new_id = false;
    main_user = false;
    id_ad = false;


  }

  if (digitalRead(add_id_pin) && id_ad)
  {
    num = num + 1;
    if (num > 16)
    {
      num = 1;
    }
    print_num(num);
  }







}//end of void


//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////


/*This function will print the ID numbers when adding new ID*/
void print_num(uint8_t)
{
  if (num == 1)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(">1  2   3   4   ");
    delay(500);
  }
  if (num == 2)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(" 1 >2   3   4   ");
    delay(500);
  }
  if (num == 3)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(" 1  2  >3   4   ");
    delay(500);
  }
  if (num == 4)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(" 1  2   3  >4   ");
    delay(500);
  }
  if (num == 5)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(">5  6   7   8   ");
    delay(500);
  }
  if (num == 6)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(" 5 >6   7   8   ");
    delay(500);
  }
  if (num == 7)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(" 5  6  >7   8   ");
    delay(500);
  }
  if (num == 8)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(" 5  6   7  >8   ");
    delay(500);
  }
  if (num == 9)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(">9  10  11  12  ");
    delay(500);
  }
  if (num == 10)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(" 9 >10  11  12  ");
    delay(500);
  }
  if (num == 11)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(" 9  10 >11  12  ");
    delay(500);
  }
  if (num == 12)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(" 9  10  11 >12  ");
    delay(500);
  }
  if (num == 13)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(">13  14  15  16 ");
    delay(500);
  }
  if (num == 14)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(" 13 >14  15  16 ");
    delay(500);
  }
  if (num == 15)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(" 13  14 >15  16 ");
    delay(500);
  }
  if (num == 16)
  {
    lcd.setCursor(0, 0);
    lcd.print("Select ID number");
    lcd.setCursor(0, 1);
    lcd.print(" 13  14  15 >16 ");
    delay(500);
  }
}









/*This function will read the fingerprint placed on the sensor*/
uint8_t getFingerprintID()
{
  uint8_t p = finger.getImage();
  switch (p)
  {
    case FINGERPRINT_OK:
      break;
    case FINGERPRINT_NOFINGER: return p;
    case FINGERPRINT_PACKETRECIEVEERR: return p;
    case FINGERPRINT_IMAGEFAIL: return p;
    default: return p;
  }
  // OK success!

  p = finger.image2Tz();
  switch (p)
  {
    case FINGERPRINT_OK: break;
    case FINGERPRINT_IMAGEMESS: return p;
    case FINGERPRINT_PACKETRECIEVEERR: return p;
    case FINGERPRINT_FEATUREFAIL: return p;
    case FINGERPRINT_INVALIDIMAGE: return p;
    default: return p;
  }
  // OK converted!

  p = finger.fingerFastSearch();

  if (p == FINGERPRINT_OK)
  {
    scanning = false;
    counter = 0;
    if (add_new_id)
    {
      if (finger.fingerID == main_user_ID)
      {
        main_user = true;
        id_ad = false;
      }
      else
      {
        add_new_id = false;
        main_user = false;
        id_ad = false;
      }

    }
    else
    {
      miServo.write(door_open_degrees); //degrees so door is open
      digitalWrite(red_led, LOW);      //Red LED turned OFF
      digitalWrite(green_led, HIGH);   //Green LED turned ON, shows door OPEN

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("   User match   ");

      lcd.setCursor(0, 1);
      lcd.print(" ID: #");

      lcd.setCursor(6, 1);
      lcd.print(finger.fingerID);

      lcd.setCursor(9, 1);
      lcd.print("%: ");

      lcd.setCursor(12, 1);
      lcd.print(finger.confidence);

      myFile = SD.open(file_name_to_save, FILE_WRITE);
      myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
      myFile.print(" -- User match for ID# "); myFile.print(finger.fingerID);
      myFile.print(" with confidence: ");   myFile.print(finger.confidence); myFile.println(" - door open");
      myFile.close();
      door_locked = false;
      delay(4000);
      lcd.setCursor(0, 0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0, 1);
      lcd.print("  -door  open-  ");
      delay(50);
    }
  }//end finger OK

  else if (p == FINGERPRINT_NOTFOUND)
  {
    scanning = false;
    id_ad = false;
    counter = 0;
    lcd.setCursor(0, 0);
    lcd.print("    No match    ");
    lcd.setCursor(0, 1);
    lcd.print("   Try again!   ");
    add_new_id = false;
    main_user = false;

    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.print(" -- No match for any ID. Door status still the same");
    myFile.close();
    delay(2000);
    if (door_locked)
    {
      lcd.setCursor(0, 0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0, 1);
      lcd.print(" -door  locked- ");
    }
    else
    {
      lcd.setCursor(0, 0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0, 1);
      lcd.print("  -door  open-  ");
    }
    delay(2);
    return p;
  }//end finger error
}// 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;
  // found a match!
  return finger.fingerID;
}

//////////////////////////////////////////

/*This function will add new ID to the database*/
uint8_t getFingerprintEnroll() {

  int p = -1;
  if (!first_read)
  {
    lcd.setCursor(0, 0);
    lcd.print("Add new as ID#  ");
    lcd.setCursor(14, 0);
    lcd.print(id);
    lcd.setCursor(0, 1);
    lcd.print("  Place finger  ");
  }

  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
      case FINGERPRINT_OK:
        lcd.setCursor(0, 0);
        lcd.print("  Image taken!  ");
        lcd.setCursor(0, 1);
        lcd.print("                ");
        delay(100);
        first_read = true;
        break;
      case FINGERPRINT_NOFINGER:
        lcd.setCursor(0, 0);
        lcd.print("Add new as ID#  ");
        lcd.setCursor(14, 0);
        lcd.print(id);
        lcd.setCursor(0, 1);
        lcd.print("  Place finger  ");
        break;
      case FINGERPRINT_PACKETRECIEVEERR:
        lcd.setCursor(0, 0);
        lcd.print("  Comunication  ");
        lcd.setCursor(0, 1);
        lcd.print("     ERROR!     ");
        delay(1000);
        break;
      case FINGERPRINT_IMAGEFAIL:
        lcd.setCursor(0, 0);
        lcd.print("     -Image     ");
        lcd.setCursor(0, 1);
        lcd.print("     ERROR!     ");
        delay(1000);
        break;
      default:
        lcd.setCursor(0, 0);
        lcd.print("    -Unknown    ");
        lcd.setCursor(0, 1);
        lcd.print("     ERROR!     ");
        delay(1000);
        break;
    }
  }

  // OK success!

  p = finger.image2Tz(1);
  switch (p) {
    case FINGERPRINT_OK:
      lcd.setCursor(0, 0);
      lcd.print("Image converted!");
      lcd.setCursor(0, 1);
      lcd.print("                ");
      break;
    case FINGERPRINT_IMAGEMESS:
      lcd.setCursor(0, 0);
      lcd.print("Image too messy!");
      lcd.setCursor(0, 1);
      lcd.print("                ");
      delay(1000);
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      lcd.setCursor(0, 0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0, 1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
    case FINGERPRINT_FEATUREFAIL:
      lcd.setCursor(0, 0);
      lcd.print(" No fingerprint ");
      lcd.setCursor(0, 1);
      lcd.print("features found  ");
      delay(1000);
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      lcd.setCursor(0, 0);
      lcd.print(" No fingerprint ");
      lcd.setCursor(0, 1);
      lcd.print("features found  ");
      delay(1000);
      return p;
    default:
      lcd.setCursor(0, 0);
      lcd.print("    -Unknown    ");
      lcd.setCursor(0, 1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
  }

  lcd.setCursor(0, 0);
  lcd.print(" Remove finger! ");
  lcd.setCursor(0, 1);
  lcd.print("                ");
  delay(2000);
  p = 0;
  while (p != FINGERPRINT_NOFINGER) {
    p = finger.getImage();
  }
  lcd.setCursor(0, 1);
  lcd.print("ID# ");
  lcd.setCursor(4, 1);
  lcd.print(id);
  p = -1;
  lcd.setCursor(0, 0);
  lcd.print("Place again the ");
  lcd.setCursor(0, 1);
  lcd.print("same finger     ");
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
      case FINGERPRINT_OK:
        lcd.setCursor(0, 0);
        lcd.print("  Image taken!  ");
        lcd.setCursor(0, 1);
        lcd.print("                ");
        break;
      case FINGERPRINT_NOFINGER:
        lcd.setCursor(0, 0);
        lcd.print("Place again the ");
        lcd.setCursor(0, 1);
        lcd.print("same finger     ");
        break;
      case FINGERPRINT_PACKETRECIEVEERR:
        lcd.setCursor(0, 0);
        lcd.print("  Comunication  ");
        lcd.setCursor(0, 1);
        lcd.print("     ERROR!     ");
        delay(1000);
        break;
      case FINGERPRINT_IMAGEFAIL:
        lcd.setCursor(0, 0);
        lcd.print("     -Image     ");
        lcd.setCursor(0, 1);
        lcd.print("     ERROR!     ");
        delay(1000);
        break;
      default:
        lcd.setCursor(0, 0);
        lcd.print("    -Unknown    ");
        lcd.setCursor(0, 1);
        lcd.print("     ERROR!     ");
        delay(1000);
        break;
    }
  }

  // OK success!

  p = finger.image2Tz(2);
  switch (p) {
    case FINGERPRINT_OK:
      lcd.setCursor(0, 0);
      lcd.print("Image converted!");
      lcd.setCursor(0, 1);
      lcd.print("                ");
      break;
    case FINGERPRINT_IMAGEMESS:
      lcd.setCursor(0, 0);
      lcd.print("Image too messy!");
      lcd.setCursor(0, 1);
      lcd.print("                ");
      delay(1000);
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      lcd.setCursor(0, 0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0, 1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
    case FINGERPRINT_FEATUREFAIL:
      lcd.setCursor(0, 0);
      lcd.print(" No fingerprint ");
      lcd.setCursor(0, 1);
      lcd.print("features found  ");
      delay(1000);
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      lcd.setCursor(0, 0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0, 1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
    default:
      lcd.setCursor(0, 0);
      lcd.print("    -Unknown    ");
      lcd.setCursor(0, 1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
  }

  // OK converted!
  lcd.setCursor(0, 0);
  lcd.print(" Creating model ");
  lcd.setCursor(0, 1);
  lcd.print("for ID# ");
  lcd.setCursor(8, 1);
  lcd.print(id);

  p = finger.createModel();
  if (p == FINGERPRINT_OK) {
    lcd.setCursor(0, 0);
    lcd.print(" Print matched! ");
    lcd.setCursor(0, 1);
    lcd.print("                ");
    delay(1000);
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    lcd.setCursor(0, 0);
    lcd.print("  Comunication  ");
    lcd.setCursor(0, 1);
    lcd.print("     ERROR!     ");
    delay(1000);
    return p;
  } else if (p == FINGERPRINT_ENROLLMISMATCH) {
    lcd.setCursor(0, 0);
    lcd.print("Fingerprint did ");
    lcd.setCursor(0, 1);
    lcd.print("not match       ");
    delay(1000);
    return p;
  } else {
    lcd.setCursor(0, 0);
    lcd.print("    -Unknown    ");
    lcd.setCursor(0, 1);
    lcd.print("     ERROR!     ");
    delay(1000);
    return p;
  }

  lcd.setCursor(0, 1);
  lcd.print("ID# ");
  lcd.setCursor(4, 1);
  lcd.print(id);
  p = finger.storeModel(id);
  if (p == FINGERPRINT_OK) {
    lcd.setCursor(0, 0);
    lcd.print("     Stored     ");
    lcd.setCursor(0, 1);
    lcd.print("                ");
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.print(" -- New fingerprint stored for ID# "); myFile.println(id);
    myFile.close();
    delay(1000);
    first_read = false;
    id_ad = false;

  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    lcd.setCursor(0, 0);
    lcd.print("  Comunication  ");
    lcd.setCursor(0, 1);
    lcd.print("     ERROR!     ");
    delay(1000);
    return p;
  } else if (p == FINGERPRINT_BADLOCATION) {
    lcd.setCursor(0, 0);
    lcd.print("Could not store ");
    lcd.setCursor(0, 1);
    lcd.print("in that location");
    delay(1000);
    return p;
  } else if (p == FINGERPRINT_FLASHERR) {
    lcd.setCursor(0, 0);
    lcd.print("Error writing to");
    lcd.setCursor(0, 1);
    lcd.print("flash           ");
    delay(1000);
    return p;
  } else {
    lcd.setCursor(0, 0);
    lcd.print("    -Unknown    ");
    lcd.setCursor(0, 1);
    lcd.print("     ERROR!     ");
    delay(1000);
    return p;
  }
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.