简体   繁体   中英

Arduino C++ String variables with RFID

I'm trying to scan a RFID tag to add it to a variable string named RFID1. However, it keeps saying access denied as if it were still in the main loop when scanning the RFID card when it is in a do while loop in another void function.

I'm not sure why this is happening..

 do{ // DO WHILE STATEMENT FOR ADDING RFID
          if
           // 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();

            RFID1 == content.substring(1); // RFID USER 1 = RFID TAG
            delay(3000); // WAIT 3 SECONDS
            user1AddLoop + 1; // BREAK OUT OF LOOP

            } // END OF DO WHILE STATEMENT FOR ADDING RFID
            while(user1AddLoop == 0 );

^^ Function for trying to add RFID to the variable. ^^

 // ---- RFID CODE ---- //

  // 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 : SCAN YOUR RFID TAG");
  content.toUpperCase();
  if (content.substring(1) == RFID1 || content.substring(1) ==  RFID2 || content.substring(1) ==  RFID3) //change here the UID of the card/cards that you want to give access
  {
   rfidOpen();  
      }


 else   {
    rfidDeny();
  }

^^ Code for searching RFID to open door in main loop ^^

I am aware they use the same code to search for RFID, however, I cannot understand why it keeps giving me access denied as if it was in the void loop() instead of in it's own function, named rfidMenu() which is called from the rfidOpen() void as seen in the code.

This is my code:

void readCard()
{
  cardContent = "";
  if ( ! mfrc522.PICC_IsNewCardPresent() )
    return;
  if ( ! mfrc522.PICC_ReadCardSerial() )
    return;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    cardContent.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    cardContent.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  cardContent.toUpperCase();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM