How to display "0" when tag is not read RFID RDM6300

Hi everyone. I am working on a project where my goal is to read tags at a constant time interval and display “0” when no tags are read. I followed a tutorial and modified the code I obtained from:

https://www.mschoeffler.de/2018/01/05/arduino-tutorial-how-to-use-the-rdm630-rdm6300-rfid-reader/

and was able to display the RFID tags constantly. However, I want it to also constantly print “0” when the system does not detect any tags. Could anyone give me advice on how to go about it? Thank you so much in advance! I’ve pasted the modified code below.

// (c) Michael Schoeffler 2018, http://www.mschoeffler.de
#include <SoftwareSerial.h>

const int BUFFER_SIZE = 14; // RFID DATA FRAME FORMAT: 1byte head (value: 2), 10byte data (2byte version + 8byte tag), 2byte checksum, 1byte tail (value: 3)
const int DATA_SIZE = 10; // 10byte data (2byte version + 8byte tag)
const int DATA_VERSION_SIZE = 2; // 2byte version (actual meaning of these two bytes may vary)
const int DATA_TAG_SIZE = 8; // 8byte tag
const int CHECKSUM_SIZE = 2; // 2byte checksum

SoftwareSerial ssrfid = SoftwareSerial(6,8); 

uint8_t buffer[BUFFER_SIZE]; // used to store an incoming data frame 
int buffer_index = 0;

void setup() {
Serial.begin(9600); 

ssrfid.begin(9600);
ssrfid.listen(); 

}

void loop() {
 if (ssrfid.available() > 0){
   bool call_extract_tag = false;
   
   int ssvalue = ssrfid.read(); // read 
   if (ssvalue == -1) {// no data was read
     return;
   }
   
   if (ssvalue == 2) { // RDM630/RDM6300 found a tag => tag incoming 
     buffer_index = 0;
   } else if (ssvalue == 3) { // tag has been fully transmitted       
     call_extract_tag = true; // extract tag at the end of the function call
   }

   if (buffer_index >= BUFFER_SIZE) { // checking for a buffer overflow (It's very unlikely that an buffer overflow comes up!)
     Serial.println("Error: Buffer overflow detected!");
     return;
   }
   
   buffer[buffer_index++] = ssvalue; // everything is alright => copy current value to buffer

   if (call_extract_tag == true) {
     if (buffer_index == BUFFER_SIZE) {
       unsigned tag = extract_tag();
     } else { // something is wrong... start again looking for preamble (value: 2)
       buffer_index = 0;
       return;
       
     }
   }    
 }
}

unsigned extract_tag() {
   uint8_t msg_head = buffer[0];
   uint8_t *msg_data = buffer + 1; // 10 byte => data contains 2byte version + 8byte tag
   uint8_t *msg_data_version = msg_data;
   uint8_t *msg_data_tag = msg_data + 2;
   uint8_t *msg_checksum = buffer + 11; // 2 byte
   uint8_t msg_tail = buffer[13];

   // print message that was sent from RDM630/RDM6300

   long tag = hexstr_to_value(msg_data_tag, DATA_TAG_SIZE);
   Serial.println(tag);

   long checksum = 0;
   for (int i = 0; i < DATA_SIZE; i+= CHECKSUM_SIZE) {
     long val = hexstr_to_value(msg_data + i, CHECKSUM_SIZE);
     checksum ^= val;
   }
   if (checksum == hexstr_to_value(msg_checksum, CHECKSUM_SIZE)) { // compare calculated checksum to retrieved checksum
   }
   return tag;
}

long hexstr_to_value(char *str, unsigned int length) { // converts a hexadecimal value (encoded as ASCII string) to a numeric value
 char* copy = malloc((sizeof(char) * length) + 1); 
 memcpy(copy, str, sizeof(char) * length);
 copy[length] = '\0'; 
 // the variable "copy" is a copy of the parameter "str". "copy" has an additional '\0' element to make sure that "str" is null-terminated.
 long value = strtol(copy, NULL, 16);  // strtol converts a null-terminated string to a long value
 free(copy); // clean up 
 return value;
}
if (ssvalue == -1) {// no data was read
      return;
    }

So you want to actually do something if ssvalue == -1 after all?

If you hurry, you can put your code inside tags before mods point out your breaking rules.

You could write your program so it normally prints '0' once per second except when it detects a valid RFID tag, You might want to arrange for the valid tag to be displayed for more than 1 second before the display returns to printing '0'

Note that if you try to print '0' if there is no tag (or the wrong tag) you will find that the Arduino tries to print '0' hundreds of times per second which will screw things up.

...R

Slumpert:
So you want to actually do something if ssvalue == -1 after all?

If you hurry, you can put your code inside tags before mods point out your breaking rules.

Thank you for the heads up. I put the code inside the tags now. Also I tried having the code print out “0” when the ssvalue == -1 by putting:

if (ssvalue == -1) {
      Serial.println("0");
      return;
    }

but it did not work.

ahsw21:
Thank you for the heads up. I put the code inside the tags now. Also I tried having the code print out "0" when the ssvalue == -1 by putting:

if (ssvalue == -1) {

Serial.println("0");
      return;
    }



but it did not work.

ahsw21:
Thank you for the heads up. I put the code inside the tags now. Also I tried having the code print out "0" when the ssvalue == -1 by putting:

if (ssvalue == -1) {

Serial.println("0");
      return;
    }



but it did not work.

This would indicate that ssvalue is NOT -1. Serial.println the value of ssvalue before the if statement and report back what that value is.

   if (call_extract_tag == true)
    {
      if (buffer_index == BUFFER_SIZE)
      {
        unsigned tag = extract_tag();
      }
      else     // something is wrong... start again looking for preamble (value: 2)
      {
        buffer_index = 0;
        return;
      }
    }
// ADD THIS PART:
    else
      Serial.println(0);

Romonaga:
This would indicate that ssvalue is NOT -1. Serial.println the value of ssvalue before the if statement and report back what that value is.

Ok so I plugged in Serial.println(ssvalue) as shown:

    int ssvalue = ssrfid.read(); // read 
    Serial.println(ssvalue);
    if (ssvalue == -1) {// no data was read
      return;

and the result was:

14:31:44.386 → 2
14:31:44.386 → 48
14:31:44.386 → 55
14:31:44.386 → 48
14:31:44.386 → 48
14:31:44.386 → 54
14:31:44.386 → 50
14:31:44.420 → 52
14:31:44.420 → 67
14:31:44.420 → 54
14:31:44.420 → 68
14:31:44.420 → 52
14:31:44.420 → 52
14:31:44.420 → 3
14:31:44.420 → 6442093

6442093 is the tag ID number. I’m not sure what all the numbers between 2 and 3 are.

johnwasser:

   if (call_extract_tag == true)

{
     if (buffer_index == BUFFER_SIZE)
     {
       unsigned tag = extract_tag();
     }
     else     // something is wrong... start again looking for preamble (value: 2)
     {
       buffer_index = 0;
       return;
     }
   }
// ADD THIS PART:
   else
     Serial.println(0);

Thank you for your input John. I added those two lines to the code:

        buffer_index = 0;
        return;
        
      }
    } else
      Serial.println(0);

However it seems like it just adds 13 zeros in between each tag reading as shown below.

14:36:27.426 -> 6442093
14:36:27.463 -> 0
14:36:27.463 -> 0
14:36:27.463 -> 0
14:36:27.463 -> 0
14:36:27.463 -> 0
14:36:27.497 -> 0
14:36:27.497 -> 0
14:36:27.497 -> 0
14:36:27.497 -> 0
14:36:27.497 -> 0
14:36:27.497 -> 0
14:36:27.497 -> 0
14:36:27.497 -> 0
14:36:27.497 -> 6442093

ahsw21:
Ok so I plugged in Serial.println(ssvalue) as shown:

    int ssvalue = ssrfid.read(); // read 

Serial.println(ssvalue);
    if (ssvalue == -1) {// no data was read
      return;




and the result was:

14:31:44.386 -> 2
14:31:44.386 -> 48
14:31:44.386 -> 55
14:31:44.386 -> 48
14:31:44.386 -> 48
14:31:44.386 -> 54
14:31:44.386 -> 50
14:31:44.420 -> 52
14:31:44.420 -> 67
14:31:44.420 -> 54
14:31:44.420 -> 68
14:31:44.420 -> 52
14:31:44.420 -> 52
14:31:44.420 -> 3
14:31:44.420 -> 6442093

6442093 is the tag ID number. I'm not sure what all the numbers between 2 and 3 are.

As you can see, you never return a -1 from this call, so the bool flip will never execute. As to where these random values are coming from, could not tell you.

ahsw21:
14:31:44.386 -> 2
14:31:44.386 -> 48
14:31:44.386 -> 55
14:31:44.386 -> 48
14:31:44.386 -> 48
14:31:44.386 -> 54
14:31:44.386 -> 50
14:31:44.420 -> 52
14:31:44.420 -> 67
14:31:44.420 -> 54
14:31:44.420 -> 68
14:31:44.420 -> 52
14:31:44.420 -> 52
14:31:44.420 -> 3
14:31:44.420 -> 6442093

6442093 is the tag ID number. I'm not sure what all the numbers between 2 and 3 are.

The 12 bytes between the Start of Text (STX=2) and End of Text (ETX=3) are the 12 characters of the tag: "0700624C6D44". The "624C6D" part is the hexadecimal tag ID which in decimal is 6442093. The first two bytes (0x0700) are probably a header and the last byte (0x44) is probably a check character (checksum or CRC).

To get a stream of 0’s when there is no tag present, change:

void loop() {
 if (ssrfid.available() > 0){

to:

void loop() {
  if (ssrfid.available() == 0)
  {
    Serial.println(0);  // Not receiving input so no tag present
  }
  else {