RFID reader error?

My RFID reader is not being acknowledged by my Arduino in this code:

// Project 45

#define lockPin 7
#define speakerPin 9
#define tx 3
#define rx 2
#define unlockLength 2000

#include <SoftwareSerial.h>

SoftwareSerial rfidReader = SoftwareSerial(rx, tx);

int users = 3;

char* cards[] = { // valid cards
  "6F005C6E60",
  "3D00251C27",
  "3D0029E6BF", 
};

char* names[] = { // cardholder names
  "Tom Smith",
  "Dick Jones",
  "Harry Roberts"
};

void setup() {
  pinMode (lockPin, OUTPUT); 
  pinMode (speakerPin, OUTPUT);
  digitalWrite(lockPin, LOW);
  Serial.begin(9600);
  rfidReader.begin(9600);
}

void loop() {
  char cardNum[10]; // array to hold card number
  byte cardBytes[6]; // byte version of card number + checksum
  int index=0; // current digit
  byte byteIn=0; // byte read from RFID
  byte lastByte=0; // the last byte read
  byte checksum = 0; // checksum result stored here
  
  if (rfidReader.read()==2) { // read the RFID reader
      while(index<12) { // 12 digits in unique serial number
          byteIn = rfidReader.read(); // store value in byteIn
          if ((byteIn==1) || (byteIn==2) || (byteIn==10) || (byteIn==13)) {return;}
 // if STX, ETX, CR or LF break
          if (index<10) {cardNum[index]=byteIn;} // store first 10 HEX digits only(last 2 are checksum)
          // convert ascii hex to integer hex value
          if ((byteIn>='0') && (byteIn<='9')) {
          byteIn -= '0';
          } 
                else if ((byteIn>='A') && (byteIn<='F')) {
          byteIn = (byteIn+10)-'A';
          }
          if ((index & 1) == 1) { // if odd number merge 2 4 bit digits into 8 bit byte
              cardBytes[index/2]= (byteIn | (lastByte<<4)); // move the last digit 4 bits left and add new digit
              if (index<10) {checksum ^= cardBytes[index/2];} // tot up the checksum value 
          }
          lastByte=byteIn; // store the last byte read
          index++; // increment the index
          if (index==12) {cardNum[10] = '\0';} // if we have reached the end of all digits add a null terminator
    }
   
   Serial.println(cardNum); // print the card number
   int cardIndex =checkCard(cardNum); // check if card is valid and return index number
   if(cardIndex>=0 && (cardBytes[5]==checksum)) { // if card number and checksum are valid
        Serial.println("Card Validated");
        Serial.print("User: ");
        Serial.println(names[cardIndex]); // print the relevant name
        unlock(); // unlock the door
        Serial.println();
    }
    else {
        Serial.println("Card INVALID");
        tone(speakerPin, 250, 250);
        delay(250);
        tone(speakerPin, 150, 250);
        Serial.println();
    }
  }
}

int checkCard(char cardNum[10]) {
    for (int x=0; x<=users; x++) { // check all valid cards
        if(strcmp(cardNum, cards[x])==0) { // compare with last read card number
            return (x); // return index of card number
        }
    }
  return (-1); // negative value indicates no match
}

void unlock() {
    tone(speakerPin, 1000, 500);
    digitalWrite(lockPin, HIGH);
    delay(unlockLength);
    digitalWrite(lockPin, LOW);
    }

Using on a mega, I am POSITIVE that the reader works, and the rx pin works, too. I am at wit's end. Any help appreciated.

-Jelimoore

Using on a mega

Which has 4 hardware serial ports. Why are you (mis)using SoftwareSerial?

Well, yes, but I got this code from a book, and the part that really stumps me is that it's in a book that's all about Arduino! But yes, I have tried the Serial1 but the result: ÿÿÿÿÿÿÿÿÿÿÿ. The baud rate is set correctly, 9600, and all of the software serial rfidreader functions are replaces with Serial1.

Well, yes, but I got this code from a book

Which one?

and the part that really stumps me is that it's in a book that's all about Arduino!

Perhaps with the assumption that you have a UNO, rather than a Mega.

But yes, I have tried the Serial1 but the result: ÿÿÿÿÿÿÿÿÿÿÿ.

But, you're not about to show us that code or describe the wiring changes you made. OK. Fine. All we can do, then, is wish you luck. Good luck.

(deleted)

This:

ÿÿÿÿÿÿÿÿÿÿÿ.

Generally means that you tried to read something from the serial port when there was nothing there. Take a look in the reference at serial.available.

The book "Beginning Arduino". It probably assumes you're using an uno, rather than a mega, but I have used the code perfectly on the board before. The only wiring changes I have made are putting the "D0" wire into the RX1 line of the mega. Plus changing out the software serial ports in the code.

And I am using an ID-12LA from sparkfun.

You could look at the SoftwareSerial library page, and see what pins can be used. Use one or two supported pins, and connect the RFID reader to those pins.

And you realize that this code will compile in both 0022 and 1.x.x, but only in 0022 will it actually produce something.