Pages: [1]   Go Down
Author Topic: Parallax RFID Reader Integration  (Read 415 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello I am currently interfacing my Parallax RFID Reader to my Duemilenove so that I will have an RFID Garage Door Opener and I am adapting some code from the PlayGround. I have the reader connected and running. I am getting tag numbers out of it and it works great. I want to set Pin 10 high for 1.5 sec if the variable is equal to the correct tag number. The number will be hard coded in and I am having a hard time finding the variable that stores the tag number. It would be greatly appreciated if someone would clean up the code too.

Here is the code:
Code:
// Modified by Worapoht K.
#include <SoftwareSerial.h>

int  val = 0;
char code[10];
int bytesread = 0;
const int relayPin = 13;
#define rxPin 8
#define txPin 9
// RFID reader SOUT pin connected to Serial RX pin at 2400bps to pin8

void setup()
{
  Serial.begin(2400);  // Hardware serial for Monitor 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(relayPin, OUTPUT);
}


void loop()
{
  SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);
  RFID.begin(2400);

  if((val = RFID.read()) == 10)
  {   // check for header
    bytesread = 0;
    while(bytesread<10)
    {  // read 10 digit code
      val = RFID.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
      Serial.print("TAG code is: ");   // possibly a good TAG
      Serial.println(code);            // print the TAG code 
  }
    bytesread = 0;                        // wait for a second
    delay(500); 
}

   if(code  == "0800E1514A")
 {
   delay(35);
   digitalWrite(relayPin, HIGH); 
   delay(1500);
   digitalWrite(relayPin, LOW);
 }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know where you copied that code from, but it's pretty crappy code. There is no reason to create an instance of the SoftwareSerial class on every pass through loop. SoftwareSerial is obsolete. You should be using NewSoftSerial, instead.

Code:
Serial.println(code);            // print the TAG code 
Hey, look, I think we found the variable where the tag is stored.

Code:
   if(code  == "0800E1514A")
This will never be true.  The variable code is an array of characters. It is not a string (lower case s) because it is not NULL-terminated, so you can't use the strcmp function, either.

You'll need to set up a for loop, and compare each character individually. Or, properly NULL-terminate the array of characters.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok i have replaced my ASCII character comparison to a string the reader now works but, any rfid card that is scanned works. Here is my code. Why is it that any card that is scanned sets relayPin HIGH? I only want the specified card code to work.
Code:
// Modified by Worapoht K.
#include <SoftwareSerial.h>

char cardString[13] = "0800E1514A";
int  val = 0;
char code[10];
int bytesread = 0;
const int relayPin = 10;
#define rxPin 8
#define txPin 9
// RFID reader SOUT pin connected to Serial RX pin at 2400bps to pin8

void setup()
{
  Serial.begin(2400);  // Hardware serial for Monitor 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(relayPin, OUTPUT);
}


void loop()
{
  SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);
  RFID.begin(2400);

  if((val = RFID.read()) == 10)
  {   // check for header
    bytesread = 0;
    while(bytesread<10)
    {  // read 10 digit code
      val = RFID.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
      Serial.print("TAG code is: ");   // possibly a good TAG
      Serial.println(code);            // print the TAG code 
  }
    bytesread = 0;                        // wait for a second
    delay(500); 
}
   if(code[10] == cardString[13])
 {
   delay(35);
   digitalWrite(relayPin, HIGH); 
   delay(1500);
   digitalWrite(relayPin, LOW);
 }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why is it that any card that is scanned sets relayPin HIGH?
Maybe because of this:
Code:
if(code[10] == cardString[13])
The value in code[10] is undefined, because the array only holds 10 elements, in positions 0 thru 9. The value in cardString[13] is similarly undefined, because the array only holds 13 elements, in positions 0 thru 12.

If you want to compare two strings (lower case s/arrays of chars), you must use strvmp(), and the strings must be the same length.
Logged

Pages: [1]   Go Up
Jump to: