So.. I'm building an attendance logger with an arduino, sd card shield, and RFID module.
This is the code I modified for checking the data inside my rfid card (on block 1)
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 53
#define RST_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
byte readbackblock[16];
//byte erase[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//all zeros. This can be used to delete a block.
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("Read data");
//type A
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xAA;
}
//type B
for (byte i = 0; i < 6; i++) {
key.keyByte[i + 10] = 0xAA;
}
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
Serial.println("card present");
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.println("correct key");
readBlock(1, readbackblock);//read the block back
Serial.print("read block: ");
for (int j = 0 ; j < 16 ; j++) //print the block contents
{
Serial.write (readbackblock[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
}
Serial.println("");
}
I have my own function.ino for the readBlock(x,y);
This code works. But when I load this program (a code that writes on the sd card, which works too)
#include <SPI.h>
#include <MFRC522.h>
#include <SD.h>
#define SS_PIN 53
#define RST_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
File myFile;
byte readbackblock[16];
char n;
//byte erase[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//all zeros. This can be used to delete a block.
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("Text to file in SD");
//type A
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xAA;
}
//type B
for (byte i = 0; i < 6; i++) {
key.keyByte[i + 10] = 0xAA;
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
Serial.println("card present");
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.println("correct key");
myFile=SD.open("test.txt",FILE_WRITE);
if (myFile) {
Serial.print("Writing to test.txt...");
readBlock(1,readbackblock);
for(int i=0;i<16;i++){
n=(char)readbackblock[i];
myFile.print(n);}
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
then load back the rfid reader program, it doesn't work anymore... unless I plug-unplug the arduino.
What do you think is the problem?
Thanks!