我正在尝试在存在RFID标签的情况下仅打印一次,而在将其取下时仅打印一次。当我运行一个脚本时,该脚本可以识别所有标记,如下所示:
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
bool CurrentCardPresentStatus = false; // current state of the button
bool lastCardPresentStatus = false; // previous state of the button
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
}
void loop(){
CurrentCardPresentStatus = mfrc522.PICC_IsNewCardPresent();
if (CurrentCardPresentStatus != lastCardPresentStatus) {
if (CurrentCardPresentStatus == true) {
Serial.println("CARD PRESENT");
} else {
Serial.println("NO CARD");
}
}
lastCardPresentStatus = CurrentCardPresentStatus;
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
}
但是,当我尝试对存储的标签数组执行相同的操作时,就会出现我的问题。我需要向功能mfrc522.PICC_ReadCardSerial();
中添加ObjectTagChecker()
才能访问完整的UID。这为以前的简单检查场景带来了问题。基本上,当我将卡持在读卡器上时,或者我现在同时获得了“检测到的卡”和“没有存储卡”,或者我想要的卡以外的所有其他变体。谁能从我下面的代码中告诉我我哪里出了问题?非常感谢:)
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
char* instrumentUIDs[] = {"E6 7A 78 89", "67 10 DB 2B", "54 8F DA 2B", "84 C2 BE 1E", "A7 0B 75 F2"};
bool CurrentCardPresentStatus = false; // current state of the button
bool lastCardPresentStatus = false; // previous state of the button
//******************************************************************************
void setup() {
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
}
//******************************************************************************
void loop(){
CurrentCardPresentStatus = mfrc522.PICC_IsNewCardPresent();
if (CurrentCardPresentStatus != lastCardPresentStatus) {
if (CurrentCardPresentStatus == true) {
ObjectTagChecker();
} else {
Serial.println("NO CARD PRESENT");
}
}
lastCardPresentStatus = CurrentCardPresentStatus;
//Serial.print(mfrc522.uid.size);
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
}
//******************************************************************************
void ObjectTagChecker() {
mfrc522.PICC_ReadCardSerial();
String content= "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
for (int i = 0; i < (sizeof(instrumentUIDs)/2); i++) {
if (content.substring(1) == instrumentUIDs[i]) {
Serial.println("CARD DETECTED");
}
}
}
非常感谢!