Tried knocking together some code to allow hard coding of tag IDs which works OK. Might need to change the for loop though as anything in else just gets repeated, a switch might do the trick but it's late.
For anyone interested, here's my code, would appreciate any improvements that could be made, including an scenario where the tag's not recognized as valid.
/*
Example file for communicating with the NFRC522. The program prints the card data.
Created by Eelco Rouw - Originally adapted by Grant Gibson.
*/
// Pinout
// SDA - 2.2
// SCK - 1.5
// Mosi - 1.7
// Miso - 1.6
// IRQ - NC
// GND - GND
// RST - 1.3
// VCC - VCC
#include <Mfrc522.h>
// the sensor communicates using SPI, so include the library:
#include <SPI.h>
int chipSelectPin = 10;
int NRSTDP = 5;
int ledpin1 = 3;
int ledpin2 = 6;
int cardint = 5;
Mfrc522 Mfrc522(chipSelectPin,NRSTDP);
unsigned char serNum[5];
unsigned char sectorKey[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char readData[16];
void setup()
{
Serial.begin(9600); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
// Start the SPI library:
SPI.begin();
// Initialize the Card Reader
digitalWrite(chipSelectPin, LOW);
pinMode(RED_LED, OUTPUT);
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
Mfrc522.Init();
}
void loop()
{
unsigned char i,tmp;
unsigned char status;
unsigned char str[MAX_LEN];
unsigned char RC_size;
unsigned char blockAddr;
//String mynum = "";
String mycardno;
char* myarray[2][5]={ //Array of tags and related names, tag numbers copied from serial console after 1s
{"205928944228","1251291514471","7748944415","157172894468","611918944247"},
{"Dad","Mum","Baby","Dog","Cat"}
};
status = Mfrc522.Request(PICC_REQIDL, str);
if (status == MI_OK)
{
Serial.println("Card detected");
Serial.print(str[0],BIN);
Serial.print(" , ");
Serial.print(str[1],BIN);
Serial.println(" ");
}
status = Mfrc522.Anticoll(str);
memcpy(serNum, str, 5);
if (status == MI_OK)
{
Serial.println("The card's number is : ");
Serial.print(serNum[0], DEC);
Serial.print(" , ");
Serial.print(serNum[1], DEC);
Serial.print(" , ");
Serial.print(serNum[2], DEC);
Serial.print(" , ");
Serial.print(serNum[3], DEC);
Serial.print(" , ");
Serial.print(serNum[4], DEC);
Serial.println(" ");
mycardno = String(serNum[0]) += String(serNum[1]) += String(serNum[2]) += String(serNum[3]) += String(serNum[4]); // Appends the content of the serNum array to give a unique card no
Serial.println(mycardno);
Mfrc522.SelectTag(serNum);
status = Mfrc522.Auth(PICC_AUTHENT1A,1,sectorKey,serNum);
if (status == MI_OK)
{
Serial.println("Authenticated...\r\n");
} else
{
Serial.println("Error authenticating...\r\n");
}
status = Mfrc522.ReadBlock(1, readData);
if (status == MI_OK)
{
for(i=0; i<16; i++)
{
Serial.write(readData[i]);
delay(10);
}
} else {
Serial.println("Error reading.");
}
int i;
for(i = 0; i < 5; i = i + 1)
{
if (mycardno == myarray[0][i])
{
Serial.print("Hello ");
Serial.println(myarray[1][i]); // Should Print out name matching tag
}
/*else {
Serial.println("Bugger Off");
}*/
}
delay(1000);
Mfrc522.Init();
}
//Serial.println(" ");
Mfrc522.Halt();
}
hello i tried your code, but i can not get it to work, i connected it exactly as the one you in your pictures, right now im getting both red and green leds on and nothing changes when i place the cards near the reader, could you help me please!?
[Energia Library] NFC card reading with Energia - MF RC 522
in MSP Energia Libraries
Posted
hello i tried your code, but i can not get it to work, i connected it exactly as the one you in your pictures, right now im getting both red and green leds on and nothing changes when i place the cards near the reader, could you help me please!?