Got ya. I'd like to build a bare bones read and print sketch.
As I don't have control over whats being sent via Serial I can't package the data in a nice way i.e. start and end identifiers.
I'm learning as I go here, so bare with me!
I've found this sketch for another RFID module
int RFIDResetPin = 13;
//Register your RFID tags here
char tag1[13] = "4500B8A5025A";
char tag2[13] = "4500B8C30F31";
char tag3[13] = "4500B8A4C49D";
void setup(){
Serial.begin(9600);
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
//ONLY NEEDED IF CONTROLING THESE PINS - EG. LEDs
pinMode(2, OUTPUT);
}
void loop(){
char tagString[13];
int index = 0;
boolean reading = false;
while(Serial.available()){
int readByte = Serial.read(); //read next available byte
if(readByte == 2) reading = true; //begining of tag
if(readByte == 3) reading = false; //end of tag
if(reading && readByte != 2 && readByte != 10 && readByte != 13){
//store the tag
tagString[index] = readByte;
index ++;
}
}
checkTag(tagString); //Check if it is a match
clearTag(tagString); //Clear the char of all value
resetReader(); //eset the RFID reader
}
void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////
if(strlen(tag) == 0) return; //empty, no need to contunue
if(compareTag(tag, tag1)){ // if matched tag1, do this
lightLED(2);
Serial.print("a");
}else if(compareTag(tag, tag2)){ //if matched tag2, do this
lightLED(2);
Serial.print("b");
}else if(compareTag(tag, tag3)){
lightLED(2);
Serial.print("c");
}else{
Serial.println(tag); //read out any unknown tag
}
}
void lightLED(int pin){
///////////////////////////////////
//Turn on LED on pin "pin" for 250ms
///////////////////////////////////
digitalWrite(pin, HIGH);
delay(250);
digitalWrite(pin, LOW);
}
void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
digitalWrite(RFIDResetPin, LOW);
digitalWrite(RFIDResetPin, HIGH);
delay(150);
}
void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
for(int i = 0; i < strlen(one); i++){
one[i] = 0;
}
}
boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////
if(strlen(one) == 0) return false; //empty
for(int i = 0; i < 12; i++){
if(one[i] != two[i]) return false;
}
return true; //no mismatches
}
The bit that is interesting is the While loop
while(Serial.available()){
int readByte = Serial.read(); //read next available byte
if(readByte == 2) reading = true; //begining of tag
if(readByte == 3) reading = false; //end of tag
if(reading && readByte != 2 && readByte != 10 && readByte != 13){
//store the tag
tagString[index] = readByte;
index ++;
}
}
It looks more efficient. Although I don't fully understand how the beginning and ending part works
if(readByte == 2) reading = true; //begining of tag
if(readByte == 3) reading = false; //end of tag
I understand that this codes has a lot of extra stuff i.e. checking previously stated tag values that it compares and then prints a letter based on whether it recognises the tag. But it also prints the tag if it doesn't know it.
I guess I want a sketch that will
- Check to see if there is anything available via serial
- if there is, put the Tag hex key in an array (knowing where the start and end are)
- Print the hex key
- Clear the hex key array
- Put the module back into seek mode
In the first sketch, I don't understand the halt function
void halt()
{
//Halt tag
rfid.print(255, BYTE);
rfid.print(0, BYTE);
rfid.print(1, BYTE);
rfid.print(147, BYTE);
rfid.print(148, BYTE);
}
or the seek function
void seek()
{
//search for RFID tag
rfid.print(255, BYTE);
rfid.print(0, BYTE);
rfid.print(1, BYTE);
rfid.print(130, BYTE);
rfid.print(131, BYTE);
delay(10);
}
Would you be able to explain those?