Multiple readings on RFID RDM6300 on Serial1 (Mega)

Hello everyone.

I have tried to follow in a project with RFID RDM6300 in an Arduino Mega, using the Serial1 port, because in the future I will use another antenna in Serial2 and because of this makes unfeasible the use of the SoftwareSerial library. (at least that’s what I understood by looking at some posts here on the forum).

After a while researching, I found a good example that can serve as a basis for completing the project.

Using Serial1, the example gives me the read tag and the comparisons in the array that are stored in the tags.

The problem is that repeated readings are taking place on the antenna. the sure thing would be to lean the tag on the antenna and make it read only.

I already tried to put delay (), already tried to do a comparison array and use the memcpy and memcmp functions and I was not successful.

Could someone help me?

char* allowedTags[] = {
  "[INSERT IDENTIFIER HERE]", // Tag 1
  "A0721A2EAD", // Tag 2
  "ABC123DE45", // Tag 3
};

char* tagName[] = {
  "[ADD YOUR NAME HERE]", // Tag 1
  "Neto", // Tag 2
  "NAME 3", // Tag 3
};

int numberOfTags = sizeof(allowedTags)/sizeof(allowedTags[0]);

int incomingByte = 0; // To store incoming serial data

void setup() {
Serial.begin(9600); // Serial port for connection to host
Serial1.begin(9600); // Serial port for connection to RFID module
Serial.println("RFID Reader Initialized");
}

void loop() {
  new_read();
}


int findTag( char tagValue[10] ) {
  for (int thisCard = 0; thisCard < numberOfTags; thisCard++) {
    if(strcmp(tagValue, allowedTags[thisCard]) == 0){
      return(thisCard + 1);
    }
  }
  return(0);
} 

//////////////////////////////////////////////////////////////////////////////

void new_read(){
    byte i = 0;
    byte val = 0;
    byte checksum = 0;
    byte tempByte = 0;
    byte tagBytes[6]; // "Unique" tags are only 5 bytes but we need an extra byte for the checksum
    char tagValue[10];
    char checkTag[10];
    byte bytesRead = 0;

    if (0x02 == (val = Serial1.read ())) { // Check for header
      while (bytesRead < 12) { // Read 10 digit code + 2 digit checksum
        val = Serial1.read();
        
        // Append the first 10 bytes (0 to 9) to the raw tag value
        if (bytesRead < 10){
          tagValue[bytesRead] = val;
        }
        
        // Check if this is a header or stop byte before the 10 digit reading is complete
       if ((0x0D == val) ||
              (0x0A == val) ||
              (0x03 == val) ||
              (0x02 == val)) {
            // stop reading - there is an error.
            break;
          }
        
        // 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 a byte to the code:
        if (bytesRead & 1 == 1) {
          // Make space for this hex-digit by shifting the previous digit 4 bits to the left
          tagBytes[bytesRead >> 1] = (val | (tempByte << 4));
          
          if (bytesRead >> 1 != 5) { // If we're at the checksum byte,
            checksum ^= tagBytes[bytesRead >> 1]; // Calculate the checksum... (XOR)
          };
        } else {
          tempByte = val; // Store the first hex digit first
        }
        
        bytesRead++; // Ready to read next digit
      }
      
      // Send the result to the host connected via USB
      if (bytesRead == 12) { // 12 digit read is complete
          tagValue[10] = '\0'; // Null-terminate the string
          if ((tagBytes[5] == 255) or (tagBytes[5] == 0)){
          }else{

          Serial.print("Tag read: ");
          for (i=0; i<5; i++) {
            // Add a leading 0 to pad out values below 16
            if (tagBytes[i] < 16) {
            Serial.print("0");
            }
            Serial.print(tagBytes[i], HEX);
          }
          Serial.println();
          
          Serial.print("Checksum: ");
          Serial.print(tagBytes[5], HEX);
          Serial.println(tagBytes[5] == checksum ? " -- passed." : " -- error.");

          // Show the raw tag value
          Serial.print("VALUE: ");
          Serial.println(tagValue);

          // Search the tag database for this particular tag
          int tagId = findTag( tagValue );
          
          // Only fire the strike plate if this tag was found in the database
          if( tagId > 0 ){
            Serial.print("Authorized tag ID ");
            Serial.print(tagId);
            Serial.print(": unlocking for ");
            Serial.println(tagName[tagId - 1]); // Get the name for this tag from the database
            //unlock(); // Fire the strike plate to open the lock
          } else {
            Serial.println("Tag not authorized");
          }
            Serial.println(); // Blank separator line in output
        }
      }

      bytesRead = 0;
      delay(1000);
    }
}

this is what I get on the Serial port when I only put the tag once.

RFID Reader Initialized
Tag read: 00721A2EAD
Checksum: 4B -- passed.
VALUE: A0721A2EAD
Authorized tag ID 2: unlocking for Neto

Tag read: 00721A2EAD
Checksum: 4B -- passed.
VALUE: A0721A2EAD
Authorized tag ID 2: unlocking for Neto

Tag read: 00721A2EAD
Checksum: 4B -- passed.
VALUE: A0721A2EAD
Authorized tag ID 2: unlocking for Neto

:cold_sweat:

Anyone?