仅在存在或不存在RFID卡的情况下如何执行一次代码

时间:2018-08-27 04:23:49

标签: arduino rfid

我的代码非常简单,但是我不知道如何执行简单的任务。

每次读取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;
    }
  }
}

2 个答案:

答案 0 :(得分:2)

也许不用2个 if 常量,而是可以将它们组合为 if-else 语句。您还希望使用在条件句顶部创建的变量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;
  }
}

答案 1 :(得分:0)

我找到了一种可能的解决方案,使用“ 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;
    }