我的代码非常简单,但是我不知道如何做一个简单的任务。
每次读取RFID卡时,我希望它触发一次事件。这在隔离状态下工作得很好。
但是,我也希望每次取走一张卡时都会发生一个不同的一次性事件。这一点似乎把整个事情搞砸了。
我正在使用MFRC522库。
谁能告诉我在同一个代码中做这两件事的方法?我对这一切都是新手。
非常感谢:)我的代码在这里:
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
bool executed = false;
void setup() {
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
}
void loop() {
if ( mfrc522.PICC_IsNewCardPresent()) {
if ( mfrc522.PICC_ReadCardSerial()) {
Serial.println("Great!.. each time a card is read and re-read this text will print *ONCE ONLY*");
}
}
// BELOW I WANT TO EXECUTE A COMMAND *ONCE ONLY* EACH TIME A CARD IS TAKEN AWAY
// WHEN UNCOMMENTED, THE "Oh dear.." PRINTS CONTINUOUSLY
// if ( !mfrc522.PICC_IsNewCardPresent()) {
// Serial.println("Oh dear... this seems to keep printing... ");
// }
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
}此外,我还尝试了修改后的代码,如下所示。这也不起作用。它打印卡片时没有问题,但当卡片被取出时,连续发生多次打印:
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
bool executed = false;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
Serial.println("Waiting for RFID-chip...");
}
void loop() {
if ( mfrc522.PICC_IsNewCardPresent()) {
if ( mfrc522.PICC_ReadCardSerial()) {
Serial.println("Great!.. each time a card is read and re-read this text will print *ONCE ONLY*");
executed = true;
}
}
else {
if (executed) {
Serial.println("Oh dear... this seems to keep printing... ");
executed = false;
}
}
}发布于 2018-08-28 01:53:16
也许,您可以将它们组合到一个if-else语句中,而不是拥有2个if耐力。您还希望在条件语句中使用在顶部创建的变量executed。
以下是我的看法:
if ( mfrc522.PICC_IsNewCardPresent()) {
if ( mfrc522.PICC_ReadCardSerial()) {
Serial.println("Great!.. each time a card is read and re-read this text will print *ONCE ONLY*");
executed = true;
}
}
else {
if (executed) {
Serial.println("Oh dear... this seems to keep printing... ");
executed = false;
}
}发布于 2020-05-02 03:02:24
我发现了一种可能的解决方案,使用"CurrentCard“作为变量来存储当前的卡UID,并使用"CardTimeOut”作为时间限制变量来在您离开卡时重置存储的UID。希望这对你有帮助!
String CurrentCard;
int CardTimeOut = 0;
void loop {
// Look for new cards
if (mfrc522.PICC_IsNewCardPresent())
{
CardTimeOut = 0;
if (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();
if (content.substring(1) != CurrentCard)
{
if (content.substring(1) == "D7 48 35 4B") //change here the UID of the card/cards that you want to give access
{
Serial.println("Authorized access");
Serial.println();
//Do something
}
else
{
Serial.println(" Access denied");
}
CurrentCard = content.substring(1);
}
}
}
if (CardTimeOut > 10)
{
CurrentCard = "";
}
CardTimeOut += 1;
}https://stackoverflow.com/questions/52032537
复制相似问题