0

I'm working on the rfid module and found an example that do it well. But it has a long testing the 5 bytes of the rfid received bytes, and I've tried to write it in a more compact way.

I did it with a for loop, but how to AND the conditions of the if statement?

How to change this:

if(bt0[cnt-1] == rfid.serNum[0] &&
   bt1[cnt-1] == rfid.serNum[1] &&
   bt2[cnt-1] == rfid.serNum[2] &&
   bt3[cnt-1] == rfid.serNum[3] &&
   bt4[cnt-1] == rfid.serNum[4]){
   lcd.clear();
   lcd.print("card accepted");
   digitalWrite(green_led,HIGH);
   digitalWrite(red_led,LOW);
 }
 else{
    lcd.clear();
    lcd.print("card denied");
    digitalWrite(green_led,LOW);
    digitalWrite(red_led,HIGH);
}

To something like this:

    for(byte i=0;i<MXCD;i++){
      if(bt[i] == rfid.serNum[i%5]){
        cnt++;
      }
    }
    if(cnt==5){
      lcd.clear();
      lcd.print("card accepted");
      digitalWrite(green_led,HIGH);
      digitalWrite(red_led,LOW);
      cnt = 0;
    }
    else{
      lcd.clear();
      lcd.print("card denied");
      digitalWrite(green_led,LOW);
      digitalWrite(red_led,HIGH);
      cnt = 0; 
    }

In this way, I did it with 'cnt' variable to count the number of successful matched bytes then test of the number is equal to 5.

Is there a better way than this one ?

Thanks for any comments or suggestions :)

5
  • 5
    Hint: There is a standard C function for this, memcmp(). You would use it like, if (!memcmp(bt, rfid.serNum, 5)) .... Ref. cplusplus.com/reference/cstring/memcmp Commented Mar 10, 2019 at 17:14
  • wow thanks !! this function actually isn't new to me, but I didn't thought of it and its usage at this situation ! thanks again man ..
    – R1S8K
    Commented Mar 10, 2019 at 20:09
  • 1
    @MikaelPatel you should post that as an answer so the OP can accept it.
    – Duncan C
    Commented Mar 11, 2019 at 0:25
  • Yes, you're right.
    – R1S8K
    Commented Mar 11, 2019 at 12:49
  • Perch Eagle and @MikaelPatel, I want to learn something out of this question and your accepted answer, but I do not understand how it works. You write you want to compare elements of five different arrays (bt0[], bt1[], … bt4[]) with elements of one array (rfid.serNum[]). As an answer you are fine with a comparison with a new array bt[]. To make this work there must be a relation between bt[] and bt0[], bt1[], … bt4[]. Where in your sketch is this done or is this naturally but only I cannot understand?
    – RJPlog
    Commented Mar 25, 2019 at 20:55

1 Answer 1

2

There is a standard C function for this, memcmp(). It performs a memory block compare and can be used like this:

if (!memcmp(bt, rfid.serNum, sizeof(bt))) {
  lcd.clear();
  lcd.print("card accepted");
  digitalWrite(green_led,HIGH);
  digitalWrite(red_led,LOW);    
} 
else {
  ...
}

Ref. http://www.cplusplus.com/reference/cstring/memcmp/

1
  • Yep, it works perfectly! Thank you dude ..
    – R1S8K
    Commented Mar 11, 2019 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.