-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Useful code snippets
CptAwe edited this page May 28, 2018
·
5 revisions
Here you can find useful code snippets created by other users. If you add new code snippets please use the template which can be found here.
Source: https://github.com/miguelbalboa/rfid/issues/352
Credits: https://github.com/metamorphious
Used environment:
- OS version: Win 10
- Arduino IDE version: 1.8.1
- MFRC522 Library version: 1.3.6
- Arduino device: Arduino nano
- MFRC522 device: RFID-RC522
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
bool rfid_tag_present_prev = false;
bool rfid_tag_present = false;
int _rfid_error_counter = 0;
bool _tag_found = false;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
}
void loop() {
rfid_tag_present_prev = rfid_tag_present;
_rfid_error_counter += 1;
if(_rfid_error_counter > 2){
_tag_found = false;
}
// Detect Tag without looking for collisions
byte bufferATQA[2];
byte bufferSize = sizeof(bufferATQA);
// Reset baud rates
mfrc522.PCD_WriteRegister(mfrc522.TxModeReg, 0x00);
mfrc522.PCD_WriteRegister(mfrc522.RxModeReg, 0x00);
// Reset ModWidthReg
mfrc522.PCD_WriteRegister(mfrc522.ModWidthReg, 0x26);
MFRC522::StatusCode result = mfrc522.PICC_RequestA(bufferATQA, &bufferSize);
if(result == mfrc522.STATUS_OK){
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return;
}
_rfid_error_counter = 0;
_tag_found = true;
}
rfid_tag_present = _tag_found;
// rising edge
if (rfid_tag_present && !rfid_tag_present_prev){
Serial.println("Tag found");
}
// falling edge
if (!rfid_tag_present && rfid_tag_present_prev){
Serial.println("Tag gone");
}
}
Source: https://github.com/miguelbalboa/rfid/issues/384
Credits: https://github.com/CptAwe
Used environment:
- OS version: Win 10
- Arduino IDE version: 1.8.5
- MFRC522 Library version: 1.4.0
- Arduino device: Arduino UNO
- MFRC522 device: RFID-RC522
byte Card[16][4];
// fills the "Card[16][4]" with 0s
void ResetInfo(){
for (int i=0; i<=15; i++){
for (int j=0; j<=4; j++){
Card[i][j]=0;
}
}
}
// fills the "Card[16][4]" with data
void ReadInfo() {
ResetInfo();
MFRC522::StatusCode status;
byte buffer[18]; // 16 (data) + 2 (CRC)
for (byte page=0; page<=15; page+=4){
byte byteCount = sizeof(buffer);
status=NFC.MIFARE_Read(page,buffer,&byteCount);
if (status != NFC.STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(NFC.GetStatusCodeName(status));
return;
}
// [page][index]
// 0-15 0-3
// Card [16] [4]
//
// 0-3 page 0
// 4-7 page 1
// 8-11 page 2
// 12-15 page 3
// buffer[16+2]
int i_=0;
for (int i=page; i<=page+3; i++){
for (int j=0; j<=3; j++){
Card[i][j]=buffer[4*i_ + j];
}
i_++;
}
}
// This is to stop the card from sending the info over and over again
NFC.PICC_HaltA();
}
// shows the "Card[16][4]" after filling it
void ShowInfo(){
ReadInfo();
Serial.println("--------------------------");
for (int i=0; i<16; i++){
for (int j=0; j<4; j++){
Serial.print(Card[i][j],HEX);
Serial.print(" ");
}
Serial.println();
}
Serial.println("--------------------------");
}