Different fingerprint ID to do different task problem

I have stored 2 fingerprint (ID#1 & ID#10), and now I have to control the water pump with 2 different fingerprint. For ID#1, it will start pumping liquid for 5 seconds if matched, and for ID#10, it will start to pump for 10 seconds if matched. The code now I have uploaded is just simply match fingerprint and start pump for 5 seconds. I have no idea how to write the code for ID#10. Please help, thanks!

#include <Adafruit_Fingerprint.h>



SoftwareSerial mySerial(2, 3);

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);



#define RELAY_PIN       10

#define ACCESS_DELAY    5000 // Keep lock unlocked for 3 seconds 





void setup()

{

  // set the data rate for the sensor serial port

  finger.begin(57600);

  delay(5);

  if (finger.verifyPassword()) 

  {

  } 

  else 

  {

    while (1) { delay(1); }

  }

  

  pinMode(RELAY_PIN, OUTPUT);

  digitalWrite(RELAY_PIN, LOW);   //Switch off relay initially. Relay is LOW level triggered relay so we need to write HIGH.

}



void loop()

{

  if ( getFingerPrint() != -1)

  {

    digitalWrite(RELAY_PIN, HIGH);

    delay(ACCESS_DELAY);

    digitalWrite(RELAY_PIN, LOW);   

  }  

  delay(50);            //Add some delay before next scan.

}



// returns -1 if failed, otherwise returns ID #

int getFingerPrint() 

{

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

}

  // found a match!

  return finger.fingerID;

You appear to be returning the ID of the fingerprint that has been found. You could test the ID that is returned to establish which one has been recognised. Try printing the value returned as a first step

Thank you for replying
I have write the code but for fingerprint ID#10
the delay is not 10seconds and it turn on and off with random seconds
Am I on the right track?

#include <Adafruit_Fingerprint.h>



SoftwareSerial mySerial(2, 3);

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);



#define RELAY_PIN       10

#define ACCESS_DELAY    5000 // Keep lock unlocked for 5 seconds 

#define ACCESS_DELAY_MORE 10000



void setup()

{

  // set the data rate for the sensor serial port

  finger.begin(57600);

  delay(5);

  if (finger.verifyPassword()) 

  {

  } 

  else 

  {

    while (1) { delay(1); }

  }

  

  pinMode(RELAY_PIN, OUTPUT);

  digitalWrite(RELAY_PIN, LOW);   //Switch off relay initially. Relay is LOW level triggered relay so we need to write HIGH.

}



void loop()

{

  if ( getFingerPrint() != -1)

  {

    digitalWrite(RELAY_PIN, HIGH);

    delay(ACCESS_DELAY);

    digitalWrite(RELAY_PIN, LOW);   

  }  

  delay(50);            //Add some delay before next scan.

}



// returns -1 if failed, otherwise returns ID #

int getFingerPrint() 

{

  int 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;
  if (finger.fingerID==10){
digitalWrite(RELAY_PIN, HIGH);

    delay(ACCESS_DELAY_MORE);

    digitalWrite(RELAY_PIN, LOW);   
return finger.fingerID;
  }  
}

Here is your sketch with the annoy[ng blank lines removed to make it easier to read

#include <Adafruit_Fingerprint.h>
SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
#define RELAY_PIN 10
#define ACCESS_DELAY 5000  // Keep lock unlocked for 5 seconds
#define ACCESS_DELAY_MORE 10000

void setup()
{
    // set the data rate for the sensor serial port
    finger.begin(57600);
    delay(5);
    if (finger.verifyPassword())
    {
    }
    else
    {
        while (1) { delay(1); }
    }
    pinMode(RELAY_PIN, OUTPUT);
    digitalWrite(RELAY_PIN, LOW);  //Switch off relay initially. Relay is LOW level triggered relay so we need to write HIGH.
}

void loop()
{
    if (getFingerPrint() != -1)
    {
        digitalWrite(RELAY_PIN, HIGH);
        delay(ACCESS_DELAY);
        digitalWrite(RELAY_PIN, LOW);
    }
    delay(50);  //Add some delay before next scan.
}

// returns -1 if failed, otherwise returns ID #
int getFingerPrint()
{
    int 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;
    if (finger.fingerID == 10)
    {
        digitalWrite(RELAY_PIN, HIGH);
        delay(ACCESS_DELAY_MORE);
        digitalWrite(RELAY_PIN, LOW);
        return finger.fingerID;
    }
}

Take note especially of these two lines

    // found a match!
    return finger.fingerID;

If a match has been found then the function ends immediately and the ID is returned to where the function was called so subsequent lines are never executed

It would be more conventional to call getFingerPrint() and take action on the value that is returned rather than taking action in the getFingerPrint() function itself

As it is, as long as what is returned is not -1 then these lines will be executed whatever the ID is

    {
        digitalWrite(RELAY_PIN, HIGH);
        delay(ACCESS_DELAY);
        digitalWrite(RELAY_PIN, LOW);
    }

Great! Now it works! Thank you for your help!!!

For the benefit of others, please post your working sketch

this is the working sketch

#include <Adafruit_Fingerprint.h>
SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
#define RELAY_PIN 10
#define ACCESS_DELAY 5000  // Keep lock unlocked for 5 seconds
#define ACCESS_DELAY_MORE 10000

void setup()
{
    // set the data rate for the sensor serial port
    finger.begin(57600);
    delay(5);
    if (finger.verifyPassword())
    {
    }
    else
    {
        while (1) { delay(1); }
    }
    pinMode(RELAY_PIN, OUTPUT);
    digitalWrite(RELAY_PIN, LOW);  //Switch off relay initially. Relay is LOW level triggered relay so we need to write HIGH.
}

void loop()
{
    if (getFingerPrint() != -1)
    {
       
    }
    delay(50);  //Add some delay before next scan.
}

// returns -1 if failed, otherwise returns ID #
int getFingerPrint()
{
    int 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!

    if (finger.fingerID == 1)
    {
        digitalWrite(RELAY_PIN, HIGH);
        delay(ACCESS_DELAY);
        digitalWrite(RELAY_PIN, LOW);
      
    }
if (finger.fingerID == 10)
    {
        digitalWrite(RELAY_PIN, HIGH);
        delay(ACCESS_DELAY_MORE);
        digitalWrite(RELAY_PIN, LOW);
      
    }
}

Once again, thank you for your help!!!

I am glad that you got it working

A more conventional way of doing it would be as follows. I can't test the sketch as I don't have the fingerprint hardware, but you should be able to see the principle

#include <Adafruit_Fingerprint.h>
SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
#define RELAY_PIN 10
#define ACCESS_DELAY 5000  // Keep lock unlocked for 5 seconds
#define ACCESS_DELAY_MORE 10000

void setup()
{
    // set the data rate for the sensor serial port
    finger.begin(57600);
    delay(5);
    if (finger.verifyPassword())
    {
    }
    else
    {
        while (1) { delay(1); }
    }
    pinMode(RELAY_PIN, OUTPUT);
    digitalWrite(RELAY_PIN, LOW);  //Switch off relay initially. Relay is LOW level triggered relay so we need to write HIGH.
}

void loop()
{
    int id = getFingerPrint();
    if (id == 1)
    {
        digitalWrite(RELAY_PIN, HIGH);
        delay(ACCESS_DELAY);
        digitalWrite(RELAY_PIN, LOW);
    }
    else if (id == 10)
    {
        digitalWrite(RELAY_PIN, HIGH);
        delay(ACCESS_DELAY_MORE);
        digitalWrite(RELAY_PIN, LOW);
    }
    delay(50);  //Add some delay before next scan.
}

// returns -1 if failed, otherwise returns ID #
int getFingerPrint()
{
    int 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;
}
1 Like