Parallax RFID reader not reading card

Hi,

I’m trying to build a safe that opens when the correct card is read by an RFID reader. For some reason, the reader refuses to read the card even when it is enabled (the LED is red). Sometimes it will read the card right after uploading a new sketch (but not after resetting the board), but after the initial reading, it will no longer read cards. Can someone please give me some guidance on how to make it work?

Thanks

#include <Servo.h>
#include <EEPROM.h>
#include “EEPROMAnything.h”
int val = 0;
char code[11]; //the code read by the RFID reader
char validCode [11]; //code read from EEPROM is stored here
int bytesread = 0;
#define buttonPin 10
int lockState = 0;
unsigned long time;
Servo safeServo;

void setup(){
Serial.begin (2400);
pinMode(2,OUTPUT); //“enable” pin for RFID reader
pinMode (A1,OUTPUT); //LED
pinMode (buttonPin, INPUT);
digitalWrite(2, LOW);
EEPROM_readAnything (0, validCode); //assign the code stored in EEPROM to “validCode”
safeServo.attach (9);
safeServo.write (85);
}

bool readRFID(){
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
bytesread = 0;
digitalWrite(2, HIGH); // deactivate the RFID reader for a moment so it will not flood
delay(1500); // wait for a bit
digitalWrite(2, LOW); // Activate the RFID reader
return true; //returns “true” if a card is successfully read
}
}
}
return false; //returns “false” if a card is not successfully read
}

void openSafe()
{
safeServo.write (0);
delay (1000);
}

void closeSafe()
{
safeServo.write (180);
delay (1000);
}

void loop() {
switch (lockState)
{
case 0: //Locked State
digitalWrite (2, LOW);
digitalWrite (A1, LOW); // turn the LED off
if (digitalRead (buttonPin) == LOW) // if the button is pressed
{
time = millis(); //stores the amount of time since the program started in the variable “time”
lockState = 2;
digitalWrite (A1, HIGH); // turn the LED on
break;
}
else if(readRFID()== true) //if button is not pressed but a card is read
{
if (strcmp (code, validCode) == 0) // if the code on the card and the valid code match
{
openSafe();
lockState = 1;
break;
}
}

break;
case 1: //Unlocked State
digitalWrite (2, HIGH); // deactivate the RFID reader
Serial.println (“Unlocked”);
delay (3000);
Serial.println (“Locked”);
digitalWrite (2, LOW); //enable the reader
closeSafe();
lockState = 0;
break;
case 2: //Reprogram Mode
if (time + 5000 <= millis()) // if five seconds have elapsed since entering reprogram mode
{
Serial.println (“Locked”);
lockState = 0;
break;
}

else if (readRFID()) //if a card is read before 5 seconds have elapsed
{
//EEPROM_writeAnything (0, code); //write the code on the card to EEPROM
Serial.println (“Locked”);
lockState = 0;
break;
}
//break;
}
}

sorry...posted twice by accident. please respond to the thread at: http://forum.arduino.cc/index.php?topic=262821.0

thanks

No, remove the other thread yourself and use this one. Post the link to the said RFID reader, your hookup with pictures.

I can't delete it. I keep getting an error: "You cannot delete your own topics in this board." Also, the RFID reader in question is: RFID Card Reader - Serial - Parallax. I'm not posting pictures of the setup because I know it is not at fault; it does work sometimes (as described in the original question), and it works with other sketches

You should read the sample code. In the sample code, a zero is inserted into the end of the array containing the 10-character sequence. Where is that in your code?

Curious. I don't know about the flooding argument. I don't have an RFID reader. If you place an RFID on it, does it keep reporting what it reads?

I'm not sure how well you hooked up your circuits. It's one of the standard things I ask when someone has a circuit that is not working. If you did everything including circuit and code correctly, you wouldn't be here. You may be an expert in building circuits but without showing a picture, we all think you made a spaghetti monster. :stuck_out_tongue:

For the duplicate post, press report to moderator and kindly ask them to remove it. They'd be happy to help.

I didn't understand the example code. However, the code in my program is adapted from here: http://playground.arduino.cc/Learning/PRFID. I know that part of my code (the one that actually reads the card) is sound because the program used to work and I didn't touch that part of it (I must have messed something else up).

In terms of flooding, I believe it does happen, but if you'll notice, my program deactivates the reader after reading a card to prevent just such an occurrence.

I tried taking pictures of my wiring, but I can't seem to get a good picture; I'm no wiring expert, but I know that the wiring is sound (I don't mean to seem stubborn).

What I really don't understand is why the program would only work sometimes (and that too only after re-uploading the sketch). nice profile pic btw, liudr :slight_smile:

Other thread removed.