2 RFID Readers

Hello Friends,

i did a RFID door lock, and it was working fine. Now i want to put 2 RFID readers, but i'm unable to. can anyone tell me what's wrong?

#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 3); // RX and TX
SoftwareSerial OUTRFID(6, 7); // RX and TX


int data1 = 0;

int yes = 9;
int no = 4;
int buzzer = 5;
char Str6[15] = "Swipe Card"; // What to display on LCD by default

char buffer[5];
char serInString[25];
int pId =0;
int serInLen = 25;


// Use allowedTags[0] in place of tag1 and allowedTags[1] in place of tag2, etc

       int allowedTags[][11] = {
         {49,50,48,48,48,55,49,50,51,57,13},         // Tag 1
         {50,51,48,55,53,49,52,50,55,56,13}         // Tag 2
       };
       
       int blockedTags[][11] = {
         {53,50,56,55,51,57,53,55,51,57,13},         // Tag 1
         
       };
	   
       int newtag[11] = { 0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons
	   
	   
	   char* tagName[] = {
      "Hanish",              // Tag 1
      "Jeremy Clarkson",      // Tag 2
      "Dexter D Dog",       // Tag 3
    };
    
    char* tagNameBlock[] = {
      "Tom",              // Tag 1
      "Jeerry",      // Tag 2
    };
	   
// The number of entries is:
const int tagCount = sizeof allowedTags / sizeof allowedTags[0];
const int tagCount2 = sizeof blockedTags / sizeof blockedTags[0];


void setup()
{
  RFID.begin(9600);    // start serial to Check in RFID reader
  OUTRFID.begin(9600);    // start serial to Check out RFID reader
  Serial.begin(9600);  // start serial to PC 
   
  pinMode(yes, OUTPUT); // for status LEDs
  pinMode(no, OUTPUT);
  
  pinMode(buzzer, OUTPUT);   // sets the pin as output
  
    LcdInitialise();
    LcdClear();
    LcdString(Str6);
     
    Serial.println("#S|LOGRFID|[]#");    
}


void readTags()
{
 

  if (RFID.available() > 0) 
  {
    // read tag numbers
    delay(100); // needed to allow time for the data to come in from the serial buffer.


    Serial.print("Tag id is :");
    
    for (int z = 0 ; z < 11 ; z++) // read the rest of the tag
    {
      data1 = RFID.read();
      newtag[z] = data1;
      
      Serial.print(newtag[z]);
     
      for(int zx=0 ; z < 10 ; z++)
      {
      Serial.print(",");
      break;
      }
    }
    
    Serial.println(" ");
    
    RFID.flush(); // stops multiple reads

    // Search the tag database for this particular tag
    int tagId = findTag( newtag );
	int tagId2 = findBlockedTag( newtag );
		  
    if( tagId > 0 )
          {
	    cardAccepted();
          } 
          
	else if( tagId2 > 0)
		  {
		cardBlocked();
			}
		  
    else 
          {
            
            Serial.println("Tag Unidentified");
	    cardRejected ();
          
          }
          Serial.println();     // Blank separator line in output
        }
 }


void readTagsOUT()
{
 

  if (OUTRFID.available() > 0) 
  {
    // read tag numbers
    delay(100); // needed to allow time for the data to come in from the serial buffer.


    Serial.print("Tag id is :");
    
    for (int z = 0 ; z < 11 ; z++) // read the rest of the tag
    {
      data1 = OUTRFID.read();
      newtag[z] = data1;
      
      Serial.print(newtag[z]);
     
      for(int zx=0 ; z < 10 ; z++)
      {
      Serial.print(",");
      break;
      }
    }
    
    Serial.println(" ");
    
    OUTRFID.flush(); // stops multiple reads

    // Search the tag database for this particular tag
    int tagId = findTag( newtag );
	int tagId2 = findBlockedTag( newtag );
		  
    if( tagId > 0 )
          {
	    cardAccepted();
          } 
          
	else if( tagId2 > 0)
		  {
		cardBlocked();
			}
		  
    else 
          {
            
            Serial.println("Tag Unidentified");
	    cardRejected ();
          
          }
          Serial.println();     // Blank separator line in output
        }
 }


boolean comparetag(int aa[11], int bb[11])
{
  boolean ff = false;
  int fg = 0;
  for (int cc = 0 ; cc < 11 ; cc++)
  {
    if (aa[cc] == bb[cc])
    {
      fg++;
    }
  }
  if (fg == 11)
  {
    ff = true;
  }
  return ff;
}



 /**
     * Search for a specific tag in the database
     */
int findTag( int newtag[11] ) {
      for (int thisCard = 0; thisCard < tagCount; thisCard++) {
        // Check if the tag value matches this row in the tag database
        if(comparetag(newtag, allowedTags[thisCard]) == true)
        {
          // The row in the database starts at 0, so add 1 to the result so
          // that the card ID starts from 1 instead (0 represents "no match")
          return(thisCard + 1);
        }
      }
      // If we don't find the tag return a tag ID of 0 to show there was no match
      return(0);
    }

	
int findBlockedTag( int newtag[11] ) {
      for (int thisCard = 0; thisCard < tagCount2; thisCard++) {
        // Check if the tag value matches this row in the tag database
        if(comparetag(newtag, blockedTags[thisCard]) == true)
        {
          // The row in the database starts at 0, so add 1 to the result so
          // that the card ID starts from 1 instead (0 represents "no match")
          return(thisCard + 1);
        }
      }
      // If we don't find the tag return a tag ID of 0 to show there was no match
      return(0);
    }


void cardAccepted(void)      //Function when card accepted
{
    int tagId = findTag( newtag );
 
    ExcelTimeStamp(); 
    ExcelNameLog();
    ExcelLog();
    
     Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("Access Granted");
     Serial.print(" {TAB} ");
     Serial.println("]#");
    
    ExcelNext();
    ExcelSave();
    
	Serial.print("Name : ");
    Serial.println(tagName[tagId - 1]); 
    Serial.println("Accepted");
    
    LcdInitialise();
    LcdClear();
    LcdString("Hello ");
    LcdString(tagName[tagId - 1]);
    
    analogWrite(buzzer,100);
    digitalWrite(yes, HIGH);
    
    delay(600);
    
    digitalWrite(buzzer, LOW);
    digitalWrite(yes, LOW);
    
    delay(1000);
    
    LcdInitialise();
    LcdClear();
    LcdString(Str6);
       
}

void cardBlocked(void)        //Function when card rejected
{
    
    ExcelTimeStamp(); 
    ExcelBlockedNameLog();
    ExcelLog();
    
     Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("TAG BLOCKED");
     Serial.print(" {TAB} ");
     Serial.println("]#");
    
    ExcelNext();
    ExcelSave();
    
    Serial.println("TAG BLOCKED");
    digitalWrite(no, HIGH);
    
    LcdInitialise();
    LcdClear();
    LcdString("TAG BLOCKED");
    
    analogWrite(buzzer,10);
    delay(100);
    digitalWrite(buzzer, LOW);
    digitalWrite(no, LOW);
    delay(200);
    digitalWrite(no, HIGH);
    analogWrite(buzzer,10);
    delay(200);
    digitalWrite(no, LOW);
    digitalWrite(buzzer, LOW);
   
    delay(300);
    
    LcdInitialise();
    LcdClear();
    LcdString(Str6);
    digitalWrite(no, LOW);
    
}

void cardRejected(void)        //Function when card rejected
{
    
    ExcelTimeStamp(); 
    ExcelNameLog();
    ExcelLog();
    
     Serial.print("#S|SENDK|[");
     Serial.print(itoa((pId), buffer, 10));
     Serial.print("&");
     Serial.print("Tag Unidentified");
     Serial.print(" {TAB} ");
     Serial.println("]#");
    
    ExcelNext();
    ExcelSave();
    
    Serial.println("Tag Unidentified");
    digitalWrite(no, HIGH);
    
    LcdInitialise();
    LcdClear();
    LcdString("NO ACCESS ");
    
    analogWrite(buzzer,10);
    delay(100);
    
    digitalWrite(no, LOW);
   
    analogWrite(buzzer,50);
    delay(500);
    digitalWrite(buzzer, LOW);
   
    digitalWrite(no, HIGH);
    delay(400);
    digitalWrite(no, LOW);
    delay(1000);
    
    LcdInitialise();
    LcdClear();
    LcdString(Str6);
    digitalWrite(no, LOW);
    
}  

void loop()
{
  readTags();
  readTagsOUT();
}

code is > 9000 characters, so i'm removed some functions irrelevant to the question. Full code is attached

I did try to change the baud rate of the new one to 14400, but still nothing.

Help please..:frowning:

Thanks

read_id__lcd_buzzer_database_name_blocked_rfid2.1.ino (15.6 KB)

Your function "readTags" looks to me to be almost identical to your function "readTagsOUT" - why?

Please post all your code and explain clearly what your problem is.

AWOL:
Your function "readTags" looks to me to be almost identical to your function "readTagsOUT" - why?

Please post all your code and explain clearly what your problem is.

Because it was supposed to do the same thing as readTags, except that its information should be from RFID reader number 2. which is OUTRFID.

I've updated the code, and attached the full code since it's greater than 9000 characters.

My problem is that i'm unable to use both RFID readers when i've added the second one. i found out when i put the "OUTRFID.begin(9600); " the first RFID reader stops reading, and the second one gives weird infos.

My problem is that i'm unable to use both RFID readers when i've added the second one. i found out when i put the "OUTRFID.begin(9600); " the first RFID reader stops reading, and the second one gives weird infos.

Exactly. And that's well documented. Only one instance of SoftwareSerial can listen at a time.

Quit beating a dead horse, and get the right hardware - a Mega.

PaulS:
Exactly. And that's well documented. Only one instance of SoftwareSerial can listen at a time.

Quit beating a dead horse, and get the right hardware - a Mega.

There's nothing i can do? :frowning:

You could produce a better version of SoftwareSerial (and fame, fortune and adulation would follow), but getting a Mega would be a lot simpler.

AWOL:
You could produce a better version of SoftwareSerial (and fame, fortune and adulation would follow)

Seems a good idea. I'll think about it after a couple of beers.

Thanks mate :slight_smile: