Help me with Fingerprint scanning!

Friends, i am coding really hard, day and night. I am facing a strange problem! Please help me!

My Objective: To add fingerprint and to search it.
Components connected: SM630 fingerprint module, Mega 2560 R3 and a PC
Problem: Adding and searching a same fingerprint executes correctly but when i add thumb finger and search by index finger, the program is not telling that fingerprint is not found or not executing at all

Please help me friends… I have also attached code, output images and datasheet too for your reference!

The problem I noticed is that in if statement below(commented it) ‘b’ evaluates to zero correctly but not execute statements after it!

Also ‘f’ and ‘l’ does not equals to zero! l=254, any idea???

Help me…!

/************************************************************************************
Command Code & Respond Code For  ADD fingerprint
*************************************************************************************/
byte addFingerPrint[] = {0x4D, 0x58, 0x10, 0x03, 0x40, 0x00, 0x00, 0xF8}; // 8 bytes

byte operationSuccessAdd[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x31, 0x48}; // 7 bytes
byte timeOutAdd[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x33, 0x4A}; // 7 bytes
byte processFailureAdd[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x34, 0x4B}; // 7 bytes
byte parameterErrorAdd[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x35, 0x4C}; // 7 bytes

/*************************************************
Command Code & Respond Code For Search Fingerprint
*************************************************/
byte searchFingerPrint[] = {0x4D, 0x58, 0x10, 0x05, 0x44, 0x00, 0x00, 0x00, 0x05, 0x03}; // 10 bytes

byte FingerPrint0[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x00, 0x56}; // 10 bytes
byte FingerPrint1[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x01, 0x57}; // 10 bytes
byte FingerPrint2[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x02, 0x58}; // 10 bytes
byte FingerPrint3[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x03, 0x59}; // 10 bytes
byte FingerPrint4[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x04, 0x5A}; // 10 bytes

byte operationSuccessSearch[] = {0x4D, 0x58, 0x30, 0x02, 0x44, 0x31, 0x4C}; // 7 bytes
byte timeOutSearch[] = {0x4D, 0x58, 0x30, 0x02, 0x44, 0x33, 0x4E}; // 7 bytes
byte parameterErrorSearch[] = {0x4D, 0x58, 0x30, 0x02, 0x44, 0x35, 0x50}; // 7 bytes
byte processFailureSearch[] = {0x4D, 0x58, 0x30, 0x02, 0x44, 0x34, 0x4F}; // 7 bytes
byte noMatchFinger[] = {0x4D, 0x58, 0x30, 0x02, 0x44, 0x3A, 0x55}; // 7 bytes

byte respondCorrect[] = {0x4D, 0x58, 0x30, 0x01, 0x01, 0xD7}; // 6 bytes
byte respond[6],operation[7],operationOK[7],operationFound[9],a,b,c,d,e,f,g,h,i,j,k,l,m;


void setup() {
Serial.begin(57600);
addFinger();
correctRespond();
operationSuccesful();
Serial.end();
delay(1500);
Serial.begin(57600);
searchFinger();
correctRespond();
operationSuccesful();
Serial.end();
}

void loop() {
  // put your main code here, to run repeatedly:
}

void addFinger(){
  Serial.write(addFingerPrint, sizeof(addFingerPrint));   // write the 8bytes array of the command code
  delay(1000);
}

void searchFinger(){
  // write the 8bytes array of the command code
  Serial.write(searchFingerPrint, sizeof(searchFingerPrint));
  Serial.println(" Searching... ");
  delay(2000);
}

void correctRespond() {
  // correct response contains all 6 bytes
  for(byte z=0; z<6; z++)
  {
    while(Serial.available() == 0) {}; // Wait for a byte
    respond[z] = Serial.read();
  }
    
    a = memcmp(respondCorrect, respond, sizeof(respondCorrect));

    if (a!=0) {
      Serial.println(" Invalid Command code!");
      }
  delay(500);
}

void operationSuccesful() {
  // reply contains all 7 bytes
  for(byte x=0; x<7; x++)
  {
    while(Serial.available() == 0) {}; // Wait for a byte
    operation[x] = Serial.read();
  }
    b = memcmp(operationSuccessSearch, operation, sizeof(operationSuccessSearch));
    c = memcmp(parameterErrorSearch, operation, sizeof(parameterErrorSearch));
    d = memcmp(processFailureSearch, operation, sizeof(processFailureSearch));
    e = memcmp(timeOutSearch, operation, sizeof(timeOutSearch));
    f = memcmp(noMatchFinger, operation, sizeof(noMatchFinger));  //not getting equal to zero!
    m = memcmp(operationSuccessAdd, operation, sizeof(operationSuccessAdd));
Serial.print("operation[0]");
Serial.println(operation[0],HEX);
Serial.print("operation[1]");
Serial.println(operation[1],HEX);
Serial.print("operation[2]");
Serial.println(operation[2],HEX);
Serial.print("operation[3]");
Serial.println(operation[3],HEX);
Serial.print("operation[4]");
Serial.println(operation[4],HEX);
Serial.print("operation[5]");
Serial.println(operation[5],HEX);
Serial.print("operation[6]");
Serial.println(operation[6],HEX);
Serial.println(" ");


   if (m==0) {
      Serial.println(" Fingerprint added Successfully");
         }
   else if (b==0) { // b equals zero but l does not equals to zero when wrong finger is placed! but equals zero when correct finger is placed!
      for(byte y=0; y<9; y++)
      {
        while(Serial.available() == 0) {}; // Wait for a byte
        operationFound[y] = Serial.read();
      }
        
        g = memcmp(FingerPrint0, operationFound, sizeof(FingerPrint0));
        h = memcmp(FingerPrint1, operationFound, sizeof(FingerPrint1));
        i = memcmp(FingerPrint2, operationFound, sizeof(FingerPrint2));
        j = memcmp(FingerPrint3, operationFound, sizeof(FingerPrint3));
        k = memcmp(FingerPrint4, operationFound, sizeof(FingerPrint4));
        l = memcmp(noMatchFinger, operationFound, sizeof(noMatchFinger));
Serial.print("operationFound[0]");
Serial.println(operationFound[0],HEX);
Serial.print("operationFound[1]");
Serial.println(operationFound[1],HEX);
Serial.print("operationFound[2]");
Serial.println(operationFound[2],HEX);
Serial.print("operationFound[3]");
Serial.println(operationFound[3],HEX);
Serial.print("operationFound[4]");
Serial.println(operationFound[4],HEX);
Serial.print("operationFound[5]");
Serial.println(operationFound[5],HEX);
Serial.print("operationFound[6]");
Serial.println(operationFound[6],HEX);
Serial.print("operationFound[7]");
Serial.println(operationFound[7],HEX);
Serial.print("operationFound[8]");
Serial.println(operationFound[8],HEX);

   if (g==0) {
      Serial.println(" Fingerprint 1 found ");
         }
   else if (h==0) {
      Serial.println(" Fingerprint 2 found ");
         }
   else if(i==0) {
      Serial.println(" Fingerprint 3 found ");
         }
   else if(j==0) {
      Serial.println(" Fingerprint 4 found ");
         }
   else if(k==0) {
      Serial.println(" Fingerprint 5 found ");
   }
   else if(l==0) {
   Serial.println(" No match found! ");
         }
     }
   else if(c==0) {
      Serial.println(" Parameter error! ");
         }   
   else if(d==0) {
      Serial.println(" Process failed! ");
         }   
   else if(e==0) {
      Serial.println(" Time out! ");
         }
   else if(f==0){
   Serial.println(" No match found! ");
         }
}

not found.jpg

User Manual.pdf (552 KB)

Can you copy and paste the output(s), don't take screen shots of it? It makes it impossible to search for stuff in it.

How about meaningful variable names? A variable named "l" is about the most annoying thing you could call it, as it looks just like the number "1". Next you'll be using the letter O and the number 0.

Also, a, b, c, d, e ... really? That makes the code look like a jumbled mess.

Also here:

byte FingerPrint0[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x00, 0x56}; // 10 bytes
byte FingerPrint1[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x01, 0x57}; // 10 bytes
byte FingerPrint2[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x02, 0x58}; // 10 bytes
byte FingerPrint3[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x03, 0x59}; // 10 bytes
byte FingerPrint4[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x04, 0x5A}; // 10 bytes

Start using arrays. Once you number a variable a1, a2, a3 etc. it is time to use an array.

Ditto for here:

Serial.print("operation[0]");
Serial.println(operation[0],HEX);
Serial.print("operation[1]");
Serial.println(operation[1],HEX);
Serial.print("operation[2]");
Serial.println(operation[2],HEX);
Serial.print("operation[3]");
Serial.println(operation[3],HEX);
Serial.print("operation[4]");
Serial.println(operation[4],HEX);
Serial.print("operation[5]");
Serial.println(operation[5],HEX);
Serial.print("operation[6]");
Serial.println(operation[6],HEX);

Make a loop.

You might think I am nit-picking but you are filling up your code with baffling repetitive stuff that hides what is really happening.

From the screenshots attached below, i promise you will understand what i am coming to ask!

First scenario is working correctly! i.e., Add and search same finger
Second scenario fails! i.e., Add finger and search different finger(should return false but not returning)

The main issue is here to my understanding! Correct me if wrong!

   else if (b==0) {
      for(byte y=0; y<9; y++)
      {
        while(Serial.available() == 0) {}; // Wait for a byte
        operationFound[y] = Serial.read();
      }

Help me! :frowning: :frowning: :frowning:

5ca.jpg

ssss.jpg

val.jpg

vall.jpg

irfanece: The main issue is here to my understanding! Correct me if wrong!

You haven't understood what I said about tidying up your code.

Perhaps a simplified code might help you!

How about this?

/************************************************************************************
ADD
*************************************************************************************/
byte addFingerPrint[] = {0x4D, 0x58, 0x10, 0x03, 0x40, 0x00, 0x00, 0xF8}; // 8 bytes

byte operationSuccessAdd[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x31, 0x48}; // 7 bytes
byte timeOutAdd[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x33, 0x4A}; // 7 bytes
byte processFailureAdd[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x34, 0x4B}; // 7 bytes
byte parameterErrorAdd[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x35, 0x4C}; // 7 bytes

/*************************************************
Command Code & Respond Code For Search Fingerprint
*************************************************/
byte searchFingerPrint[] = {0x4D, 0x58, 0x10, 0x05, 0x44, 0x00, 0x00, 0x00, 0x05, 0x03}; // 10 bytes

byte FingerPrint0[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x00, 0x56}; // 10 bytes
byte FingerPrint1[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x01, 0x57}; // 10 bytes
byte FingerPrint2[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x02, 0x58}; // 10 bytes
byte FingerPrint3[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x03, 0x59}; // 10 bytes
byte FingerPrint4[] = {0x4D, 0x58, 0x30, 0x04, 0x44, 0x39, 0x00, 0x04, 0x5A}; // 10 bytes

byte operationSuccessSearch[] = {0x4D, 0x58, 0x30, 0x02, 0x44, 0x31, 0x4C}; // 7 bytes
byte timeOutSearch[] = {0x4D, 0x58, 0x30, 0x02, 0x44, 0x33, 0x4E}; // 7 bytes
byte parameterErrorSearch[] = {0x4D, 0x58, 0x30, 0x02, 0x44, 0x35, 0x50}; // 7 bytes
byte processFailureSearch[] = {0x4D, 0x58, 0x30, 0x02, 0x44, 0x34, 0x4F}; // 7 bytes
byte noMatchFinger[] = {0x4D, 0x58, 0x30, 0x02, 0x44, 0x3A, 0x55}; // 7 bytes

byte respondCorrect[] = {0x4D, 0x58, 0x30, 0x01, 0x01, 0xD7}; // 6 bytes
byte respond[6],operation[7],operationOK[7],operationFound[9],a,b,c,d,e,f,g,h,i,j,k,l,m;


void setup() {
Serial.begin(57600);
addFinger();
correctRespond();
operationSuccesful();
Serial.end();
delay(1500);
Serial.begin(57600);
searchFinger();
correctRespond();
operationSuccesful();
Serial.end();
}

void loop() {
  // put your main code here, to run repeatedly:
}

void addFinger(){
  Serial.write(addFingerPrint, sizeof(addFingerPrint));   // write the 8bytes array of the command code
  delay(1000);
}

void searchFinger(){
  // write the 8bytes array of the command code
  Serial.write(searchFingerPrint, sizeof(searchFingerPrint));
  Serial.println(" Searching... ");
  delay(2000);
}

void correctRespond() {
  // correct response contains all 6 bytes
  for(byte z=0; z<6; z++)
  {
    while(Serial.available() == 0) {}; // Wait for a byte
    respond[z] = Serial.read();
  }
    
    a = memcmp(respondCorrect, respond, sizeof(respondCorrect));

    if (a!=0) {
      Serial.println(" Invalid Command code!");
      }
  delay(500);
}

void operationSuccesful() {
  // reply contains all 7 bytes
  for(byte x=0; x<7; x++)
  {
    while(Serial.available() == 0) {}; // Wait for a byte
    operation[x] = Serial.read();
  }
    b = memcmp(operationSuccessSearch, operation, sizeof(operationSuccessSearch));
    c = memcmp(parameterErrorSearch, operation, sizeof(parameterErrorSearch));
    d = memcmp(processFailureSearch, operation, sizeof(processFailureSearch));
    e = memcmp(timeOutSearch, operation, sizeof(timeOutSearch));
    f = memcmp(noMatchFinger, operation, sizeof(noMatchFinger));
    m = memcmp(operationSuccessAdd, operation, sizeof(operationSuccessAdd));

   if (m==0) {
      Serial.println(" Fingerprint added Successfully");
         }
   else if (b==0) {
      for(byte y=0; y<9; y++)
      {
        while(Serial.available() == 0) {}; // Wait for a byte
        operationFound[y] = Serial.read();
      }
        
        g = memcmp(FingerPrint0, operationFound, sizeof(FingerPrint0));
        h = memcmp(FingerPrint1, operationFound, sizeof(FingerPrint1));
        i = memcmp(FingerPrint2, operationFound, sizeof(FingerPrint2));
        j = memcmp(FingerPrint3, operationFound, sizeof(FingerPrint3));
        k = memcmp(FingerPrint4, operationFound, sizeof(FingerPrint4));
        l = memcmp(noMatchFinger, operationFound, sizeof(noMatchFinger));

   if (g==0) {
      Serial.println(" Fingerprint 1 found ");
         }
   else if (h==0) {
      Serial.println(" Fingerprint 2 found ");
         }
   else if(i==0) {
      Serial.println(" Fingerprint 3 found ");
         }
   else if(j==0) {
      Serial.println(" Fingerprint 4 found ");
         }
   else if(k==0) {
      Serial.println(" Fingerprint 5 found ");
   }
   else if(l==0) {
   Serial.println(" No match found! ");
         }
     }
   else if(c==0) {
      Serial.println(" Parameter error! ");
         }   
   else if(d==0) {
      Serial.println(" Process failed! ");
         }   
   else if(e==0) {
      Serial.println(" Time out! ");
         }
   else if(f==0){
   Serial.println(" No match found! ");
         }
}

I Serial printed that arrays and variables before so that it may help you!
And what/where actually do you think is the bug???

The program repeatedly stops here!

else if (b==0) {
for(byte y=0; y<9; y++)
{
while(Serial.available() == 0) {}; // Wait for a byte
operationFound[y] = Serial.read();
}

Still debugging!!! I think it’s not about the variable and array names… The program execution may be i suppose!

To get past that loop you would have to have provided 16 bytes of serial input. Have you?

You obviously don't think variable names are important, so perhaps I can interest you in the Obfuscated C Code Contest.

How would you like to debug this?

 main(){;
    ;;;s , ra,
 n, d), && C ( j +R( s-j+t( i- 1) )
   )D( W)m[ c* 4| z( j ) ]
    &&_( X )E (d   ( 0
       )&&  R( 6)/ 6
   ? c= 17|_ ( T )
   :01,||+
       , v =i & 3 ^ 2
       ,+j & 3&&W ( j)||y ( 10 )D (k) --
   j ? k ( O[j]=R(15 ))
: 1 D
 ( H )o ( 7+! !  i+3 )+

  i?

http://forum.arduino.cc/index.php?topic=58073.0

[quote author=Nick Gammon date=1424582127 link=msg=2105766]

 main(){;
    ;;;s , ra,
 n, d), && C ( j +R( s-j+t( i- 1) )
   )D( W)m[ c* 4| z( j ) ]
    &&_( X )E (d   ( 0
       )&&  R( 6)/ 6
   ? c= 17|_ ( T )
   :01,||+
       , v =i & 3 ^ 2
       ,+j & 3&&W ( j)||y ( 10 )D (k) --
   j ? k ( O[j]=R(15 ))
: 1 D
 ( H )o ( 7+! !  i+3 )+

  i?

http://forum.arduino.cc/index.php?topic=58073.0 [/quote]

Oh come on! Sorry about that! :-[

And this array: byte noMatchFinger[] = {0x4D, 0x58, 0x30, 0x02, 0x44, 0x3A, 0x55}; // 7 bytes is only 7 bytes long, why wait for 16 bytes??? The response code is also only 7 bytes long... You saw output pics? Any conclusions from that???

void operationSuccesful() {
  // reply contains all 7 bytes
  for(byte x=0; x<7; x++)
  {
    while(Serial.available() == 0) {}; // Wait for a byte
    operation[x] = Serial.read();
  }

...

  if (m==0) {
      Serial.println(" Fingerprint added Successfully");
         }
   else if (b==0) {
      for(byte y=0; y<9; y++)
      {
        while(Serial.available() == 0) {}; // Wait for a byte
        operationFound[y] = Serial.read();
      }

I count 16. What do you count?

o~6 = 7 bytes....

loops iterates from 0 till 6.... How come 16???

irfanece:
The program repeatedly stops here!

   else if (b==0) {

for(byte y=0; y<9; y++)
     {
       while(Serial.available() == 0) {}; // Wait for a byte
       operationFound[y] = Serial.read();
     }

You said it stops there (above). Therefore you think it read the first 7 bytes, and is now reading these 9. Total: 16.

You said it stops there (above). Therefore you think it read the first 7 bytes, and is now reading these 9. Total: 16.

Oh, i guess it makes some sense!!! My mind is so scrambled with codes!
So what do you think the correct code should be???

But did you notice these images below???

not found.jpg

Please notice that i have declared:

byte noMatchFinger[] = {0x4D, 0x58, 0x30, 0x02, 0x44, 0x3A, 0x55}; // 7 bytes array

2 conditions:

f = memcmp(noMatchFinger, operation, sizeof(noMatchFinger));

and

l = memcmp(noMatchFinger, operationFound, sizeof(noMatchFinger));

Don’t they mean anything??? Atleast one of them should evaluate to zero from pics below:

val.jpg

vall.jpg

irfanece: Don't they mean anything??? Atleast one of them should evaluate to zero from pics below:

In my first reply I said:

Can you copy and paste the output(s), don't take screen shots of it? It makes it impossible to search for stuff in it.

I conclude that you are ignoring what I am saying.

Apologizes! :-[

Here you are:

f=4
b=4
Fingerprint added Successfully
Searching…
f=9
b=0
l=254
Fingerprint 1 found

f=4
b=4
Fingerprint added Successfully
Searching…
f=9
b=0

It looks to me like your command sequence is wrong for what you are trying to do. The manual says something about "If user press different finger at the first time and second time, or the fingerprint quality is poor, module will responds as fingerprint processing failure"

From the manual:

1 HOST send command to add fingerprint at position 0: 0x4D + 0x58 + 0x10 + 0x03 + 0x40 + 0x00 + 0x00 + 0xF8 2 Module responds by receive correct: 0x4D + 0x58 + 0x30 + 0x01 + 0x01 + 0xD7 3 First time to press finger. Module will respond as operation successful after processing the first fingerprint: 0x4D + 0x58 + 0x30 + 0x02 + 0x40 + 0x31 + 0x48 4 Press finger again, and module will respond as operation successful after processing: 0x4D + 0x58 + 0x30 + 0x02 + 0x40 + 0x31 + 0x48 Remarks: 1 Fingerprint ID starts from 0 2 Fingerprint storage capacity: 768 3 If the ID is wrong in the command, module will responds as parameter error: 0x4D + 0x58 + 0x30 + 0x02 + 0x40 + 0x35 + 0x4C 4 If user press different finger at the first time and second time, or the fingerprint quality is poor, module will responds as fingerprint processing failure: 0x4D + 0x58 + 0x30 + 0x02 + 0x40 + 0x34 + 0x4B 5 If there is no finger pressing within 10 seconds, module will respond as time-out. 0x4D + 0x58 + 0x30 + 0x02 + 0x40 + 0x33 + 0x4A

Exactly! That's what i'm asking! And i have already declared those in above program codes... The problem is that they are not getting evaluated at all....

I pressed a different finger... But it is not giving me a wrong finger output!!! But the correct finger is working fine!!!