Intro and RFID Question

Hey guys, my name’s Adam, and I’m from NJ. I work at the Shack, and I kept hearing people talk about the Arduino, so I figured I’d get one to play around with =)

I just received my Duemilanove in the mail today and hooked it up to a Parallax RFID tag reader. I found some source code for a basic sketch to play around with, and I’m trying to tweak it some.

Basically I’m trying to just read the tag, figure out if it is indeed the set ID #, if so, say “VALID”, and if not, “INVALID” Here’s my code and error at the moment (I’ve tried multiple ways of doing this, none have worked)

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


int  val = 0; 
char code[];
int bytesread = 0; 

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
}  


 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(code == 0415DA38B1)
          {
            Serial.print("TAG code is VALID ");   // possibly a good TAG    
          }
        else
          {
            Serial.print("TAG code is INVALID ");
          }
         Serial.println(code);            // print the TAG code 
      } 
      bytesread = 0; 
           delay(500);                       // wait for a second 
    } 
  } 
}
24: error: invalid suffix "DA38B1" on integer constant
            if(code == 0415DA38B1)

The variable code is declared to be a character array, of size 0 (since it is not explicitly sized or initialized).

Into this array, you are trying to put 10 characters, They won't fit, of course.

Then, you are trying to compare this array to in integer. Of course, the compiler is going to complain.

You need to use strcmp to compare strings. The first problem is that 0415DA38B1 is not a string. "0415DA38B1" is, though, so you are in luck. The second problem is that code is not a string, either.

The difference between a string and an array of characters is that a string is an array of characters that is NULL terminated. Your array of characters is not NULL terminated, therefore it is a string.

Change this:

          code[bytesread] = val;         // add the digit          
          bytesread++;                   // ready to read next digit

to this:

          code[bytesread] = val;         // add the digit          
          bytesread++;                   // ready to read next digit  
          code[bytesread] = '\0'; // NULL terminate the string

to make code a string.

Okay, thanks. Where is the reference on using strcmp?

Where is the reference on using strcmp?

Perhaps you've heard of my friend, Google?

I’m getting no readout with

code[bytesread] = '\0'; // NULL terminate the string

Here’s my code (Google doesn’t explain strcmp very well):

int  val = 0;
char code[10];
int bytesread = 0;


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
}  


 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 
          code[bytesread] = '\0'; // NULL terminate the string 
        }
      }
      if(bytesread == 10)
        {              // if 10 digit read is complete
            if(strcmp (code,"0415DA38B1") == 0)
             {
            Serial.print("TAG code is VALID ");   // possibly a good TAG    
             }
        else
             {
            Serial.print("TAG code is INVALID ");
             }
         Serial.println(code);            // print the TAG code
      }
      bytesread = 0;
           delay(500);                       // wait for a second
    }
  }
}

The code array should be 11 bytes long, to accommodate the NULL on the end. The example doesn't size the array properly. It's not the best example of code around.

What values are you getting printed? It might be helpful if you printed useful text before the values you print:

Serial.print("Scanned tag: [");
Serial.print(code);Serial.println("]");

Ah, perfect. Got it working just fine now. Thanks a lot for the help! Now I just have to figure out where to go from here. Do you guys recommend any fun projects in difficulty scales? (Eg, try this one, then that one next, etc.)