Hi Guys,
I have here a Parallax RFID Read/Write Module: http://www.parallax.com/Store/Accessories/CommunicationRF/tabid/161/ProductID/688/List/0/Default.aspx?SortField=ProductName,ProductName
and a EM4x50 tag: http://www.parallax.com/Store/Accessories/CommunicationRF/tabid/161/ProductID/689/List/0/Default.aspx?SortField=ProductName,ProductName
And I found these 2 codes on how to read and write data in the tag:
Read:
#include <SoftwareSerial.h>
#define RFID_READ 0x01
#define txPin 6
#define rxPin 8
#define whichSpace 9
SoftwareSerial mySerial(rxPin, txPin);
int val;
int runs = 0;
int points = 0;
void setup()
{
Serial.begin(9600);
Serial.println("RFID Read/Write Test");
mySerial.begin(9600);
pinMode(txPin, OUTPUT);
pinMode(rxPin, INPUT);
}
void suppressAll() //suppresses the "null result" from being printed if no RFID tag is present
{
if(mySerial.available() > 0)
{ mySerial.read();
suppressAll();
}
}
void loop()
{
int val;
mySerial.print("!RW");
mySerial.write(byte(RFID_READ));
mySerial.write(byte(whichSpace));
if(mySerial.available() > 0)
{
val = mySerial.read(); //The mySerial.read() procedure is called, but the result is not printed because I don't want the "error message: 1" cluttering up the serial monitor
if (val != 1) //If the error code is anything other than 1, then the RFID tag was not read correctly and any data collected is meaningless. In this case since we don't care about the resultant values they can be suppressed
{suppressAll();}
}
if(mySerial.available() > 0) {
val = mySerial.read();
Serial.print("1st:");
Serial.println(val, DEC);
points = val;
}
if(mySerial.available() > 0) {
val = mySerial.read();
Serial.print("2nd:");
Serial.println(val, DEC);
}
if(mySerial.available() > 0) {
val = mySerial.read();
Serial.print("3rd:");
Serial.println(val, DEC);
}
if(mySerial.available() > 0) {
val = mySerial.read();
Serial.print("4th:");
Serial.println(val, DEC);
Serial.println("-----------------");
}
delay(750);
}
Write:
#include <SoftwareSerial.h>
#define RFID_WRITE 0x02
#define txPin 6
#define rxPin 8
#define whichSpace 9
#define first 1 // first, second, thrid, and fourth are four arbitrary values which will be written to the RFID tag at address whichSpace
#define second 26
#define third 3
#define fourth 27
SoftwareSerial mySerial(rxPin, txPin);
void setup()
{
Serial.begin(9600);
Serial.println("RFID Write Test");
mySerial.begin(9600);
pinMode(txPin, OUTPUT);
pinMode(rxPin, INPUT);
}
void suppressAll() //Keeps error code & the "write confirmation" codes from being printed in the serial monitor
{
if(mySerial.available() > 0)
{ mySerial.read();
suppressAll();
}
}
void loop()
{
int val;
mySerial.print("!RW");
mySerial.write(byte(RFID_WRITE));
mySerial.write(byte(whichSpace));
mySerial.write(byte(first));
mySerial.write(byte(second));
mySerial.write(byte(third));
mySerial.write(byte(fourth));
if(mySerial.available() > 0) {
val = mySerial.read();
if (val == 1) //If data was written successfully
{ Serial.println("Data written succesfully!");
suppressAll();
}
else suppressAll(); //If an error occured during writing, discard all data recieved from the RFID writer
}
delay(250);
}
these 2 codes are from: Arduino Playground - ParallaxRFIDreadwritemodule
My problem here is that I do not know how to combine these 2 into a single code, here's how I wanted the code to execute:
- Read data from tag
- Save "first" as integer "points"
- When button is pressed, "points" will add by 1 then prompt user to tab card again
- when card is present, write "points" into card and display "Data written successfully"
note: data cannot be read when button is pressed.
I tried combining the two codes, but it will write a random number, most of the time it will write first:33, second:82, third:87, fourth:2
This is my failed code:
#include <SoftwareSerial.h>
#define RFID_READ 0x01
#define RFID_WRITE 0x02
#define txPin 6
#define rxPin 8
#define whichSpace 9
#define first points // first, second, thrid, and fourth are four arbitrary values which will be written to the RFID tag at address whichSpace
#define second 0
#define third 0
#define fourth 0
SoftwareSerial mySerial(rxPin, txPin);
int val;
int runs = 0;
const int buttonPin = 13;
int buttonState = 0;
int buttonCount = 0;
int points = 0;
void setup()
{
Serial.begin(9600);
Serial.println("RFID Write Test");
mySerial.begin(9600);
pinMode(txPin, OUTPUT);
pinMode(rxPin, INPUT);
}
void suppressAll() //Keeps error code & the "write confirmation" codes from being printed in the serial monitor
{
if(mySerial.available() > 0)
{ mySerial.read();
suppressAll();
}
}
void loop()
{int val; mySerial.print("!RW");
mySerial.write(byte(RFID_READ));
mySerial.write(byte(whichSpace));
if(mySerial.available() > 0)
{
val = mySerial.read(); //The mySerial.read() procedure is called, but the result is not printed because I don't want the "error message: 1" cluttering up the serial monitor
if (val != 1) //If the error code is anything other than 1, then the RFID tag was not read correctly and any data collected is meaningless. In this case since we don't care about the resultant values they can be suppressed
{suppressAll();}
}
if(mySerial.available() > 0) {
val = mySerial.read();
Serial.print("1st:");
Serial.println(val, DEC);
points = val; //value read will be saved as points
}
if(mySerial.available() > 0) {
val = mySerial.read();
Serial.print("2nd:");
Serial.println(val, DEC);
}//read second
if(mySerial.available() > 0) {
val = mySerial.read();
Serial.print("3rd:");
Serial.println(val, DEC);
}// read third
if(mySerial.available() > 0) {
val = mySerial.read();
Serial.print("4th:");
Serial.println(val, DEC);
Serial.println("-----------------");
} //read fourth
Serial.println(points);
delay(750);
// Up to this point is the reading code
while(digitalRead(buttonPin) == HIGH){
buttonCount = buttonCount + 1;
points = points + 1;}
while(buttonCount != 0){
//write starts here
Serial.println("Tab Card Again");
mySerial.print("!RW");
mySerial.write(byte(RFID_WRITE));
mySerial.write(byte(whichSpace));
mySerial.write(byte(points));
mySerial.write(byte(second));
mySerial.write(byte(third));
mySerial.write(byte(fourth));
if(mySerial.available() > 0) {
val = mySerial.read();
if (val == 1) //If data was written successfully
{ Serial.println("Data written succesfully!");
buttonCount = 0; //buttoncount will reset when write is successful
suppressAll();
}
else suppressAll(); //If an error occured during writing, discard all data recieved from the RFID writer
}
delay(250);
}
}
Attached is my hardware connection
Please help me out. Thanks