首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何仅在RFID卡存在或不存在时执行一次代码

如何仅在RFID卡存在或不存在时执行一次代码
EN

Stack Overflow用户
提问于 2018-08-27 12:23:49
回答 2查看 1.7K关注 0票数 0

我的代码非常简单,但是我不知道如何做一个简单的任务。

每次读取RFID卡时,我希望它触发一次事件。这在隔离状态下工作得很好。

但是,我也希望每次取走一张卡时都会发生一个不同的一次性事件。这一点似乎把整个事情搞砸了。

我正在使用MFRC522库。

谁能告诉我在同一个代码中做这两件事的方法?我对这一切都是新手。

非常感谢:)我的代码在这里:

代码语言:javascript
复制
#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  
}

此外,我还尝试了修改后的代码,如下所示。这也不起作用。它打印卡片时没有问题,但当卡片被取出时,连续发生多次打印:

代码语言:javascript
复制
    #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;
    }
  }
}
EN

回答 2

Stack Overflow用户

发布于 2018-08-28 01:53:16

也许,您可以将它们组合到一个if-else语句中,而不是拥有2个if耐力。您还希望在条件语句中使用在顶部创建的变量executed

以下是我的看法:

代码语言:javascript
复制
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;
  }
}
票数 2
EN

Stack Overflow用户

发布于 2020-05-02 03:02:24

我发现了一种可能的解决方案,使用"CurrentCard“作为变量来存储当前的卡UID,并使用"CardTimeOut”作为时间限制变量来在您离开卡时重置存储的UID。希望这对你有帮助!

代码语言:javascript
复制
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;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52032537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档