-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Useful code snippets
Rotzbua edited this page Dec 18, 2017
·
5 revisions
Here you can find useful code snippets created by other users.
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");
}
}