I am working on an attendance system and for the beginning I wanted to make the comunication between the RFID and microSD (I wanted to put a clock battery later) but they don't work together (alone they do). Here is the code:
//librari
//libraries
#include <SPI.h>
#include <SD.h>
#include <MFRC522.h>
//pini RFID
//RFID pins
#define SS_PIN 10
#define RST_PIN 9
//fisier
//file
File myFile;
//comanda sa mearga RFID
//command needed for RFID
MFRC522 mfrc522(SS_PIN, RST_PIN);
//var globala pt codul de pe rfid
//global variable for the rfid numeric code
int x[4];
void setup() {
//comenzi de inceput
//beginning comanda
Serial.begin(9600);
SPI.begin();
pinMode(SS_PIN, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(SS_PIN, LOW);
digitalWrite(4, HIGH);
//initializare RFID
//initialize RFID
mfrc522.PCD_Init();
//initializare SD card
//initialize SD card
while (!SD.begin(4)) {
Serial.println("initialization failed!");
delay(5000);
}
Serial.println("initialization done.");
}
void loop() {
//variabile pentru SD card
//variable used for SD card
char cod_de_nume[16], nume[31];
int i, s, j=0;
digitalWrite(4, HIGH);
digitalWrite(SS_PIN, LOW);
//Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
//salvare cod rfid
//savind the rfid numeric code
for (i=0; i<=3; i++)
x[i]=mfrc522.uid.uidByte[i];
delay(100);
digitalWrite(SS_PIN, HIGH);
digitalWrite(4, LOW);
//denumire fisier
//giving the name to the file
for (i=0; i<=3; i++)
while (x[i]!=0)
{
cod_de_nume[j]=(char)(x[i]%10+48);
x[i]=x[i]/10;
j++;
}
cod_de_nume[j]='.';
cod_de_nume[j+1]='t';
cod_de_nume[j+2]='x';
cod_de_nume[j+3]='t';
j=j+3;
//debug: verificare daca e corect numele
//debug: checking if the formed file name is correct
Serial.println(cod_de_nume);
//deschidere fisier pentru obtinere de nume
//open the SD to obtain the names from files
myFile = SD.open(cod_de_nume);
//deschidere fisier
if (myFile) {
myFile.read(nume,31);
s=myFile.size();
i=0;
while (i<s)
{
Serial.print(nume[i]);
i++;
}
Serial.println();
}else {
// if the file didn't open, print an error:
Serial.println("fisierul cod_de_nume nu s-a putut deschide");
//in cazul in care nu s-a putut citi fisierul, ce rost mai are sa fie scris in condica
//if the file wasn't open, there's no need to write in the SD card
i=100;
}
myFile.close();
//deschidere fisier pt condica
//open file for attendance
myFile=SD.open("condica.txt", FILE_WRITE);
//scriere in condica
//writing in attendance file
if (myFile && i!=100) {
Serial.print("Writing to file...");
i=0;
while (i<s)
{
myFile.print(nume[i]);
i++;
}
myFile.println();
Serial.println("done.");
} else if (i!=100){
// if the file didn't open, print an error:
Serial.println("error opening condica.txt");
}
myFile.close();
delay (1000);
}
(Some variables are in my language but I made the comments in english as well)
The program should works in the next way: after the setup where it initializes the RFID and SD card, it searches for RFID cards. If it finds one, it saves it's numeric code in a 4 numbers vector and creates a char with it ("xxxxxxxxx.txt") then opens the SD card. If there is a file with the char name, it opens it and copies the name inside in another char , then opens the attendance file ,write the name from the char in it and searches for other RFID cards.
The problem is that, after the SD is initialized, despite being a card on the sensor (or how you call it), it can't find any RFID cards (the command "if ( ! mfrc522.PICC_IsNewCardPresent()) { return;}" keeps being executed). I searched and tried many things but nothing worked.
Here is the circuit: https://i.imgur.com/GxSG7v4.jpg
SD datasheet (my microSD adapter looks different on the back for some reason, but it's the same): http://datalogger.pbworks.com/w/file/fetch/89507207/Datalogger%20-%20SD%20Memory%20Reader%20Datasheet.pdf
RFID datasheet: https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf
Sorry if my english is a bit rusty.