RFID Repeating?

Hello Arduino world,

I have a RFID reader (http://robotics.org.za/index.php?route=product/product&path=114_96&product_id=243)
and
Arduino UNO R3

I connected it like the following instructable

This is my code

#include <SoftwareSerial.h>

SoftwareSerial mySerial(11, 12); 

void setup()  
{
  Serial.begin(9600);
  Serial.println("Serial Ready");

  mySerial.begin(9600);
  Serial.println("RFID Ready");
}

void loop()
{
  if(mySerial.available()>0)
    Serial.print("Reading");
}

Now my problem is when I open my Serial Monitor, It gives me Serial Read and RFID Ready.
Thats all good.
Now when I first scan my card/tag I get Reading printed over and over but when I remove my card/tag it still prints Reading? Why is that?
I understand that .available function reads when my card/tag is nearby and should stop reading if my card/tag is removed but I am wrong and I have tried many things now.
Can anyone send me in some new direction to help me out.

Kind Regards
Jaco

You are not reading anything from the serial buffer so once data is available it will remain available.

It helps if you post the actual code you are unsung. You can not get any readings from that code.

Values from the reader are backing up in the buffer and that is why it appeares to carry on reading when you withdraw the card.

My bad I get now what you meant by the buffer!

The program printed the card/tag a few times after it was removed(still in the buffer i guess?) that was my initial error and I fixed it by adding this while loop at the end!

#include <SoftwareSerial.h>

SoftwareSerial mySerial(11, 12); 

boolean found=true;
char c;
String msg = "";

void setup()  
{
  Serial.begin(9600);
  Serial.println("Serial Ready");

  mySerial.begin(9600);
  Serial.println("RFID Ready");
}

void loop()
{
  while(mySerial.read() != -1)
  {
    
    while(mySerial.available()>0){
      c=mySerial.read(); 
      msg += c;
      if(msg.length() == 12)
        break;
    }
    
    
    Serial.println("Gate Open");
    Serial.println("ID: "+msg);
    if(found)
    {
      msg = "";
      delay(3000);
    }
    while(mySerial.read() != -1) {}; //This one to clear the buffer?
  }
}

Many thanks!!

Oh wow nevermind the problem still occur.

Is it possible to clear to buffer after I got a first reading of my card/tag?
So that it won't keep printing afterwards like pause for 3 seconds and then be able to read cards from scratch and repeat*

There is a Serial.flush() command but I seem to remember that there are problems with it. The description in the reference pages says "Waits for the transmission of outgoing serial data to complete." which seems to me a waste of time if I understand it correctly as there will then be no data to flush.

After you have read the ID once you could read serial data and discard it until no data is available which would indicate that the card/tag had been removed.

It sounds like it is the reader that is sending the data. Therefore you have to deal with it. Does it continuously send the tag ID or will it stop after a item?

UKHeliBob yeah isnt that what my while loop was suppose to do? Discarding the buffer.

Grumpy_Mike I think that is what the problem is. Like when I scan the tag it picks it up a few times. Arduino handles the first one with that 3sec delay now after the delay it goes and read again from the RFID(which has the tag id a few more times?)
Then it outputs it like 3 or more times after the tag has been removed.
So it doesnt continuously sends it just 3 or 4 times after the tag has been removed then stops.

But for some mirycal it is working perfectly now havnt had any problems yet.
Thanks

while(mySerial.read() != -1)

Isn't this a bit dangerous?
You are reading data and then discarding it. That is it is valid data that is just being thrown away, so won't that screw up the tag number?

I don't think so, cause what I want it to do is, it scans a tag, when it gets a valid code(12 digits) it performs a task. In my code it is the (found) if statement for finding it true that it exists.
Now after that, that while loop happens and cleans up any mess(that buffer/repeating problem I had) so it will clear the RFID buffer? So yes it is data, but it is data I already used. Does it make sense? Do you think there is a better way of doing it?

Thanks

You throw away the first byte of the tag you want to read. This may not be important now but it is normally a unique bit pattern to indicate the start of the package. You should be reading this and not starting to gather in a package until you see it. For flushing your buffer at the end it is fine but not at the start.

Oh I see what you mean man. Sweet. So I can just remove that while loop and then It should be perfect? :smiley:

So I can just remove that while loop and then It should be perfect?

Better, not necessarily perfect. You have two stupid loops. You need to get rid of both of them, and the String class before you can get to perfect.

Lequack:
Oh I see what you mean man. Sweet. So I can just remove that while loop and then It should be perfect? :smiley:

Getting things perfect for Paul is a much higher hurdle than for the rest of us.
But on my part it would be good to see you recognise the start byte before you read the token. :slight_smile:

Lequack:
My bad I get now what you meant by the buffer!

The program printed the card/tag a few times after it was removed(still in the buffer i guess?) that was my initial error and I fixed it by adding this while loop at the end!

#include <SoftwareSerial.h>

SoftwareSerial mySerial(11, 12);

boolean found=true;
char c;
String msg = "";

void setup() 
{
  Serial.begin(9600);
  Serial.println("Serial Ready");

mySerial.begin(9600);
  Serial.println("RFID Ready");
}

void loop()
{
  while(mySerial.read() != -1)
  {
   
    while(mySerial.available()>0){
      c=mySerial.read();
      msg += c;
      if(msg.length() == 12)
        break;
    }
   
   
    Serial.println("Gate Open");
    Serial.println("ID: "+msg);
    if(found)
    {
      msg = "";
      delay(3000);
    }
    while(mySerial.read() != -1) {}; //This one to clear the buffer?
  }
}




Many thanks!!

In my door lock program using 6300 RFID I was having the same trouble.. it would sometimes loop and unlock several times in a row for a good tag. I added your line "while(RFID.read() != -1) { } // prevent double readings" with my own notes.. after my code to send the unlock signal to the electric latch. This has fixed the double.. and sometimes triple or more readings.

Thank you!