为什么这个Arduino代码会打印;“拒绝访问”;
我对代码有问题。每当我显示Rfid卡1时,它应该打印欢迎人1,但使用它,它会打印else语句中的拒绝访问。在第一个if语句延迟3000秒后,它正在激活else语句,而第二个if语句工作正常 代码如下:为什么这个Arduino代码会打印;“拒绝访问”;,arduino,Arduino,我对代码有问题。每当我显示Rfid卡1时,它应该打印欢迎人1,但使用它,它会打印else语句中的拒绝访问。在第一个if语句延迟3000秒后,它正在激活else语句,而第二个if语句工作正常 代码如下: #include <deprecated.h> #include <MFRC522.h> #include <MFRC522Extended.h> #include <require_cpp11.h> #include <SPI.h>
#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
}
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
String content= "";
byte letter;
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();
if (content.substring(1) == "C7 8B BB 4D")
{
Serial.println("Authorized Access,Welcome Person:1");
Serial.println();
delay(3000);
}
if (content.substring(1) == "A7 5E 63 33")
{
Serial.println("Authorized Access,Welcome Person:2");
Serial.println();
delay(3000);
}
else {
Serial.println("Access denied");
delay(3000);
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义SS_引脚10
#定义RST_引脚9
MFRC522 MFRC522(不锈钢插脚、RST插脚);//创建MFRC522实例。
无效设置()
{
Serial.begin(9600);//启动串行通信
SPI.begin();//启动SPI总线
mfrc522.PCD_Init();//初始化mfrc522
Serial.println(“将您的卡近似于读卡器…”);
Serial.println();
}
void循环()
{
//寻找新卡片
如果(!mfrc522.PICC_IsNewCardPresent())
{
回来
}
//选择一张卡片
如果(!mfrc522.PICC_ReadCardSerial())
{
回来
}
//在串行监视器上显示UID
字符串内容=”;
字节字母;
for(字节i=0;i
您有一个if
语句
紧接着,您有另一个if
语句,它有自己的else
如果您只看第二个If
,您将看到如果它不等于“A7 5E 63 33”
,那么它将打印“拒绝访问”
。你猜怎么着?第一个if
已完成,因此第二个if
将自行处理,并且它不等于该字符串,因此执行else
要解决此问题,第一个if
还需要一个else
——就在第二个if
之前:
if (...) {
}
else if (...) {
}
else {
}
如果
#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
}
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
String content= "";
byte letter;
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();
if (content.substring(1) == "C7 8B BB 4D")
{
Serial.println("Authorized Access,Welcome Person:1");
Serial.println();
delay(3000);
}
else if (content.substring(1) == "A7 5E 63 33")
{
Serial.println("Authorized Access,Welcome Person:2");
Serial.println();
delay(3000);
}
else {
Serial.println("Access denied");
delay(3000);
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义SS_引脚10
#定义RST_引脚9
MFRC522 MFRC522(不锈钢插脚、RST插脚);//创建MFRC522实例。
无效设置()
{
Serial.begin(9600);//启动串行通信
SPI.begin();//启动SPI总线
mfrc522.PCD_Init();//初始化mfrc522
Serial.println(“将您的卡近似于读卡器…”);
Serial.println();
}
void循环()
{
//寻找新卡片
如果(!mfrc522.PICC_IsNewCardPresent())
{
回来
}
//选择一张卡片
如果(!mfrc522.PICC_ReadCardSerial())
{
回来
}
//在串行监视器上显示UID
字符串内容=”;
字节字母;
for(字节i=0;i