Fingerprint PC Login not working

Hello everybody,
I am making a project with a fingerprint sensor to unlock my PC by printing my password once the sensor recognized the correct fingerprint ID #
Below is the code I have at the moment, the only issue is that when the fingerprint sensor recognized the ID # (which is 0 in my case) it doesn’t execute the Keyboard.print.
If anyone could help, much appreciated!

#include <Adafruit_Fingerprint.h>
#include <Keyboard.h>

#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)
SoftwareSerial mySerial(10, 16);
char Escape;


#else
#define mySerial Serial1
#endif


Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()
{
  Serial.begin(9600);
  while (!Serial);
  delay(100);
  Serial.println("Finding fingerprint sensor...");
  
  finger.begin(57600);
  delay(5);
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1) { delay(1); }
  }

  Serial.println(F("Reading sensor parameters"));
  finger.getParameters();
  Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX);
  Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX);
  Serial.print(F("Capacity: ")); Serial.println(finger.capacity);
  Serial.print(F("Security level: ")); Serial.println(finger.security_level);
  Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX);
  Serial.print(F("Packet len: ")); Serial.println(finger.packet_len);
  Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate);

  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");
  }
}

void loop()  // run repeatedly
{
  getFingerprintID();
  delay(50);          
}

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;
  }


  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;
  }

  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) {
    Serial.println("Did not find a match");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }

p = finger.fingerSearch();
  if (p == FINGERPRINT_OK) {
}
    
  // Finger Match
  Serial.print("Found ID #"); Serial.print(finger.fingerID);
  Serial.print(" with confidence of "); Serial.println(finger.confidence);
  return finger.fingerID;
  
  //once ID #0 is found, print password and release keys
  if(finger.fingerID == 0); 
    Keyboard.print("PASSWORD");
    Keyboard.press(0xB0); 
    Keyboard.releaseAll();
}

// 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;
  
}

The code after that ‘return’ statement will never be executed. You should move the ‘return’ statement to the end of the function.

And your ‘if’ statement only controls the ‘;’. You probably meant:

  //once ID #0 is found, print password and release keys
  if(finger.fingerID == 0) {
    Keyboard.print("PASSWORD");
    Keyboard.press(0xB0); 
    Keyboard.releaseAll();
  }

  return finger.fingerID;;
}

What that does is… nothing. Perhaps you should change that to:

  p = finger.fingerSearch();
  if (p != FINGERPRINT_OK) {
    return p;
}

Hello, thank you for the reply and helpful information. I’m a bit confused as to what you mean with moving moving the return statement to the end of the function. If you could explain that a bit I would appreciate it, thank you again!

The ‘return’ statement ends the function and return control to the place where the function was called. The lines you added after the ‘return’ statement won’t get executed because the function has ended.

Okay, gotcha. So you meant to use this instead?:

  //once ID #0 is found, print password and release keys
  if(finger.fingerID == 0) {
    Keyboard.print("PASSWORD");
    Keyboard.press(0xB0); 
    Keyboard.releaseAll();
  }

  return finger.fingerID;;
}

Yes.

I just tried that including the other suggestions you mentioned and I’m still getting the same result. It isn’t printing the Keyboard.print ("PASSWORD");

What it is saying on Serial Monitor? You can’t expect it to send the password if it doesn’t display the message:

Found ID #0 with confidence of ___

What does your ‘getFingerprintID’ function look like after fixing the various mistakes?

The following is what is showing up.
image
The function looks like this:

   p = finger.fingerSearch();
  if (p != FINGERPRINT_OK) {
    return p;
}

I just changed the finger.fingerSearch() to what it was at previously and the serial monitor shows this:
image