Pages: [1]   Go Down
Author Topic: RFID Repeating?  (Read 1121 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
http://www.instructables.com/id/Arduino-and-RFID-from-seeedstudio/?ALLSTEPS

This is my code
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
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4407
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

Code:
#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!!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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*
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4407
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh I see what you mean man. Sweet. So I can just remove that while loop and then It should be perfect? smiley-grin
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.  smiley 
Logged

Pages: [1]   Go Up
Jump to: