Hi there guys, so I'm having a little bit of a problem with using an rfid scanner and sd module together. I'm using the SDfat library by William Greiman (which has been fantastic) and the rfid library by Miguel Balboa. So I have both rfid and sd modules connected to one Arduino via SPI with the rfid SS pin on D10 and SD SS pin on D4. I can use each library separately (such as the examples) and they work fine with scanning cards, reading and writing to an sd card etc... However when it comes to using both combined I can't open a file to read it.
FYI, I have not placed any pull up resistors on the SS lines, opting to bring the pins HIGH with software in setup. I have also not put a diode on the second SPI device MISO line either which I have seen in some multi SPI device connections.
My code I have whipped up is here:
#include <SPI.h>
#include <MFRC522.h>
#include "SdFat.h"
SdFat sd;
SdFile cardsFile;
SdFile regFile;
#define RST_PIN 9 // Configurable pin, see default layout
#define SS_PIN 10 // Configurable pin, see default layout
#define chipSelect 4 //select SPI line for SD Card
String strUID = "";
char UID[30];
char line[100];
int stringIndex = 0;
char readIn;
int i = 0;
int j;
int cardPresent;
int check =0;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
MFRC522::MIFARE_Key key; // Set key
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Initialise serial communication with PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 hardware
//Initialise pinModes for SPI select lines
pinMode(SS_PIN, OUTPUT);
pinMode(chipSelect, OUTPUT);
digitalWrite(SS_PIN, HIGH);
digitalWrite(chipSelect, HIGH);
// Prepare key - all keys are set to FFFFFFFFFFFF at chip delivery from the factory
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Serial.println("Initialising SD Card");
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
sd.initErrorHalt();
}
Serial.println("SD Card initialised");
//digitalWrite(chipSelect, LOW);
}
void loop() {
check = 0;
//Activate rfid communication
digitalWrite(SS_PIN, LOW);
// Check for present card and select if present
if (! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
delay(50);
return;
} else {
//Deactivate rfid communication, Activate SD communiction
digitalWrite(SS_PIN, HIGH);
digitalWrite(chipSelect, LOW);
Serial.println("card detected");
//get card id
for (byte i = 0; i < mfrc522.uid.size; i++) {
strUID += (mfrc522.uid.uidByte[i] < 0x10 ? "0" : "") + String(mfrc522.uid.uidByte[i], HEX);
}
strUID.toUpperCase(); // Ensure upper case HEX
strUID.toCharArray(UID, 30); //Convert to array ready for sending
Serial.print("card ID is ");
Serial.println(strUID);
//Open cardsList.txt file for reading
if (!cardsFile.open("test.txt", O_READ)) {
sd.errorHalt("opening test.txt for read failed");
}
////////////////////////////////////////Doesn't get past this point.////////////////////////////////////
Serial.println("cards file opened");
//Read through file to check for card in cardsList.txt
while (cardsFile.available()) {
readIn = cardsFile.read();
if (readIn != '\n') {
line[stringIndex] = readIn;
stringIndex++;
} else {
line[stringIndex] = '\0';
Serial.print("debug: ");
Serial.print(i++);
Serial.print(" ");
Serial.println(line);
if (strcmp(UID, line) == 0) {
Serial.println("found equal");
cardPresent = 0;
break;
} else {
Serial.println("did not find equal");
cardPresent = 1;
}
stringIndex = 0;
}
}
//Close and flush file
cardsFile.close();
digitalWrite(chipSelect, HIGH);
}
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
I'm sorry I know it's quite a bit of code, but I want to make sure you get the full picture.
What it essentially does is wait for a rfid card to be scanned formats the UID into string and then array ready for various future use. It then opens and reads a file with a list of cards and will check line by line to check if the card is registered in the txt file.
The rfid initializes fine and the sd initializes fine(I was having issues with that before which I resolved with bring SS pins HIGH in setup and also using a different SD card), I can scan a card however, when it gets to opening the test file to read, it outputs an error. (Code below the attempt to read the file may have errors, I have not managed to test that yet as I'm at a roadblock with opening and reading, so no need to worry about that)
This is my output (error is last 2 lines) I am unsure as to what these error codes mean? I could not find an error code list anywhere. Also I know that the test.txt file can be opened as I did it with the SDFat readWrite example which I adjusted to just read (that code did not include any rfid related code)
Initialising SD Card
SD Card initialised
card detected
card ID is C399E6D9
error: opening test.txt for read failed
SD errorCode: 0X4,0XFF
I am absolutely baffled as of yet and been headbanging real hard> I may try adding the diode and pull up resistors but I have a feeling I may be doing something wrong in my code.
I will probably follow this diagram example when adding the resistors and diode, except with 2 devices
Thanks guys, I appreciate any help!