Very strange problem with my rfid reader.

Hello

Every time i sweeps my rfid tag in front the parallax rfid reader then arduinon reacts correctly. Only if I'm fast :slight_smile:
But if I happen to keep my RFID tag in front of RFID reader for as long as about three seconds and then remove it, the arduinon believe that I still have the RFID Tag in front RFID reader and repeat the function within the IF argument.

As you can see below is the if argument that activates when the right rfid tag is sweped, and I have about 9 sec to see a reaction and remove the RFID tag or it will repeat.

In this example as I sweep in front of RFID tag reader for about 3 seconds and only once, but with this result below.

The following sequence happens:
Magnet closed then lock the door!
Magnet closed then lock the door!
Magnet closed then lock the door!
Right key!
0800E11A3A
Magnet closed then lock the door!
Magnet closed then lock the door!
Magnet closed then lock the door!
Right key!
0800E11A3A
Magnet closed then lock the door!
Magnet closed then lock the door!
Magnet closed then lock the door!
Right key!
0800E11A3A
....

The IF argument:

        if(strcmp(code, tag) == 0){      // compare tag with code
                   
                                      Serial.println("Right key!");
                                      Serial.println(code);

                                      myservo.write(120);                    // position the servo angle at 90 degrees
              
                                      digitalWrite(ledGrn, HIGH);   
              
                                      delay(5000);
                        
                                      buttonState = digitalRead(buttonPin);  // check if the pushbutton is pressed.                                       
                                      if (buttonState == HIGH)               // if it is, the buttonState is HIGH:
                                      {
                                        
                                      delay(3000);
                                      Serial.println("Magnet closed then lock the door!");
                                      myservo.write(0);                      // position the servo angle at 90 degrees   
                                      digitalWrite(ledGrn, LOW);
                                      delay(1000);
                                      
                                      }    
                                  }

The entire code:

#include <Servo.h> 
Servo myservo;
int pos = 0;

char tag[12] = {'0', '8', '0', '0', 'E', '1', '1', 'A', '3', 'A'};
char code[12];

int  val = 0;  
int bytesread = 0; 

int ledRed =  7;             // red led
int ledGrn =  12;            // green led

const int buttonPin = 4;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  
Serial.begin(2400);          // RFID reader SOUT pin connected to Serial RX pin at 2400bps 
pinMode(2,OUTPUT);           // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin 
digitalWrite(2, LOW);        // Activate the RFID reader

  myservo.attach(9);         // attaches the servo on pin 9 to the servo object 

  pinMode(ledRed, OUTPUT);
  pinMode(ledGrn, OUTPUT);
  
  pinMode(buttonPin, INPUT);    // initialize the pushbutton pin as an input
  
             }  


 void loop() { 

  if(Serial.available() > 0) {           // if data available from reader 

    if((val = Serial.read()) == 10) {    // check for header 
      bytesread = 0; 
      while(bytesread<10)                // read 10 digit code 
       {
        if( Serial.available() > 0) 
         { 
          val = Serial.read(); 
          if((val == 10)||(val == 13))   // if header or stop bytes before the 10 digit reading 
          {
            break;                       // stop reading 
          } 
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit  
        } 
      } 
      if(bytesread == 10)                // if 10 digit read is complete 
      {
           
        if(strcmp(code, tag) == 0){      // compare tag with code
                   
                                      Serial.println("Right key!");
                                      Serial.println(code);

                                      myservo.write(120);                    // position the servo angle at 90 degrees
              
                                      digitalWrite(ledGrn, HIGH);   
              
                                      delay(5000);
                        
                                      buttonState = digitalRead(buttonPin);  // check if the pushbutton is pressed.                                       
                                      if (buttonState == HIGH)               // if it is, the buttonState is HIGH:
                                      {
                                        
                                      delay(3000);
                                      Serial.println("Magnet closed then lock the door!");
                                      myservo.write(0);                      // position the servo angle at 90 degrees   
                                      digitalWrite(ledGrn, LOW);
                                      delay(1000);
                                      
                                      }    
                                  }

              else {
        
                      Serial.println("Wrong key!");
                      Serial.println(code);
                      delay(1000);
              
                   }
    
      }
      
                        bytesread = 0;  
                        delay(500);            // wait for a second 

                                  } 
                             }
 
       buttonState = digitalRead(buttonPin);  // check if the pushbutton is pressed.                                                  
       if (buttonState == HIGH)               // if it is, the buttonState is HIGH:     
       {  
          delay(3000);
          Serial.println("Magnet closed then lock the door!");
          myservo.write(0);  // position the servo angle at 90 degrees
          digitalWrite(ledGrn, LOW);
          
       }
       
       buttonState = digitalRead(buttonPin);  // check if the pushbutton is pressed.                                                  
       if (buttonState == LOW)                // if it is, the buttonState is HIGH:     
       {   
          Serial.println("Magnet open then open the door!");
          myservo.write(120);                 // position the servo angle at 90 degrees
          digitalWrite(ledGrn, HIGH);
          
       }

              }

I hope you understand my problem. And I apologize for my bad English.

The random indents and curly braces not lines up make your code hard to read.

Are there lights on the reader that flash when a card is read? If so, do the lights stay on, flash more then once, or flash just once?

Only if I'm fast. But if I happen to keep my RFID tag in front of RFID reader for as long as about three seconds

Three seconds is a long time. No where near fast. Closer to really, really slow, actually.

Now I've refined the code a bit.
Yes there is an LED on the reader, but it is constantly lit red.

PaulS:
"Three seconds is a long time. No where near fast. Closer to really, really slow, actually."

Yes it is. But I did not mean that three seconds was fast. Without that feature worked as it would if I was fast.

My RFID reader has two LEDs. One is a power indicator, and is lit whenever the RFID has power. The other is a "working" indicator, and flashes whenever a tag is scanned.

The number of times that LED flashes and the number of times that the code reports a tag read should match.

Of course, if you don't have the "working" LED, it's harder to troubleshoot the system.

Okey.
I have been following two examples in the link below. And they all give me just a red light on the RFID reader.
http://www.arduino.cc/playground/Learning/PRFID

Is it a parallax you also have?

No. I have a different brand RFID reader.

The way I would debug this is to put a Serial.print statement after reading the tag, to print the tag read, and remove all the rest of the code.

See if scanning the tag prints more than one message. It shouldn't.

What to do next depends on whether the message prints once, or more than once.

I did this like you said and discovered that there is something wrong with the original code at the arduino page.
I put a delay after reading the hold of 5 sec, after holding the tag in about 3 sec I removed it. The Serial.print give me massage in about 10 seconds after removing it.

Very strange. Can you not just "empty" code variable or something?

I put a delay after reading the hold of 5 sec, after holding the tag in about 3 sec I removed it. The Serial.print give me massage in about 10 seconds after removing it.

A massage 10 seconds after removing the tag? I like it. I need a parallax RFID reader, I think.

"after reading the hold..." ???

Can you not just "empty" code variable or something?

Yes, if you do it right. Post your code, again, please.

Yes, messages 10 sec after removing the tag. Is it just me with this problem? :-/

Here is the code:

#include <Servo.h> 
Servo myservo;
int pos = 0;

char tag[12] = {'0', '8', '0', '0', 'E', '1', '1', 'A', '3', 'A'};
char code[12];

int  val = 0;  
int bytesread = 0; 

int ledRed =  7;             // red led
int ledGrn =  12;            // green led

const int buttonPin = 4;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  
Serial.begin(2400);          // RFID reader SOUT pin connected to Serial RX pin at 2400bps 
pinMode(2,OUTPUT);           // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin 
digitalWrite(2, LOW);        // Activate the RFID reader

  myservo.attach(9);         // attaches the servo on pin 9 to the servo object 

  pinMode(ledRed, OUTPUT);
  pinMode(ledGrn, OUTPUT);
  
  pinMode(buttonPin, INPUT);    // initialize the pushbutton pin as an input
  
             }  


 void loop() { 

  if(Serial.available() > 0) {           // if data available from reader 

    if((val = Serial.read()) == 10) {    // check for header 
      bytesread = 0; 
      while(bytesread<10)                // read 10 digit code 
       {
        if( Serial.available() > 0) 
         { 
          val = Serial.read(); 
          if((val == 10)||(val == 13))   // if header or stop bytes before the 10 digit reading 
          {
            break;                       // stop reading 
          } 
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit  
        } 
      } 
      if(bytesread == 10)                // if 10 digit read is complete 
      {
           
        if(strcmp(code, tag) == 0){      // compare tag with code
                   digitalWrite(2, HIGH);             // activate RFID reader 

                                      Serial.println("Right key!");
                                      Serial.println(code);

                                      myservo.write(120);                    // position the servo angle at 90 degrees
              
                                      digitalWrite(ledGrn, HIGH);   
              
                                      delay(5000);

                                      buttonState = digitalRead(buttonPin);  // check if the pushbutton is pressed.                                       
                                      if (buttonState == HIGH)               // if it is, the buttonState is HIGH:
                                      {
                                        
                                      delay(3000);
                                      Serial.println("Magnet closed then lock the door!");
                                      myservo.write(0);                      // position the servo angle at 90 degrees   
                                      digitalWrite(ledGrn, LOW);
                                      delay(1000);
                                      
                                      }    
                                  }

              else {
        
                      Serial.println("Wrong key!");
                      Serial.println(code);
                      delay(1000);
              
                   }
    
      }
      
                        bytesread = 0;  
                        delay(500);            // wait for a second 
                        digitalWrite(2, HIGH);             // deactivate RFID reader 
                                  } 
                             }
 
       buttonState = digitalRead(buttonPin);  // check if the pushbutton is pressed.                                                  
       if (buttonState == HIGH)               // if it is, the buttonState is HIGH:     
       {  
          delay(3000);
          Serial.println("Magnet closed then lock the door!");
          myservo.write(0);  // position the servo angle at 90 degrees
          digitalWrite(ledGrn, LOW);
          
       }
       
       buttonState = digitalRead(buttonPin);  // check if the pushbutton is pressed.                                                  
       if (buttonState == LOW)                // if it is, the buttonState is HIGH:     
       {   
          Serial.println("Magnet open then open the door!");
          myservo.write(120);                 // position the servo angle at 90 degrees
          digitalWrite(ledGrn, HIGH);
          
       }

              }

I said to remove all the processing of the tag and button readings. All you want to do is read the tag and print a message. Right now, there are many places for bugs to hide. We want to remove a lot of them, to see if that removed the bug, too.

I figured out the fix it, but I can't post it because I'm new and I need to burn my first message so I can put my code on here.

I don't know if anyone else has this problem but I did and it was frustrating. I am using the parallax RFID reader and connected it through serial to my arduino just like the Arduino Playground suggested to:

http://www.arduino.cc/playground/Learning/PRFID

On the above page it shows that you can change the state of the RFID reader pin called "Enable" to LOW to activate the reader. The code available has the reader enabled all of the time. To stop the repeating when you swipe the RFID, I changed the state of pin 2 to high (disabling the reader) right after the first "If" statement.

// RFID reader for Arduino 
// Wiring version by BARRAGAN <http://people.interaction-ivrea.it/h.barragan> 
// Modified for Arudino by djmatic


int  val = 0; 
char code[11]; 
int bytesread = 0; 
char pass[11] = {'0','F','0','3','0','2','9','F','6','5'};
int ledgPin = 8;
int ledrPin = 9;


void setup() { 

Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps 
pinMode(2,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin 
digitalWrite(2, LOW);                  // Activate the RFID reader
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);

}  


 void loop() { 
   
    digitalWrite(2, LOW);
  if(Serial.available() > 0) {    // if data available from reader 
  
  [glow]  digitalWrite(2, HIGH);[/glow]

    if((val = Serial.read()) == 10) {   // check for header 
      bytesread = 0; 
      while(bytesread<10) {              // read 10 digit code 
        if( Serial.available() > 0) { 
          val = Serial.read(); 
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading 
            break;                       // stop reading 
          } 
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit  
        } 
      } 
      if(bytesread == 10) {              // if 10 digit read is complete 
        if(strcmp(code, pass) == 0) {
          digitalWrite(ledgPin, HIGH);
          delay(1500);
          digitalWrite(ledgPin, LOW);
        }
        else {
          digitalWrite(ledrPin, HIGH);
          delay(300);
          digitalWrite(ledrPin, LOW);
          delay(300);
          digitalWrite(ledrPin, HIGH);
          delay(300);
          digitalWrite(ledrPin, LOW);
          delay(300);
          digitalWrite(ledrPin, HIGH);
          delay(300);
          digitalWrite(ledrPin, LOW);
          delay(300);
        }
          
        Serial.print("TAG code is: ");   // possibly a good TAG 
        Serial.println(code);            // print the TAG code 
      } 
      bytesread = 0; 
           delay(500);                       // wait for a second 
    } 
  } 
  
}

Don't forget to change the state back to LOW just before the IF statement, as seen in the coding above.
Have Fun!