If else statement???

Not sure if I should have posted in the projects section, but admins please feel free to move this thread.

I’m building a door access system using the most popular sensor sold on Adafruit. I got everything working nicely except for one thing. A certain portion of the script I cannot get working correctly. When a valid fingerprint is detected I am able to print “Access Granted…” to the LCD however I can’t seem to get it working when a print isn’t detected. The sketch already had output writing to the Serial it seems for rejected prints, and I put my code right under the write line but it still isn’t working. Could someone tell me what I’m doing wrong here? Or if its missing completely, could someone explain to me what this part is for and what I need to add to accomplish what I’m trying to do?

I think I should be modifying the code below but could be wrong:

   // 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 :(");
        [u][b]lcd.print("Access Denied")[/b][/u]
        while (1);
      }
      Serial.println("Waiting for valid finger...");

Full sketch:

 #define relay 6
    #include <SPI.h>
    #include <Adafruit_Fingerprint.h>
    #include <SoftwareSerial.h>
    #include <LiquidCrystal.h>
    
    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

   
    int getFingerprintIDez();

    // pin #2 is IN from sensor (GREEN wire)
    // pin #3 is OUT from arduino  (WHITE wire)
    SoftwareSerial mySerial(9, 10);
     
     
    Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
     
    void setup()  
    {
      lcd.begin(16,2); // setup how many columns and rows lcd has
      Serial.begin(9600);
      Serial.println("fingertest");
      pinMode(relay, OUTPUT);
      digitalWrite(relay, HIGH);
     
      // 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);
      }
      Serial.println("Waiting for valid finger...");
    }
     
    void loop()                     // run over and over again
    {
      getFingerprintIDez();
      delay(50);            //don't ned to run this at full speed.
      lcd.setCursor(0, 1); // set cursor to line 0,1
      lcd.print("Awaiting Scan.."); // prints a message to the lcds screen

      }
     
    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");
        return p;
      } else {
        Serial.println("Unknown error");
        return p;
      }  
     
    }
 
      // 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!
      Serial.print("Found ID #"); Serial.print(finger.fingerID);
      Serial.print(" with confidence of "); Serial.println(finger.confidence);
      digitalWrite(relay, LOW);
      lcd.clear();
      lcd.print("Access Granted!"); // prints a message to the lcds screen
      delay(3000);
      lcd.clear();
      digitalWrite(relay, HIGH);
      return finger.fingerID;
    }

Thanks in advance

I reviewed the code again and I think it could possibly be this portion, just not sure what I have to add to it:

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

anthony_c:
I reviewed the code again and I think it could possibly be this portion, just not sure what I have to add to it:

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

and
....
    your process
...

return p;
}

I think it is a poor coding habit, laziness??, to use switch default coded as "unknown / invalid error" if it is actually good value.
( I do it too!)

Addendum
So how do you get to your process if !FINGERPRINT_OK , hence something else (?ID?) ALWAYS returns ?

So, what happens when a print ins't detected, eg, what do you mean by not working?

One thing i see is that you tell the LCD to print "Access Granted" but i can't seem to find where you tell the LCD to print when access isnt' granted, I only see see Serial.print and not lcd.print...

Why have you a big long function called getFingerprintID() that does not seem to be used at all. If it is not needed delete it an post the shorter and less confusing code.

In loop()

    void loop()  
    {
      getFingerprintIDez();

you are not making use of the value returned by getFingerprintIDez() as a basis for updating the LCD when there is no match.

...R

Thanks for the replies. This is actually an Adafruit sketch which I am using/modifying (or at least trying to). I actually had inserted a line of code where I thought it would work but just posted the original code since it didn’t work for some reason:

    // 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;
      }[b] else if (p == FINGERPRINT_NOTFOUND) {
        Serial.println("Did not find a match"); <-- not printing to serial when match not found
       lcd.print("Access Denied!");<-- not printing to lcd when match not found
        return p;
      } else {
        Serial.println("Unknown error");
        return p;
      }  
     
    }

It doesn’t even print to serial when a mount isn’t found so something is wrong, I just can’t put my finger on it! (no pun intended)…
Ive tried puting the lcdwrite code in different places but just can’t seem to find the right spot.

What do you see if you print p after

p = finger.fingerFastSearch();

anthony_c:
Thanks for the replies.

Did you edit your program to remove the irrelevant function? And then post the tidied up version so we all know exactly what we are dealing with?

...R

Can you answer these ? Just yes / no.
I don't care where the original code came from and there is no need of all of your code at this point.
I was under the impression we are trying to solve ONE logical / coding issue and don't need to know WHAT it does at this point.

  1. You have a function returning just success or error codes?

  2. you have a function returning just "ID" - whatever is that.

  3. You have a function returning BOTH?

  4. Do you process just success / error codes?

  5. Do you use the "ID" for further processing?

IMHO your code would be more logical / compact if you stayed with "switch" since you are essentially checking only one variable type in one shot.

BTW that is ONE of the major difference between using switch and if - if we go back to the OP Ha Ha

Vaclav:
Can you answer these ? Just yes / no.
I don't care where the original code came from and there is no need of all of your code at this point.
I was under the impression we are trying to solve ONE logical / coding issue and don't need to know WHAT it does at this point.

  1. You have a function returning just success or error codes?

  2. you have a function returning just "ID" - whatever is that.

  3. You have a function returning BOTH?

  4. Do you process just success / error codes?

  5. Do you use the "ID" for further processing?

IMHO your code would be more logical / compact if you stayed with "switch" since you are essentially checking only one variable type in one shot.

BTW that is ONE of the major difference between using switch and if - if we go back to the OP Ha Ha

  1. You have a function returning just success or error codes?
    I am not sure, I'm not an experienced programmer at all.. just learning.

  2. you have a function returning just "ID" - whatever is that.
    The only edit I made to this script was to print "Access Granted" to LCD

  3. You have a function returning BOTH?
    Again, this is coding from Adafruit.. not added by me. Simply was trying to take their script and modify to my needs.

I do plan to build on this script more extensively but as of right now I am just simply trying to write "Access Granted" when successful and "Access Denied" when not.

Once I get those working, I plan on having it light a Green LED for success, and Red led for denied.

Sorry I can't answer any of the questions pertaining to the existing code. I'm just not experienced enough.. but I am trying to learn.

Robin2:
Why have you a big long function called getFingerprintID() that does not seem to be used at all. If it is not needed delete it an post the shorter and less confusing code.

In loop()

    void loop()  

{
     getFingerprintIDez();




you are not making use of the value returned by getFingerprintIDez() as a basis for updating the LCD when there is no match.

...R

This code was written by Adafruit. I’m surprised to know that their code is a mess. Heh

Although what I posted has been virtually unchanged. Here's a link tot he Github:

anthony_c:
Although what I posted has been virtually unchanged.

I have been hoping you would change it to remove irrelevant stuff and make it easier for you and us to understand.

...R

Sorry I can't answer any of the questions pertaining to the existing code. I'm just not experienced enough.. but I am trying to learn.

You should not have to apologize, unfortunately for some here the format is more important than the questions....

I would go back to Github and just copy their sample code fingerprint.ino.

Here is the part you started asking about:
Pay attention to functions return values and WRITE you OWN comments to clarify stuff.
For example getImage obviously returns success / error codes and NOT an image.
( That is why I asked my questions).

FYI - it is pretty common to write code ( and publish it!) without much comments etc., but some of the clues are written in Serial functions. Use source code and not just "cut and paste" stuff.

Good luck.

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

anthony_c:
Sorry I can’t answer any of the questions pertaining to the existing code. I’m just not experienced enough… but I am trying to learn.

Apologies. I did not see this until @Vaclav highlighted it.

This incorporates the changes I suggested, but I am not able to test it.

 #define relay 6
#include <SPI.h>
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

 
int getFingerprintIDez();

// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (WHITE wire)
SoftwareSerial mySerial(9, 10);
 
 
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
 
void setup()  
{
	lcd.begin(16,2); // setup how many columns and rows lcd has
	Serial.begin(9600);
	Serial.println("fingertest");
	pinMode(relay, OUTPUT);
	digitalWrite(relay, HIGH);
 
	// 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);
	}
	Serial.println("Waiting for valid finger...");
}
 
void loop()                     // run over and over again
{
	int retVal = getFingerprintIDez();
	delay(50);            //don't ned to run this at full speed.
	if (retVal == -1) {
		lcd.setCursor(0, 1); // set cursor to line 0,1
		lcd.print("PROBLEM"); // prints a message to the lcds screen
	}
	lcd.setCursor(0, 1); // set cursor to line 0,1
	lcd.print("Awaiting Scan.."); // prints a message to the lcds screen

}
 
 
	// 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!
	Serial.print("Found ID #"); Serial.print(finger.fingerID);
	Serial.print(" with confidence of "); Serial.println(finger.confidence);
	digitalWrite(relay, LOW);
	lcd.clear();
	lcd.print("Access Granted!"); // prints a message to the lcds screen
	delay(3000);
	lcd.clear();
	digitalWrite(relay, HIGH);
	return finger.fingerID;
}

…R