如何获得我的代码以等待我扫描第二个RFID?

时间:2019-05-12 04:23:44

标签: c++ rfid

我是否可以将代码暂停在两个if语句的中间,以便我可以扫描第二个RFID,以便代码可以继续使用第二个RFID的信息继续执行第二个if语句?

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

  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) {


     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));

  }
  Serial.println();
  //Serial.print("Message : ");
  content.toUpperCase();
  int count = 0;

  if (content.substring(1) == "89 E7 75 47") 
  {
    Serial.println("Authorized access"); 
    Serial.println();
    digitalWrite(yellowLED, HIGH);
    delay(1000);
    digitalWrite(yellowLED, LOW);

  count = count + 1;
  }
 else   {
    Serial.println(" Access denied");
    Serial.println();
    digitalWrite(redLED, HIGH);
    delay(2000);
    digitalWrite(redLED, LOW);


if (content.substring(2) == "F9 9A 76 47")
  {
    Serial.println("Authorized access"); 
    Serial.println();
    digitalWrite(yellowLED, HIGH);
    delay(1000);
    digitalWrite(yellowLED, LOW); 
    count = count + 1;
  }
 else   {
    Serial.println(" Access denied");
    Serial.println();
    digitalWrite(redLED, HIGH);
    delay(1000);
    digitalWrite(redLED, LOW);
  }

if (count == 2 ) {
    digitalWrite(greenLED, HIGH);
    delay(3000);
    digitalWrite(greenLED, LOW);

}

}
}

我想在代码持续时间内扫描两个RFID,但是在我扫描完一个RFID之后,整个代码都会通过。

1 个答案:

答案 0 :(得分:0)

如果要等到数据可用,可以使用Serial.available()。这是一个方便的函数,可返回bool

if ( ! mfrc522.PICC_IsNewCardPresent())
{
  return;
}

while(! Serial.available()){}

// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {

  return;
}

请记住,应该在下一个循环之前完全读取串行缓冲区,或者应该调用Serial.flush()