代码:我正在使用MFRC522 1.4.5库和一个Arduino Uno以及一个RFID RC522。 我试图有一个LED灯,每当我扫描RFID模块与卡或钥匙。
另一个注意事项是,我对Arduino中的编码并不是太熟悉,我主要是想学习电路(然而,我确实了解编码概念,因为我有Python和Java的背景,我只是不知道应该如何解释这些错误消息)。
#include <MFRC522.h>
#include <SPI.h>
#define SDAPIN 10
#define RESETPIN 8
#define LED 4
byte FoundTag; // Variable used to check if Tag was found
byte ReadTag; // Variable used to store anti-collision value to read Tag information
byte TagData[MAX_LEN]; // Variable used to store Full Tag Data
byte TagSerialNumber[5]; // Variable used to store only Tag Serial Number
byte GoodTagSerialNumber[5] = {0x22, 0x23, 0x15, 0xD}; // The Tag Serial number we are looking for
MFRC522 nfc(SDAPIN, RESETPIN);
void setup() {
SPI.begin(); // Opens up the communicationto the serial monitor in Arduino IDE
Serial.begin(9600);
pinMode(LED, OUTPUT); // Setup the pin 4 to be the output of LED
// Start to find an RFID Module
Serial.println("Looking for RFID Reader");
nfc.begin();
byte version = nfc.getFirmwareVersion(); // Variable to store Firmware version of the Module
// If can't find an RFID Module
if (! version) {
Serial.print("Didn't find RC522 board.");
while (1); //Wait until a RFID Module is found
}
// If found, print the information about the RFID Module
Serial.print("Found chip RC522 ");
Serial.print("Firmware version: 0x");
Serial.println(version, HEX);
Serial.println();
}
void loop() {
String GoodTag = "False"; // Variable used to confirm good Tag Detected
// Check to see if a Tag was detected
// If yes, then the variable FoundTag will contain "MI_OK"
FoundTag = nfc.requestTag(MF1_REQIDL, TagData);
if (FoundTag == MI_OK) {
delay(200);
// Get anti-collision value to properly read information from the Tag
ReadTag = nfc.antiCollision(TagData);
memcpy(TagSerialNumber, TagData, 4); // the tag information in the TagSerialNumber variable
// Check if detected Tag has the right Serial number we are looking for
for (int i = 0; i < 4; i++) {
if (GoodTagSerialNumber[i] != TagSerialNumber[i]) {
break; // if not equal, then break out of the "for" loop
}
if (i == 3) { // if we made it to 4 loops then the Tag Serial numbers are matching
GoodTag = "TRUE";
}
}
if (GoodTag == "TRUE") {
Serial.println("Success!");
Serial.println();
digitalWrite(LED, HIGH);
delay(1500);
}
else {
Serial.println("TAG NOT ACCEPTED");
Serial.println();
digitalWrite(LED, LOW);
}
delay(500);
}
}
以下是我收到的错误消息:
Arduino: 1.8.12 (Windows 10), Board: "Arduino Uno"
RFID-RC522_LED:10:14: error: 'MAX_LEN' was not declared in this scope
byte TagData[MAX_LEN]; // Variable used to store Full Tag Data
^~~~~~~
C:\Users\jeffo\Arduino\Code\RFID-RC522_LED\RFID-RC522_LED.ino: In function 'void setup()':
RFID-RC522_LED:22:7: error: 'class MFRC522' has no member named 'begin'
nfc.begin();
^~~~~
RFID-RC522_LED:23:22: error: 'class MFRC522' has no member named 'getFirmwareVersion'
byte version = nfc.getFirmwareVersion(); // Variable to store Firmware version of the Module
^~~~~~~~~~~~~~~~~~
C:\Users\jeffo\Arduino\Code\RFID-RC522_LED\RFID-RC522_LED.ino: In function 'void loop()':
RFID-RC522_LED:40:18: error: 'class MFRC522' has no member named 'requestTag'
FoundTag = nfc.requestTag(MF1_REQIDL, TagData);
^~~~~~~~~~
RFID-RC522_LED:40:29: error: 'MF1_REQIDL' was not declared in this scope
FoundTag = nfc.requestTag(MF1_REQIDL, TagData);
^~~~~~~~~~
RFID-RC522_LED:40:41: error: 'TagData' was not declared in this scope
FoundTag = nfc.requestTag(MF1_REQIDL, TagData);
^~~~~~~
RFID-RC522_LED:41:19: error: 'MI_OK' was not declared in this scope
if (FoundTag == MI_OK) {
^~~~~
C:\Users\jeffo\Arduino\Code\RFID-RC522_LED\RFID-RC522_LED.ino:41:19: note: suggested alternative: 'MISO'
if (FoundTag == MI_OK) {
^~~~~
MISO
RFID-RC522_LED:44:19: error: 'class MFRC522' has no member named 'antiCollision'
ReadTag = nfc.antiCollision(TagData);
^~~~~~~~~~~~~
exit status 1
'MAX_LEN' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
RFID-RC522_LED:10:14:错误:未在此作用域中声明“MAX_LEN”
在C++中,变量必须声明。 编译器只是不知道hwat MAX_LEN是。 它没有类型和值,但您试图将它用作数组大小:
byte TagData[MAX_LEN];
大写字母通常用于宏。 所以你很可能错过了一个
#definemax_len4//或您需要任何数字。
则在使用MAX_LEN的任何地方都将其替换为4,从而生成字节标记数据[4];
或者,您必须告诉编译器它的类型和值。
例如int MAX_LEN=4;
RFID-RC522_LED:22:7:错误:“class mfrc522”没有名为“begin”的成员
创建名为NFC
的MFRC522
类的实例
MFRC522 nfc(SDAPIN, RESETPIN);
然后尝试调用
nfc.begin();
但正如错误所说,该类中没有begin方法。
请参阅该类的文档。
其他所有错误都差不多。
在google上快速搜索可以发现,由于某种原因,您正试图在MFRC522
类的实例上使用ADAFRUIT_NFCShield_I2C
类的方法。
如果您不通读类引用而随意地将教程放在一起,通常会发生这种情况。