loading

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 <http://people.interaction-ivrea.it/h.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


Jayefuu7 years ago
I'm a bit rusty on my C++. You could try changing the type of tag1 and tag2 to string and setting them to '0F030380A2' and '0415ED4211'. I know strings are arrays of chars, but I can't remember if the string class will let you use arrays of chars or whether it's expecting variables of type string. Should only take you a sec to change and test so worth a try!
Jayefuu Jayefuu7 years ago
Looking here, these use arrays of chars so what I suggested isn't it.
JaredsProjects (author)  Jayefuu7 years ago
I asked it on the arduino forum and got the answer.  I had to make the tags strings and had to terminate the strings for it to work.

AI