Using string.equals() and not getting the results...

Hello, I am implementing an RFID reader that receives a tag ID via text message and compares it to a tag that enters its field.

I have been successful in reading the tag and printing the ID to the serial window. Additionally, because I am comparing an incoming string and the tag id is numeric I have converted the ID into a string of chars using itoa(). When I try to use string.equals(string2) on this new string, I'm not getting the results. Even though when I user Serial.print() on the new string it's identical to the incoming "message".

One more thing: the length of the new string (called IDofTag in my code) is 8. The string is: "39fldf22" which implies that there is a null character at the end. How do I remove it or can I do: string.equals("39fldf22\0")? Everything I'm talking about is in the print serial function.

Thanks! My code:

#include <SoftwareSerial.h>
#include <SPI.h>
SoftwareSerial rfid(12,13);

//RFID variables
int flag = 0;
int Tag[11];
//String TagID = "";
String IDofTag = ""; //Global version of TagID
String message = "39fldf22";



//void check_for_notag(void);
void halt(void);
void parse(void);
void print_serial(void);
void read_serial(void);
void seek(void);
void set_flag(void);

//Time Variables
const int  cs=8; //chip select 

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting...");
  
  //Initialize Time
  RTC_init();
  
  //day(1-31), month(1-12), year(0-99), hour(0-23), minute(0-59), second(0-59)
  SetTimeDate(28,3,12,8,57,0); 
  
  //Initialize RFID
  rfid.begin(19200);
  delay(10);
}

void loop()
{
  read_serial();
  
}

int RTC_init(){ 
	  pinMode(cs,OUTPUT); // chip select
	  // start the SPI library:
	  SPI.begin();
	  SPI.setBitOrder(MSBFIRST); 
	  SPI.setDataMode(SPI_MODE1); // both mode 1 & 3 should work 
	  //set control register 
	  digitalWrite(cs, LOW);  
	  SPI.transfer(0x8E);
	  SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
	  digitalWrite(cs, HIGH);
	  delay(10);
}

int SetTimeDate(int d, int mo, int y, int h, int mi, int s){ 
	int TimeDate [7]={s,mi,h,0,d,mo,y};
	for(int i=0; i<=6;i++){
		if(i==3)
			i++;
		int b= TimeDate[i]/10;
		int a= TimeDate[i]-b*10;
		if(i==2){
			if (b==2)
				b=B00000010;
			else if (b==1)
				b=B00000001;
		}	
		TimeDate[i]= a+(b<<4);
		  
		digitalWrite(cs, LOW);
		SPI.transfer(i+0x80); 
		SPI.transfer(TimeDate[i]);        
		digitalWrite(cs, HIGH);
}
}

String ReadTimeDate(){
	String temp;
	int TimeDate [7]; //second,minute,hour,null,day,month,year		
	for(int i=0; i<=6;i++){
		if(i==3)
			i++;
		digitalWrite(cs, LOW);
		SPI.transfer(i+0x00); 
		unsigned int n = SPI.transfer(0x00);        
		digitalWrite(cs, HIGH);
		int a=n & B00001111;    
		if(i==2){	
			int b=(n & B00110000)>>4; //24 hour mode
			if(b==B00000010)
				b=20;        
			else if(b==B00000001)
				b=10;
			TimeDate[i]=a+b;
		}
		else if(i==4){
			int b=(n & B00110000)>>4;
			TimeDate[i]=a+b*10;
		}
		else if(i==5){
			int b=(n & B00010000)>>4;
			TimeDate[i]=a+b*10;
		}
		else if(i==6){
			int b=(n & B11110000)>>4;
			TimeDate[i]=a+b*10;
		}
		else{	
			int b=(n & B01110000)>>4;
			TimeDate[i]=a+b*10;	
			}
	}
	temp.concat(TimeDate[4]);
	temp.concat("/") ;
	temp.concat(TimeDate[5]);
	temp.concat("/") ;
	temp.concat(TimeDate[6]);
	temp.concat("     ") ;
	temp.concat(TimeDate[2]);
	temp.concat(":") ;
	temp.concat(TimeDate[1]);
	temp.concat(":") ;
	temp.concat(TimeDate[0]);
  return(temp);
}

void halt()
{
 //Halt tag
  //rfid.print(255, BYTE);
  rfid.write(255);
  //rfid.print(0, BYTE);
  int i = 0;
  rfid.write(i);
  //rfid.print(1, BYTE);
  rfid.write(1);
  //rfid.print(147, BYTE);
  rfid.write(147);
  //rfid.print(148, BYTE);
  rfid.write(148);
}

void parse()
{
  while(rfid.available())
  {
    if(rfid.read() == 255)
    {
      for(int i=1;i<11;i++)
      {
        Tag[i]= rfid.read();
      }
    }
  }
}

void print_serial()
{ 
  if(flag == 1)
  {
    //print to serial port
    Serial.print(Tag[8], HEX);
    Serial.print(Tag[7], HEX);
    Serial.print(Tag[6], HEX);
    Serial.print(Tag[5], HEX);
    delay(10);
    Serial.println(ReadTimeDate());
    
    String TagID = "";
    
    char buffer[1];
    for(int index = 8; index>4; index--)
    {
      itoa(Tag[index], buffer,16);
      TagID = TagID += buffer;
    }
    
    IDofTag = TagID; //Globalize TagID
    Serial.println(IDofTag.length());
    
    if(IDofTag.equals(message))
    {
      Serial.println("Entered IF loop");
    }
    
    Serial.println(IDofTag);
    
    
    
  }
}

void read_serial()
{
  seek();
  delay(10);
  parse();
  set_flag();
  print_serial();
  delay(100);
}

void seek()
{
  
  rfid.write(255);
  int i = 0;
  rfid.write(i);
  rfid.write(1);
  rfid.write(130);
  rfid.write(131);
  delay(10);
}

void set_flag()
{
  if(Tag[2] == 6){
    flag++;
  }
  if(Tag[2] == 2){
    flag = 0;
  }
}

What's all the RTC stuff doing in a sketch about RFID?

 for(int i=1;i<11;i++)

The code isn't commented; why doesn't the index go from zero?

  while(rfid.available())
  {
    if(rfid.read() == 255)
    {
      for(int i=1;i<11;i++)
      {
        Tag[i]= rfid.read();
      }
    }
  }
}

So, if there is one byte available to read, read all 12 of them. Got it. Perhaps you need to rework this bit of rubbish.

void loop()
{
  read_serial();
  
}

Now, it's a great idea to use functions, but this is ridiculous.

    char buffer[1];
    for(int index = 8; index>4; index--)
    {
      itoa(Tag[index], buffer,16);

Tag[n] contains a byte, in the range 0 to 255. Converting that to a hex string will result in a string between "00" to "FF". Even if the leading 0 is suppressed, the trailing NULL that itoa() adds means that the array size needs to be at least 3 elements, not 1.

      TagID = TagID += buffer;

Waste, waste, waste. You REALLY need to figure out exactly what this statement is doing. You probably will not believe the amount of resources this simple (mangled) statement consumes.

The RTC will be used to attach a time signature to when an RFID tag is read.

I will change the buffer[1] to buffer[3], what do you suggest I use instead of the += operator?

In addition, can anyone address how to compare the two strings successfully? I'm having trouble with that, perhaps I am not aware of extra characters that exist in TagID and that aren't being printed with Serial.print but are included in the comparison with IDofTag.equals();

The RTC will be used to attach a time signature to when an RFID tag is read.

Probably useful, once you've debugged the RFID code, but no point in posting it here.

In addition, can anyone address how to compare the two strings successfully?

For C strings, strcmp does it for me.

what do you suggest I use instead of the += operator?

First thing I'd do is recommend is that you forget all about the String class, but I know that you won't.

So, the problem with the statement

TagID = TagID += buffer;

is that the size of TagID is determined, along with the size of buffer. The two sizes are added, and that much memory is allocated. The data that was in TagID is copied to the new memory, and the data that is in buffer is copied after it. The space that TagID was pointing to is deallocated, and TagID now points to the new memory location. That is, the TagID on the right of the equals sign does. You then assign this value to another variable that happens to be the same one. That doesn't matter, though. The copy constructor is called anyway, with more allocing and copying.

The TagID += buffer; part of that statement is all that is needed. It is the rest that is the real waste.