Arduino RFID Reading Unkown length of tag

I Have a an arduino Mega 1280 and am reading 10 digit RFID tags. The code is below. Is there a way to change the code to were i can read rfid tags were i dont know the length of the RFID ?

#define TAG_LEN 12

char tag[12] = {'0', '1', '0', '3', '6', 'A', 'F', '2', 'B', '1'};
char code[12];
int bytesread = 0;
int ledPin = 13; // Connect LED to pin 13
int rfidPin = 22; // RFID enable pin connected to digital pin 2
int val=0;
void setup() {

Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(rfidPin,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
pinMode(ledPin,OUTPUT); // Set ledPin to output

digitalWrite(rfidPin, LOW); // Activate the RFID reader

 void loop() {

  if(Serial.available() > 0) {	    // if data available from reader
    if((val = == 10) {   // check for header
	bytesread = 0;
	while(bytesread<10) {		  // read 10 digit code
	  if( Serial.available() > 0) {
	    val =;
	    if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
		break;			     // stop reading
	    code[bytesread] = val;	   // add the digit
	    bytesread++;			 // ready to read next digit
	if(bytesread >= 10) {		  // if 10 digit read is complete

	 if(strcmp(code, tag) == 0) {
	   Serial.print("Tag matches: ");
	 else {
	   Serial.println(" does not match");

	bytesread = 0;
	     delay(500);			     // wait for a second

void blink() {
  digitalWrite(ledPin, HIGH);
  digitalWrite(ledPin, LOW);

// extra stuff
// digitalWrite(2, HIGH);		 // deactivate RFID reader

reading 10 digit RFID tags

Do you think you are going to get a tag with a different number of digits? You need to know either the number of digits or what constitutes the start and end of a tag field.

For the number of digits just change the number 10 to what you want. Better still turn the number 10 into a variable and set it once at the start of the code.

You are already checking for the code to actually end here:

        if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading

So just make the "code" variable long enough to hold the lengths you want (eg. 20?).

Then instead of this:

    if(bytesread >= 10) {         // if 10 digit read is complete

... check if you got the terminator, eg.

        if((val == 10)||(val == 13)) { // we got end of code

By the way, using strcmp to compare codes assumes you put 0x00 at the end of the code you read in, which I don't see you doing. Also doing it the way you have assumes that the entire code arrives at once, whereas under serial comms you may only get part of it. Below is a link to the code I am using for a security system at home (valid code numbers changed).

Note the different way loop is written. An 0x02 character (STX) is sent by the reader at the start of the code sequence and a 0x03 (ETX) character at the end. That is what I use to check for the end of the code, not the length.