title: RFID can't read tag ID and data at the same time
body: <p>Win-10, Arduino IDE 1.8.13, Arduino NANO, MFRC522 Library.
  All code based on the provided library samples.</p>
  <p>GOAL: I want to read the card ID + read + write to a card without having to remove and re-enter the antenna field.</p>
  <p>I wrote separate sketches for each operation and each works independently. When I combined the code into one sketch it will read the card ID but fails to do the read/write functions. If I comment out all but one call I can ReadCardData() or WriteCardData() or CardId(). I know it has to do with how the card is detected/halted but I can't figure out the right combination. Any suggestions on what to do?</p>
  <p>QUESTION: Is there a way I can read the card ID (sector 0 block 0) and extract the value without having to use the IsNewCardPresent()?</p>
  <p>Some unrelated code has been cropped .......
  data results placed in declared variables.</p>
  <p>loop from combined functions</p>
  <pre><code>     void loop() {
    if(digitalRead(DropOff)== LOW){
      digitalWrite(Red, HIGH);
    }else{
      digitalWrite(Red, LOW);
    }
    if(digitalRead(PickUp)== LOW){
      digitalWrite(Green, HIGH);
    }else{
      digitalWrite(Green, LOW);
    }
    // print the string when a newline arrives:
    delay(1000);
    cardTime = millis();
    A = cardTime - holdTime;
    if (A &gt;= 2000){
      Serial.print(&quot;A = &quot;);
      Serial.println(A);
       // WriteCardData();
       // delay(3000);
       //CardId();
        ReadCardData ();
        // WriteCardData();
        holdTime = cardTime;
    } else {
      //holdTime = cardTime;
    }
  </code></pre>
  <p>all these function work</p>
  <pre><code>void CardId(){
  
  if (  mfrc522.PICC_IsNewCardPresent()){
      //Serial.println(F(&quot;A new card is Present&quot;));
    mfrc522.PICC_ReadCardSerial();
    tagString = &quot;&quot;;
    tagString = String(mfrc522.uid.uidByte[0]);
    tagString = tagString + String(mfrc522.uid.uidByte[1]);
    tagString = tagString + String(mfrc522.uid.uidByte[2]);
    tagString = tagString + String(mfrc522.uid.uidByte[3]);
    Serial.print(&quot;UID = &quot;);
    Serial.println(tagString);
    //closeCard();
    delay(200);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(tagString);
  } // end main if
  else {
    closeCard();
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(&quot;no card&quot;);
  }
  } // end function
  
  // **************************************************************
  void ReadCardData (){
      String myTest = &quot;&quot;;
      myTest = openCard();
      Serial.println(myTest);
      carType = ReadBlock(1,4,7);
      Serial.print(&quot;carType = &quot;);
      Serial.println(carType);
      carColor = ReadBlock(1,5,7);
      Serial.print(&quot;carColor = &quot;);
      Serial.println(carColor);
      carName = ReadBlock(1,6,7);
      Serial.print(&quot;carName = &quot;);
      Serial.println(carName);
      carNumber = ReadBlock(2,8,11);
      Serial.print(&quot;carNumber = &quot;);
      Serial.println(carNumber);
      carOwner = ReadBlock(2,9,11);
      Serial.print(&quot;carOwner = &quot;);
      Serial.println(carOwner);
      closeCard();
  }
  
  // *************************************************************
  void WriteCardData (){
      openCard();
      isGood = WriteBlock(carType,4,7);
      //Serial.print(&quot;carType = &quot;);
      Serial.println(isGood);
      isGood = WriteBlock(carColor,5,7);
      //Serial.print(&quot;carColor = &quot;);
      Serial.println(isGood);
      isGood = WriteBlock(carName,6,7);
      //Serial.print(&quot;carName = &quot;);
      Serial.println(isGood);
      isGood = WriteBlock(carNumber,8,11);
      //Serial.print(&quot;carNumber = &quot;);
      Serial.println(isGood);
      isGood = WriteBlock(carOwner,9,11);
      //Serial.print(&quot;carOwner = &quot;);
      Serial.println(isGood);
      closeCard();
  }
  
  
  
  
  // ***************************************************************************
  void dump_byte_array(byte *buffer, byte bufferSize) {
      for (byte i = 0; i &lt; bufferSize; i++) {
          //Serial.print(buffer[i] &lt; 0x10 ? &quot; 0&quot; : &quot; &quot;);
          //Serial.print(buffer[i], HEX);
          msgArray[i] = buffer[i];
         //Serial.print(msgArray[i]);
      }
  
  }
  
  // **********************************************************************
  String openCard(){
   /* int z = 0;
     for (int i=0; i&lt;5; i++){
           if ( mfrc522.PICC_IsNewCardPresent()){
              break;
           }else{ 
                z = ++z; 
                if (z = 4){
                   return &quot;loop failed&quot;;
                }   
           }
      }
      if ( ! mfrc522.PICC_ReadCardSerial()){
          return &quot;read failed&quot;;
      }
      */
      while ( ! mfrc522.PICC_IsNewCardPresent()) {
       //do nothing
    }
    mfrc522.PICC_IsNewCardPresent();
  return &quot;got it!&quot;;
  }
  // ********************************************************************
  void closeCard(){
       // Halt PICC
      mfrc522.PICC_HaltA();
      // Stop encryption on PCD
      mfrc522.PCD_StopCrypto1();
  }
  // *********************************************************************
  String ReadBlock (int sector, int blockAddr, int trailerBlock){
   
      //MFRC522::StatusCode status;
      byte buffer[18];
      byte size = sizeof(buffer);
      // Authenticate using key A
      //Serial.println(F(&quot;Authenticating using key A...&quot;));
      status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &amp;key, &amp;(mfrc522.uid));
      if (status != MFRC522::STATUS_OK) {
           //Serial.print(F(&quot;PCD_Authenticate() failed: &quot;));
           //Serial.println(mfrc522.GetStatusCodeName(status));
          return &quot;failed&quot;;
      }
      // Read data from the block
       //Serial.print(F(&quot;Reading data from block &quot;)); Serial.print(blockAddr);
       //Serial.println(F(&quot; ...&quot;));
      status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &amp;size);
      if (status != MFRC522::STATUS_OK) {
          // Serial.print(F(&quot;MIFARE_Read() failed: &quot;));
          // Serial.println(mfrc522.GetStatusCodeName(status));
          return &quot;failed&quot;;
      }
      //Serial.print(F(&quot;Data in block &quot;)); Serial.print(blockAddr); Serial.println(F(&quot;:&quot;));
      dump_byte_array(buffer, 16); Serial.println();
  
      Serial.println();
      String q = &quot;&quot;;
      int t = 0;
      int w = 0;
      for (int i=0; i&lt;16; i++){
        t = msgArray[i] + 0;
        if (t&lt;32 || t&gt;126)        // check for random byte values that aren't acceptable ASCII
          {
             msgArray[i] = 32;    // change bad value to a space bar
          }
      }    
      q = msgArray;    
      q.trim();                   // get rid of trailing spaces
      w = q.length();
       //Serial.print(&quot;size of array = &quot;);
       //Serial.println(w);
       //Serial.println(q);
      if (w &lt; 1){                 // test for empty string -- will pass random printable ASCII
        return &quot;unknown&quot;;
      }else{
  
        return q;
      }
  }
  // ********************************************
  String WriteBlock (String storeData, int blockAddr, int trailerBlock){
      // Serial.println(&quot;enter function&quot;);
      storeData.trim();
      int w = storeData.length();
      if (w &lt; 16) {
        for (int i=0; i &lt; (16-w); i++){
          storeData += &quot; &quot;;
        }
        // Serial.println(&quot;pad data&quot;);
        for (int i=0; i &lt; 16; i++){
          myBlock[i] = storeData[i];
        }
      }
      else if (w &gt; 16){
         for (int i=0; i &lt; 16; i++){
          myBlock[i] = storeData[i];
        }
        // Serial.println(&quot;remove data&quot;);
      }
      //MFRC522::StatusCode status;
      byte buffer[18];
      byte size = sizeof(buffer);
      // Authenticate using key A
      // Serial.println(F(&quot;Authenticating using key A...&quot;));
      status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &amp;key, &amp;(mfrc522.uid));
      if (status != MFRC522::STATUS_OK) {
          //Serial.print(F(&quot;PCD_Authenticate() failed: &quot;));
          //Serial.println(mfrc522.GetStatusCodeName(status));
          return &quot;failed&quot;;
      }
   
      // Write data to the block
      // Serial.print(F(&quot;Writing data into block &quot;)); Serial.print(blockAddr);
      // Serial.println(F(&quot; ...&quot;));
      //dump_byte_array(myBlock, 16); Serial.println();
      status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, myBlock, 16);
      if (status != MFRC522::STATUS_OK) {
          // Serial.print(F(&quot;MIFARE_Write() failed: &quot;));
          // Serial.println(mfrc522.GetStatusCodeName(status));
           return &quot;failed&quot;;
      }
      // Serial.println(&quot;end of function&quot;);
  return &quot;good&quot;;
  
  
  
  
  
  }
  // ********************************************
  </code></pre>