Hey Everyone
Having issues with using the MFRC522 RFID reader with the RF transmitter using VirtualWire or Radiohead libraries.
I am trying to read the NUID from the RFID then send it by RF to another Arduino. I have combined a stripped down version of the ReadNUID example and examples from instructables for the RF transmitter. Both work fine seperately so I know it is wired correctly and that the code should be fine.
I don't think it is a power issue as they are the only two things connected to the Arduino nano?
Could it be a library issue?
It compiles and uploads fine however I did some error testing and as soon as it gets to the following lines it seems to just stop. Not sure if it crashes or is in an infinite loop.
For Virtualwire
vw_setup(4000);
For Radiohead
driver.send((uint8_t *)field, strlen(field));
The codes I used are below,
Virtualwire code
#include <SPI.h>
#include <MFRC522.h>
#include <VirtualWire.h>
#define SS_PIN 10
#define RST_PIN 9
char *field;
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[3];
void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
Serial.println(F("This code scan the MIFARE Classsic NUID."));
Serial.println();
vw_set_ptt_inverted(true); //
vw_set_tx_pin(4);
vw_setup(4000);// speed of data transfer Kbps
}
void loop() {
// Look for new cards
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid.PICC_ReadCardSerial())
return;
if (rfid.uid.uidByte[0] != nuidPICC[0] ||
rfid.uid.uidByte[1] != nuidPICC[1] ||
rfid.uid.uidByte[2] != nuidPICC[2] ||
rfid.uid.uidByte[3] != nuidPICC[3] ) {
Serial.println(F("A new card has been detected."));
if (rfid.uid.uidByte[0] == 54 ||
rfid.uid.uidByte[1] == 70 ||
rfid.uid.uidByte[2] == 247 ||
rfid.uid.uidByte[3] == 48 ) {
field = "1";
}
if (rfid.uid.uidByte[0] == 133 ||
rfid.uid.uidByte[1] == 68 ||
rfid.uid.uidByte[2] == 238 ||
rfid.uid.uidByte[3] == 197 ) {
field = "2";
}
Serial.println(F("The NUID tag is:"));
printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
vw_send((uint8_t *)field, strlen(field));
vw_wait_tx(); // Wait until the whole message is gone
}
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
/**
* Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}
Radiohead code
#include <SPI.h>
#include <MFRC522.h>
#include <RH_ASK.h>
#define SS_PIN 10
#define RST_PIN 9
char *field;
RH_ASK driver;
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[3];
void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
Serial.println(F("This code scan the MIFARE Classsic NUID."));
Serial.println();
}
void loop() {
// Look for new cards
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid.PICC_ReadCardSerial())
return;
if (rfid.uid.uidByte[0] != nuidPICC[0] ||
rfid.uid.uidByte[1] != nuidPICC[1] ||
rfid.uid.uidByte[2] != nuidPICC[2] ||
rfid.uid.uidByte[3] != nuidPICC[3] ) {
Serial.println(F("A new card has been detected."));
if (rfid.uid.uidByte[0] == 54 ||
rfid.uid.uidByte[1] == 70 ||
rfid.uid.uidByte[2] == 247 ||
rfid.uid.uidByte[3] == 48 ) {
field = "1";
}
if (rfid.uid.uidByte[0] == 133 ||
rfid.uid.uidByte[1] == 68 ||
rfid.uid.uidByte[2] == 238 ||
rfid.uid.uidByte[3] == 197 ) {
field = "2";
}
Serial.println(F("The NUID tag is:"));
printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
driver.send((uint8_t *)field, strlen(field));
driver.waitPacketSent();
}
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
/**
* Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}