2 RFID readers connected to 1 Mega

Hello there,

I took this sample : bildr Can You ID This? ID-20 RFID Reader + Arduino - bildr and convert it to working with 2 RFID readers - 1 to Serial1 and 1 to Serial2.

Here is the code -

int RFIDResetPin = 13;
int RFIDResetPin2 = 12;
int reader=0;

String last1="";
String last2="";

void setup(){
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);

pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
pinMode(RFIDResetPin2, OUTPUT);
digitalWrite(RFIDResetPin2, HIGH);
}

void loop(){

char tagString1[13];
char tagString2[13];
int index = 0;
boolean reading = false;

// reader=2;
while(Serial2.available()){

int readByte = Serial2.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 && readByte>30 && readByte<123){
//store the tag
tagString2[index] = readByte;
index ++;
}
}

// reader=1;
reading=false;
while(Serial1.available()){
int readByte = Serial1.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 && readByte>30 && readByte<123){
//store the tag
tagString1[index] = readByte;
index ++;
}
}

reader=2;
if (last2!=tagString2) {
Serial.print(“2:”); //read out any unknown tag
Serial.println(tagString2); //read out any unknown tag
last2=tagString2;
}

clearTag(tagString2); //Clear the char of all value

reader=1;
if (last1!=tagString1) {
Serial.print(“1:”); //read out any unknown tag
Serial.println(tagString1); //read out any unknown tag
last1=tagString1;
}
clearTag(tagString1); //Clear the char of all value
// digitalWrite(RFIDResetPin2, LOW);
// digitalWrite(RFIDResetPin2, HIGH);
resetReader();
}

void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
digitalWrite(RFIDResetPin, LOW);
digitalWrite(RFIDResetPin, HIGH);
digitalWrite(RFIDResetPin2, LOW);
digitalWrite(RFIDResetPin2, 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 = 0;

  • }*
    }
    When reading 1 tag on each reader - it works ok. When placing 2 tags ON THE SAME TIME - it disappear one of the tags.
    Any ideas ?
    Thanks and best regards,
    Ofer

Any ideas ?

I have an idea that you didn't read the "how to post in this forum" threads before you posted.

When reading 1 tag on each reader - it works ok. When placing 2 tags ON THE SAME TIME - it disappear one of the tags.

Sure, because you only have one variable that determines whether you are reading.

All the data from a tag does not arrive at once. So, several passes through loop() are required to read each tag. The reading variable is true or false for both readers. You need one for each reader.

That doesn't make any sense. Also in the original sample, the tag is being cleared every loop.

When placing 2 tags ON THE SAME TIME - it disappear one of the tags.

Most RFID readers can only read one tag at a time, so with two tags on the reader you will see nothing.

2 tags - 1 on first reader, 1 on second reader.

In that case it is what PaulS said.

Did you read my respond about it ?!??

That doesn't make any sense.

Did you try using two separate variables?

oferll:
Did you read my respond about it ?!??

I did and you are wrong it did make sense.
However, if what you meant was that you did not understand his answer then ask about the bit you didn't understand.

Every loop the function clearTag(tagString) is being called. This function clear the data inside the tagString. So I don't understand his sentence - "So, several passes through loop() are required to read each tag".

This function clear the data inside the tagStrin

You sure of that?

{
   one = 0; // wont
    one[i] = 0; // will

Think about this:-

 while(Serial1.available()){

At 9600 baud data is coming in from the reader at approximately one character every mS. How long do you think it takes to execute the code in that while loop? If your answer is more than 1mS then you will read everything in before that while loop terminates.
The Arduino is a 16MHz processor so in 1mS it can execute 16000 machine code instructions. I know machine code instructions are not C instructions but is a very poor compiler that takes that many instructions to complete the code in that while loop.

OK. So clearTag(tagString) doesn't clear the char array. I understand.

What I am not understand is this line inside the main loop() -
int index = 0;

So, for each new loop - it start placing coming characters from the first char on TagString... If what you are saying it's true - the string will always 1 character long of the last charcter that was read.
but it's working - it displays 12 chars for each tag. How does it work ??!?!?

How does it work

It is not immediately obvious how screwed up code produces something.
I suspect that something is stalling one of the loops until the buffer of the other reader fills up. However without access to your readers, which you are not telling us about it is hard to tell. If you want to spend some time with debug printouts then you can but getting it right is probably the better option than trying to work out what it is doing.
You have a compiler between you and the code so it is not an easy thing to do.
I can only offer advice into what is wrong.