Go Down

Topic: RFID Tag Compare (Read 1 time) previous topic - next topic

PaulS

Quote
Here is the code for the loop:

You could, of course, print in the body of the if statement the value of n, so you know WHICH tag the scanned one matched, if that was important.

UNTEngineer

I have another question, similarly relating to this.

I want the program to print out when the card validates, and also when the card doesnt validate. Ive tried this:

Code: [Select]

if(memcmp(master, target_tag[n], TAG_LEN) == 0 )  //Compares two bytes of TAG_LEN, if they are a match, the argument will be 0, thus 0 == 0
  {
            Serial.println("This Card is a Match!");
  }
        else if(memcmp(master, target_tag[n], TAG_LEN) != 0)
            {     
              Serial.println("This Card is NOT a Match!");
            }


If it validates, it outputs once. If it doesnt validate, it outputs twice- for each tag it reads in.

What I want it to do is compare vs both tags, and then if it validates, great, output once - like its already doing. If not, output ONCE as well. Any suggestions of how to get that to work?
Working on a Smart Home Management System. Visit my blog: http://tae09.blogspot.com/ to find out more, and to keep track with the project.

frank26080115

it's hard to diagnose the problem without the entire code
Freelance engineer, consultant, contractor. Graduated from UW in 2013.

PaulS

Code: [Select]
if(memcmp(master, target_tag[n], TAG_LEN) == 0 )  //Compares two bytes of TAG_LEN, if they are a match, the argument will be 0, thus 0 == 0
  {
            Serial.println("This Card is a Match!");
  }
        else if(memcmp(master, target_tag[n], TAG_LEN) != 0)

If the value returned be memcmp() is not 0, how can it be anything but not zero? The second if test is redundant.

Most likely what is happening is that you are learning that one of the tags is not tag 0 and is not tag 1, when you read tag 2. If you were to include n in the output message, I think you'd see that the messages are NOT identical. But, without seeing the whole code, who knows for sure.

UNTEngineer


If the value returned be memcmp() is not 0, how can it be anything but not zero? The second if test is redundant.

Most likely what is happening is that you are learning that one of the tags is not tag 0 and is not tag 1, when you read tag 2. If you were to include n in the output message, I think you'd see that the messages are NOT identical. But, without seeing the whole code, who knows for sure.


I was trying to do an IF/ELSE statement, and I wasnt getting anywhere, so I figured to try the opposite of my IF statement to see if that worked. It did exactly the same as the ELSE statement. So yes, its redundant. However, since you guys are most insistent about seeing the whole code to analyze the problem, here you go:

Code: [Select]

#include <NewSoftSerial.h>
NewSoftSerial mySerial(2, 3); //rx, tx

#define TAG_LEN 5
#define ARR_LEN 2

byte target_tag[ARR_LEN][TAG_LEN] = //This array of TAG ID's will later be in a database on the BB
{{0x4F, 0x00, 0x88, 0xB3, 0x4F}, //Card 1 Tag ID
{TAG_ID_2}}; //Card 2 Tag ID

boolean match = false; // initialize card match to false

void setup() {
Serial.begin(9600);// connect to the serial port
mySerial.begin(9600);
Serial.println("Bring an RFID tag near the reader...");
}

void loop () {
  byte i = 0;
  byte val = 0;
  byte master[6];
  byte checksum = 0;
  byte bytesread = 0;
  byte tempbyte = 0;

  if(mySerial.available() > 0) {
    if((val = mySerial.read()) == 2) {                  // check for header
      bytesread = 0;
      while (bytesread < 12) {                        // read 10 digit master + 2 digit checksum
        if( mySerial.available() > 0) {
          val = mySerial.read();
          if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
            break;                                    // stop reading
          }

          // Do Ascii/Hex conversion:
          if ((val >= '0') && (val <= '9')) {
            val = val - '0';
          } else if ((val >= 'A') && (val <= 'F')) {
            val = 10 + val - 'A';
          }

          // Every two hex-digits, add byte to master:
          if (bytesread & 1 == 1) {
            // make some space for this hex-digit by
            // shifting the previous hex-digit with 4 bits to the left:
            master[bytesread >> 1] = (val | (tempbyte << 4));

            if (bytesread >> 1 != 5) {                // If we're at the checksum byte,
              checksum ^= master[bytesread >> 1];       // Calculate the checksum... (XOR)
            };
          } else {
            tempbyte = val;                           // Store the first hex digit first...
          };

          bytesread++;                                // ready to read next digit
        }
      }
      //do something else perhaps wait for other data.
     
      if (bytesread == 12) {                          // if 12 digit read is complete
        Serial.println();
        Serial.print("5-byte master: ");
       
        for (i=0; i<5; i++) {
          if (master[i] < 16)
            Serial.print("0");
          byte master1 = master[i];
          Serial.print(master[i], HEX);              //Prints the Tag of the Card read
          Serial.print(" ");
        }
        for (int n=0; n<ARR_LEN; n++){               //Checks each ID Tag in the Array
       
if(memcmp(master, target_tag[n], TAG_LEN) == 0 )  //Compares two bytes of TAG_LEN, if they are a match, the argument will be 0, thus 0 == 0
  {
            Serial.println();
            Serial.println("This Card is a Match!");
            Serial.println();
  }
        else
            {     
              Serial.println("This Card is NOT a Match!");
            }
       }
    }
      bytesread = 0;
      Serial.flush();
    }
  }
}

};


Hope this helps in deciphering the two-time repetition of the "This Card is NOT a Match!" statement.
Working on a Smart Home Management System. Visit my blog: http://tae09.blogspot.com/ to find out more, and to keep track with the project.

Go Up