Hello
Every time i sweeps my rfid tag in front the parallax rfid reader then arduinon reacts correctly. 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 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.