Double RFID Reader

Hi, I have two Innovations ID-20 RFID Readers and an Arduino Mega 2560. I followed the tutorial on bildr http://bildr.org/2011/02/rfid-arduino/ and it works for one RFID Reader

But when I tried adding the other RFID Reade (I duplicated certain segments of the code), it refuses to work... basically, both readers would not read... Can anyone help me?

int RFIDResetPin = 13;
int RFIDResetPin2 = 12;

//Register your RFID tags here
char tag1[13] = "100045AC8871";
char tag2[13] = "1700DEF82C1D";
char tag3[13] = "01023C013A04";
char tag4[13] = "01023101093A";
char tag5[13] = "01023C0A4376";
char tag6[13] = "01023C000E31";
char tag7[13] = "01023C0A3207";
char tag8[13] = "1A004116317C";
char tag9[13] = "1E009A81F9FC";
char tag10[13] = "1A004162261F";

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

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

void loop(){

  char tagString[13];
  int index = 0;
  boolean 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){
      //store the tag
      tagString[index] = readByte;
      index ++;
    }
  }
 
  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){
      //store the tag
      tagString[index] = readByte;
      index ++;
    }
  }
 
  checkTag(tagString); //Check if it is a match
  clearTag(tagString); //Clear the char of all value
  resetReader(); //Reset 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
    Serial.println("received1");

  }else if(compareTag(tag, tag2)){ //if matched tag2, do this
    Serial.println("received2");

  }else if(compareTag(tag, tag3)){
    lightLED(4);

  }else if(compareTag(tag, tag4)){
    lightLED(5);

  }else if(compareTag(tag, tag5)){
    lightLED(6);

  }else if(compareTag(tag, tag6)){
    lightLED(7);

  }else if(compareTag(tag, tag7)){
    lightLED(8);

  }else if(compareTag(tag, tag8)){
    lightLED(9);

  }else if(compareTag(tag, tag9)){
    lightLED(10);

  }else if(compareTag(tag, tag10)){
    lightLED(11);

  }else{
    Serial.println(tag); //read out any unknown tag
  }

}

void lightLED(int pin){
///////////////////////////////////
//Turn on LED on pin "pin" for 250ms
///////////////////////////////////
  Serial.println(pin);

  digitalWrite(pin, HIGH);
  delay(250);
  digitalWrite(pin, LOW);
}

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

But when I tried adding the other RFID Reade (I duplicated certain segments of the code), it refuses to work... basically, both readers would not read... Can anyone help me?

How do you know it doesn't work? I see no Serial.print() statements in either block.

You seem to assume that if one reader has data to send to the Arduino that it will arrive all-at-once. This is not the case. The loop() function may iterate several thousand (or million) times while the data dribbles in. Perhaps COMPLETELY isolating the cases (separate reading, index, and tagString variables) would help.

Also, it makes no sense to call checkTag() or clearTag() or resetReader() if reading is true.

Pet peeve time. This bugs me:

int RFIDResetPin = 13;
int RFIDResetPin2 = 12;

If you are counting RFID readers, you count 1, 2, 3, etc., not a, 2, 3, etc. So, if you are going to have similar names, all similar names should have numbers in them.

i did a serial.println to test but i didn't include it in this code.

why does it make no sense to call those functions? those functions are the actions that need to be taken when the tag is read as they need to perform some sort of action such as opening the door.

i just hadn't bothered to change the labels yet. will do once i get the logic right....

Have you tried the code that works with one reader while the other reader is powered up.
RFID readers will interfere with each other if operated closer than 2M apart. The range is severely reduced and at some distance they both stop working.

why does it make no sense to call those functions? those functions are the actions that need to be taken when the tag is read as they need to perform some sort of action such as opening the door.

Getting serial data from an RFID reader takes time. Many (many, many) iterations of loop, in fact. When one iteration of loop detects that there is serial data, and that the serial data does indeed indicate the start of tag data, any such data in the buffer will also be read. There is ABSOLUTELY NO guarantee that that is ALL the data from the tag.

So, you might get 2 or 6 or (if really lucky) all 13 characters of the tag. If you are not really lucky, you will call those functions with only part of the data for a tag.

Now, calling checkTag() is OK. The tag data will fail to match any known tags, because it is incomplete.

But, calling clearTag() and resetReader() in the middle of reading a tag is not a good idea. For what I thought were obvious reasons. I guess not.

Have you tried the code that works with one reader while the other reader is powered up.
RFID readers will interfere with each other if operated closer than 2M apart. The range is severely reduced and at some distance they both stop working.

Does powering each for a few milliseconds at time negate this interference? How did you get three readers working in close proximity in the CrazyPeople project?

Does powering each for a few milliseconds at time negate this interference?

That makes it worst an RFID reader typically takes about 500mS to stabilise on power up.

How did you get three readers working in close proximity in the CrazyPeople project?

By carefully getting the spacing between the readers so that they would still work. However the read range is drastically cut down compared to what it would be with only a single reader. For that game I only needed the range to be such that it would read the card when placed on the outside of the case those readers normally have a 9 inch range and operating them in close proximity reduced this to less than one inch.