I just got a SM130 RFID and a matching shield from Sparkfun. I'm trying to figure out the coding for it. I'm a newb when it comes to RFID, and fairly new to programing. I can get it to read the RFID transponder (mainly because of the example code provided by Sparkfun, but I understand what it's doing.) Any advice, or direction as to further reading/examples would be appreciated. I need to make it so it not only reads the transponder, but only responds to certain ones.
I can get it to read the RFID transponder (mainly because of the example code provided by Sparkfun, but I understand what it's doing.)
Post that example code, and we'll help you understand it.
I need to make it so it not only reads the transponder, but only responds to certain ones.
Quite easy. See above.
Thanks. The code is as follows.
/*
RFID Eval 13.56MHz Shield example sketch v10
Aaron Weiss, aaron at sparkfun dot com
OSHW license: http://freedomdefined.org/OSHW
works with 13.56MHz MiFare 1k tags
Based on hardware v13:
D7 -> RFID RX
D8 -> RFID TX
D9 -> XBee TX
D10 -> XBee RX
Note: RFID Reset attached to D13 (aka status LED)
Note: be sure include the NewSoftSerial lib, http://arduiniana.org/libraries/newsoftserial/
Usage: Sketch prints 'Start' and waits for a tag. When a tag is in range, the shield reads the tag,
blinks the 'Found' LED and prints the serial number of the tag to the serial port
and the XBee port.
*/
#include <SoftwareSerial.h>
SoftwareSerial rfid(7, 8);
SoftwareSerial xbee(10, 9);
//Prototypes
void check_for_notag(void);
void halt(void);
void parse(void);
void print_serial(void);
void read_serial(void);
void seek(void);
void set_flag(void);
//Global var
int flag = 0;
int Str1[11];
//INIT
void setup()
{
Serial.begin(9600);
Serial.println("Start");
// set the data rate for the NewSoftSerial ports
rfid.begin(19200);
delay(10);
halt();
}
//MAIN
void loop()
{
read_serial();
}
void check_for_notag()
{
seek();
delay(10);
parse();
set_flag();
if(flag = 1){
seek();
delay(10);
parse();
}
}
void halt()
{
//Halt tag
rfid.write(255);
rfid.write(byte(0));
rfid.write(1);
rfid.write(147);
rfid.write(148);
}
void parse()
{
while(rfid.available()){
if(rfid.read() == 255){
for(int i=1;i<11;i++){
Str1[i]= rfid.read();
}
}
}
}
void print_serial()
{
if(flag == 1){
//print to serial port
Serial.print(Str1[8], HEX);
Serial.print(Str1[7], HEX);
Serial.print(Str1[6], HEX);
Serial.print(Str1[5], HEX);
Serial.println();
delay(100);
//check_for_notag();
}
}
void read_serial()
{
seek();
delay(10);
parse();
set_flag();
print_serial();
delay(100);
}
void seek()
{
//search for RFID tag
rfid.write(255);
rfid.write(byte(0));
rfid.write(1);
rfid.write(130);
rfid.write(131);
delay(10);
}
void set_flag()
{
if(Str1[2] == 6){
flag++;
}
if(Str1[2] == 2){
flag = 0;
}
}
I assume that if I add another prototype, as the writer of the code called it, called void password (void) I could stick the code in there for recognizing or denying certain Mifare tags.
Please modify your post, and change the quote tags to code tags. I really doubt that the device is attached to the smiley face pin on the Arduino.
The tag that was read is stored in Str1, which is a crappy name for an array of ints.
The parse function is crap. It assumes that all the data will be available to be read at once. That is not a valid assumption, generally.
I assume that if I add another prototype, as the writer of the code called it, called void password (void) I could stick the code in there for recognizing or denying certain Mifare tags.
Yes. Compare the collection of ints, Str1, with other collections of ints, one at a time, to determine if the collections match.
The function really should return a bool (true means the tag is approved; false means not approved), rather than nothing (void).
Thanks. What can I use in place of the parse function?
What can I use in place of the parse function?
RFID readers typically send a start marker and an end marker. You need to see what serial data is available, and discard everything until the start marker arrives. Then store every after than until the end marker arrives. When that happens, set a flag that says that the packet is complete.
Don't use the data unless the flag is set.
You might be wondering what the start and end markers are. Well, so am I. You'll simply need to print each character received, as an int, as you scan the tag, and see what characters do not correspond to characters written on the tag. Those will be the start and end markers.
if(flag = 1){
This is an assignment statement, not an equality test.