inconsistent results using delay

Hi All,

I'm having a problem using a pro micro from sparkfun (https://www.sparkfun.com/products/12640) and an ID-12LA RFID reader (RFID Reader ID-12LA (125 kHz) - SEN-11827 - SparkFun Electronics).

When I scan tags I get the output I expect if I have a delay of 500 or so in the loop, but when the delay is omitted I'm getting inconsistent results that I don't understand. Note that in both examples below I'm scanning the same tag four times. Without the delay I'm getting different results for the same tag!

Output to the serial monitor with delay(500):

int tag: 13 55 55 48 48 57 54 70 65 67 54 68 68 
string tag: 770096FAC6DD

int tag: 13 55 55 48 48 57 54 70 65 67 54 68 68 
string tag: 770096FAC6DD

int tag: 13 55 55 48 48 57 54 70 65 67 54 68 68 
string tag: 770096FAC6DD

int tag: 13 55 55 48 48 57 54 70 65 67 54 68 68 
string tag: 770096FAC6DD

Output to the serial monitor with no delay:

int tag: 13 25600 830 830 257 744 46 -6143 -6143 11778 0 2042 -32192 
string tag: 
s! 
int tag: 13 25600 830 830 257 744 -6143 258 232 -32256 0 -32768 256 
string tag: 
s! 
int tag: 13 25600 830 830 257 744 46 -1536 16391 130 47 128 -6143 
string tag: 
s! 
int tag: 13 25600 830 830 257 744 46 -6143 258 742 46 512 46 
string tag: 
s!

Here is the code I'm using:

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

void loop() {
  char tagString[13];
  int intTag1[13];
  int index = 0;
  boolean reading = true;
  boolean weGotATag = 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
      weGotATag = true;
    }

    if(reading && readByte != 10){
      //store the tag
      tagString[index] = readByte;
      intTag1[index] = readByte;
      index ++; 
    }
  }
  
  if(weGotATag){
    Serial.print("int tag: ");
    for(int i = 0; i < 13; i++){
      Serial.print(intTag1[i]);
      Serial.print(" ");
    }
      Serial.println();
      Serial.print("string tag: ");
      Serial.println(tagString);
      weGotATag = false; 
  }
  delay(500); //if I comment this delay out I get the problem
}

Any ideas would be very appreciated.

Wow, works perfectly now. Thank you very much!

whoops, spoke too soon. Now I'm getting the following output when the delay is omitted:

int tag: 13 0 0 0 0 0 0 0 0 0 0 0 0 
string tag: 

int tag: 13 0 0 0 0 0 0 0 0 0 0 0 0 
string tag: 

int tag: 13 0 0 0 0 0 0 0 0 0 0 0 0 
string tag: 

int tag: 13 0 0 0 0 0 0 0 0 0 0 0 0 
string tag:

Here's the modified code:

char tagString[13];
int intTag1[13];
int index;
boolean reading = true;
boolean weGotATag = false;
int readByte;

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

void loop() {
  index = 0;
  while(Serial1.available()){
    
    readByte = Serial1.read(); //read next available byte
    
    if(readByte == 2) { 
      reading = true; //begining of tag
    }
    if(readByte == 3) {
      reading = false; //end of tag
      weGotATag = true;
    }

    if(reading && readByte != 10){
      //store the tag
      tagString[index] = readByte;
      intTag1[index] = readByte;
      index ++; 
    }
  }
  
  if(weGotATag){
    Serial.print("int tag: ");
    for(int i = 0; i < 13; i++){
      Serial.print(intTag1[i]);
      Serial.print(" ");
    }
      Serial.println();
      Serial.print("string tag: ");
      Serial.println(tagString);
      weGotATag = false; 
  }
  //delay(500); //if I comment this delay out I get the problem
}

Again, the results are working when the delay is included.

great, thank you!