Serial Read Accuracy

Hey,

I have writing some code that reads information sent from an RFID module and displays the ID on the serial monitor and LCD panel. After adding the code for the LCD panel the output on the serial monitor seems to inaccurate. Sometimes there is no output at all or there is information missing. If i comment out the code for the LCD the correct information is displayed.

Also if i add some delays between the time i write to the serial monitor and the LCD the data is a little more stable but still inaccurate.

Board - Arduino Mega 2560

OUTPUT WITHOUT LCD CODE:

Read #1 - ID = 4A001024728029
Read #2 - ID = 4A001024728029
Read #3 - ID = 4A001024728029
Read #4 - ID = 4A001024728029
… //This demonstrates only one card but output has different cards as well.

OUTPUT WITH LCD CODE:
Read #1 - ID = 4A001024728029
Read #2 - ID =
Read #3 - ID = 4A00102472802
Read #4 - ID = 4A001024728
Read #5 - ID =

NOTE - First read is always correct.

Here is my code:

#include <LiquidCrystal.h>

int tempValue = 0;
char rfidCode[14];
int bytesRead = 0; 

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void setup() 
{ 
  lcd.begin(16, 2);
  Serial.begin(9600);    
  Serial1.begin(9600); 
  pinMode(53,OUTPUT); //Reset RFID Module;      
  digitalWrite(53, HIGH);  
}  

void loop() { 

  if(Serial1.available() > 0) 
  {          
    if((tempValue = Serial1.read()) == 10)
    {     
      bytesRead = 0; 
      while(bytesRead < 14)
      {            
        if( Serial1.available() > 0) 
        { 
          tempValue = Serial1.read();
          if((tempValue == 10)||(tempValue == 13)) 
          {  
            break;                       
          } 
          rfidCode[bytesRead] = tempValue;                 
          bytesRead++;         
        } 
      } 

      if(bytesRead == 14)
      {   
        Serial.print("ID = ");
        Serial.println(rfidCode);
        setDisplay();
      } 
      
      bytesRead = 0; 
      digitalWrite(53, LOW);               
      delay(1500);                                    
      digitalWrite(53, HIGH); 
    } 
  } 
} 


void setDisplay()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Card Found!");
  lcd.setCursor(0, 1);
  for (int i = 0; i < 14; i++)
  {
    lcd.print(rfidCode[i]);
  }
}

Thanks in advance.

Since you are always printing the same number of characters to the LCD, the time-consuming lcd.clear() call is not needed. Diddling around clearing the LCD may be causing the serial buffer to overflow.

If you (properly) NULL terminated rfidCode, and it was large enough to hold the NULL, you could print the RFID code using one statement, not 14. Serial.println(rfidCode); is wrong, since rfidCode is not NULL terminated. NULL terminating the array would make it right.

Finally, serial data delivery is not guaranteed. Your code assumes that it is, and that there will never be a lost byte. Not a good idea, really.

Does the rfid end its data string with a carriage and line feed?