I have a parallax RFID reader hooked up to the Arduino Duemilanove. For the past hour I've been trying to set up a system where the arduino will send a valid/invalid signal via usb to a computer based on whether the tag code is in an array in the program or not. The full code follows.
/**
* author Benjamin Eckel
* date 10-17-2009
*
*/
#define RFID_ENABLE 2 //to RFID ENABLE
#define CODE_LEN 10 //Max length of RFID tag
#define VALIDATE_TAG 1 //should we validate tag?
#define VALIDATE_LENGTH 200 //maximum reads b/w tag read and validate
#define ITERATION_LENGTH 2000 //time, in ms, given to the user to move hand away
#define START_BYTE 0x0A
#define STOP_BYTE 0x0D
char tag[CODE_LEN];
void setup() {
Serial.begin(2400);
pinMode(RFID_ENABLE,OUTPUT);
}
void loop() {
enableRFID();
getRFIDTag();
if(isCodeValid()) {
disableRFID();
sendCode();
delay(ITERATION_LENGTH);
}
else {
disableRFID();
Serial.println("Got some noise");
}
Serial.flush();
clearCode();
}
/**
* Clears out the memory space for the tag to 0s.
*/
void clearCode() {
for(int i=0; i<CODE_LEN; i++) {
tag[i] = 0;
}
}
/**
* Sends the tag to the computer.
*/
void sendCode() {
int card=1; // if card = 1 then the rfid tag being scanned is valid. If card = 0 then it is invalid.
int i=0; // i is the number of times the next while loop has completed.
int card1[10]; //this is the accepted RFID tag. card1[0]-card1[9] is the individual digits of the tag.
card1[0]=2;
card1[1]=2;
card1[2]=0;
card1[3]=0;
card1[4]=5;
card1[5]=2;
card1[6]=7;
card1[7]=6;
card1[8]=9;
card1[9]=0;
Serial.print("TAG:");
while(i<CODE_LEN){ //i is less than max tag length
Serial.print(tag[i]); //print out stored tag next to read tag.
Serial.print(card1[i]);
if(tag[i]!=card1[i]){ //if the read tag is different from the stored tag then an invalid tag has been read and an error will show.
Serial.print("ERROR:"); //print out error and show which digits don't match.
Serial.print(tag[i]);
Serial.print(card1[i]);
card=0; //set tag as invalid
i++; //increment i by 1
}
else
i++; //increment i by 1
}
Serial.print('\n'); //print new line
if(card==0){ //if invalid print Not Valid
Serial.print("Not Valid");
}
else if(card==1){ //if valid, print Card Is Valid
Serial.print("Card Is Valid");
}
}
/**************************************************************/
/******************** RFID Functions ***********************/
/**************************************************************/
void enableRFID() {
digitalWrite(RFID_ENABLE, LOW);
}
void disableRFID() {
digitalWrite(RFID_ENABLE, HIGH);
}
/**
* Blocking function, waits for and gets the RFID tag.
*/
void getRFIDTag() {
byte next_byte;
while(Serial.available() <= 0) {
}
if((next_byte = Serial.read()) == START_BYTE) {
byte bytesread = 0;
while(bytesread < CODE_LEN) {
if(Serial.available() > 0) { //wait for the next byte
if((next_byte = Serial.read()) == STOP_BYTE) break;
tag[bytesread++] = next_byte;
}
}
}
}
/**
* Waits for the next incoming tag to see if it matches
* the current tag.
*/
boolean isCodeValid() {
byte next_byte;
int count = 0;
while (Serial.available() < 2) { //there is already a STOP_BYTE in buffer
delay(1); //probably not a very pure millisecond
if(count++ > VALIDATE_LENGTH) return false;
}
Serial.read(); //throw away extra STOP_BYTE
if ((next_byte = Serial.read()) == START_BYTE) {
byte bytes_read = 0;
while (bytes_read < CODE_LEN) {
if (Serial.available() > 0) { //wait for the next byte
if ((next_byte = Serial.read()) == STOP_BYTE) break;
if (tag[bytes_read++] != next_byte) return false;
}
}
}
return true;
}
I'm borrowing the bulk of the code from Benjamin Eckel who gave a really good tutorial on hooking up the arduino and parallax RFID reader.
I added in this part for my own particular needs:
void sendCode() {
int card=1; // if card = 1 then the rfid tag being scanned is valid. If card = 0 then it is invalid.
int i=0; // i is the number of times the next while loop has completed.
int card1[10]; //this is the accepted RFID tag. card1[0]-card1[9] is the individual digits of the tag.
card1[0]=2;
card1[1]=2;
card1[2]=0;
card1[3]=0;
card1[4]=5;
card1[5]=2;
card1[6]=7;
card1[7]=6;
card1[8]=9;
card1[9]=0;
Serial.print("TAG:");
while(i<CODE_LEN){ //i is less than max tag length
Serial.print(tag[i]); //print out stored tag next to read tag.
Serial.print(card1[i]);
if(tag[i]!=card1[i]){ //if the read tag is different from the stored tag then an invalid tag has been read and an error will show.
Serial.print("ERROR:"); //print out error and show which digits don't match.
Serial.print(tag[i]);
Serial.print(card1[i]);
card=0; //set tag as invalid
i++; //increment i by 1
}
else
i++; //increment i by 1
}
Serial.print('\n'); //print new line
if(card==0){ //if invalid print Not Valid
Serial.print("Not Valid");
}
else if(card==1){ //if valid, print Card Is Valid
Serial.print("Card Is Valid");
}
}
My problem is that whenever I test a valid tag, it always comes back invalid for every single digit regardless of whether it is actually equal to the stored tag or not. I finally ended up narrowing down the error to the first if statement (I think) but I haven't been able to determine what is actually messing it up. This is probably due to the fact that I haven't done any serious coding since I took my first programming class about six months ago. Can someone look through the code and try to help me out? Thanks!