Arduino not recognizing certain RFID tags?

I have looked at some RFID lock programs, so that I can make own. Most of them use strcmp to compare the correct tag to the one that has been read. That is what it did in the program below (modified from the program on the arduino playground) , but nothing happens when I put a tag in front of the reader. For one tag “led1” is supposed light up and “led2” for the other. Is the tag not defined right? Did I not use strcmp correctly?

Please help!

// RFID reader for Arduino
// Wiring version by BARRAGAN
// Modified for Arudino by djmatic

int val = 0;
char code[10];
int bytesread = 0;
char tag1[10] = {‘0’,‘F’,‘0’,‘3’,‘0’,‘3’,‘8’,‘0’,‘A’,‘2’}; //define tag 1
char tag2[10] = {‘0’,‘4’,‘1’,‘5’,‘E’,‘D’,‘4’,‘2’,‘1’,‘1’}; //define tag 2
const int led1 = 12;
const int led2 = 13;

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(led1,OUTPUT);
pinMode(led2,OUTPUT);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
}

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, tag1) == 0) //compare tag1 to the tag read
{
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);
}
if(strcmp(code, tag2) == 0) //compare tag2 to the tag read
{
digitalWrite(led2, LOW);
digitalWrite(led1, HIGH);
}

}
bytesread = 0;
delay(500); // wait for a second
}
}
}

Thanks in advance!

AI

Did I not use strcmp correctly?

You did.

Is the tag not defined right?

You are the only one that can answer that. You have the tags. We don't.

Which RFID reader do you have? Does it have an LED that indicates that a tag was read?

Add another LED. Light it up when a tag is recognized, valid or not. That will tell you whether the tag was read.

If you had a serial LCD, you could connect it, and use NewSoftSerial to communicate with it. Then, you could debug the program much more easily.

I am using the parallax rfid reader. The leds indicate if the tag was read correctly.

You’re not using strcmp correctly - you’re using it to compare character arrays, not strings.
You may get away with using “strncmp”, or better still, terminate your tags and your incoming string.

What does terminate your tags and incoming string mean?

sorry I am somewhat new to arduino.

Also when I change strcmp to strncmp it wouldn't compile

A C string is an array of characters, with a zero (that's a real zero, not an ASCII '0') in the last element. This is called termination. It means your ten element arrays needs to be eleven elements long, to contain the data AND the terminator.

Thank You AWOL!!! I terminated them and now it works perfect!!! Thank you for all of your help and your quick responses!

AI

artificialintel,

There is an easier way to define an array containing a string:

char tag1[] = "0F030380A2"; //define tag 1
char tag2[] = "0415ED4211"; //define tag 2

By doing it this way, the compiler will figure out how long the array needs to be, and will automatically add the NUL terminator.

Regards,

-Mike

artificialintel, I hope you don't mind if i hijack you're thread; I'm trying to do exactly what you are, and it seems as if you've already figured it out! First, I'm using the ID-12 reader and your code that you posted here. I've gotten my two RFID tag codes by using this code

/* RFID ID12
*/

char val = 0; // variable to store the data from the serial port

void setup() {
Serial.begin(9600); // connect to the serial port
}

void loop () {
// read the serial port

if(Serial.available() > 0) {
val = Serial.read();
Serial.print(val, BYTE);

}
}

Their numbers are 2B0029AFB31E and 2B0029AB349D. So, I've replaced your tags with mine, like this:

char tag1[13] = {'2','B','0','0','2','9','A','F','B','3','1','B','\0'}; //define tag 1
char tag2[13] = {'2','B','0','0','2','9','A','B','3','4','9','D','\0'}; //define tag 2

I added the NULL character to the end, just as AWOL suggested. I also increased my index by three to account for the NULL as well as the two extra characters that my RFID tags have compared to yours. I then replaced every instance of the number 10 in your code with the number 13. I also changed the 13 to a 16 (stab in the dark :) ) in this line :

 if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
           break;                       // stop reading
         }

so now it looks like this:

if((val == 13)||(val == 16)) { // if header or stop bytes before the 10 digit reading
           break;            // stop reading
         }

When I scan my tags, nothing happens! I even used Mike Murdock's suggestion of defining a string, no luck... Can anyone please tell me what am I missing? Thanks in advance!

BriGuy,
This code has been made from some I found on the arduino playground, and from the help I have had here. It accepts two different tags to make a green led turn on. Put what your tags are in where mine are. I don’t remember exactly how it worked, it made sense at the time, but you should be able to modify it to make it do what you want.

int val = 0;
char code[11];
int bytesread = 0;
char tag1[11] = {‘0’,‘F’,‘0’,‘3’,‘0’,‘3’,‘8’,‘0’,‘A’,‘2’}; //define tag 1
char tag2[11] = {‘5’,‘0’,‘0’,‘0’,‘4’,‘9’,‘6’,‘3’,‘D’,‘E’};
const int led1 = 12;
const int led2 = 13;

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(led1,OUTPUT);
pinMode(led2,OUTPUT);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);

}

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
code[11] = 0;
if(strcmp(code, tag1) == 0 || strcmp(code, tag2)==0) //compare tag1 to the tag read
{
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);

}
}
if(strcmp(code, tag1) != 0 && strcmp(code, tag2) != 0)
{

digitalWrite(led2, LOW);
digitalWrite(led1, HIGH);
}

}
bytesread = 0;
delay(500); // wait for a second
}
}

My codes are 12 numbers long (code: 2B0029AFB31E). I am getting these numbers from the code I mentioned above. It doesn't work if I simply plug my numbers in where yours are; the compiler complains (error: too many initializers for 'char [11]'). I think I am getting confused with the fact that all of the RFID examples I find on this site use 10 digit RFID tags and mine are 12. I can't plug my tags into any example, nor can I trim the first two, or last two characters off my tags and plug that number in. Is there some kind of hidden data within my tags that isnt being displayed? I am just not too familiar with RFID. Can anyone point me in the right direction? Thanks!

Here's some code I found and modified, it uses the id20 reader. It stores codes in the arduinos EEPROM and evaluates them from there. There's a master key hard coded, when it's read the next card is stored in EEPROM to be accepted.

http://jrowarduino.blogspot.com