How to keep rfid reader triggered after one tap

hi there, I am very new to programming with Arduino. I am currently trying to create a door opening and closing system. instead of running my servo() function in one go, I have to tap the rfid tag every time to go through the function. I am unsure to why that is. can someone please help me out. would really appreciate it. also ignore the lcd display bit, I have no idea what is going on there lol. this is my code

void loop() {
  
 //don't run the loop if card is not detected
    if (! rfid.PICC_IsNewCardPresent()) 
    return;
  
    //check if NUID has been detected
    if(! rfid.PICC_ReadCardSerial())
    return;
         /////// body of the RFID code ////////
    //Store userID in newID array
    for (byte i = 0; i < 4; i++)  {
      newID[i] = rfid.uid.uidByte[i];
    }
  
 //check if ID matches the database
 if (accessDoor(userID, newID) == true && servoState == 0)  {
    Serial.println ("Access Granted");
      servoState = 1;
      previousTime = millis();
    } servo();
    
   if (accessDoor(userID, newID) == false ) {
    Serial.println("Access Denied");
    

  }

  /////// to only print once //////////
  //Halt PICC
  rfid.PICC_HaltA();
  //Stop encryption on PCD
  rfid.PCD_StopCrypto1(); 

}

bool accessDoor (byte accessCode[], byte newCode[])  {
  for (byte i = 0; i< 4; i++)  {
    if (newCode[i] != accessCode[i])  {
      return false;
    }
  }
  return true; 
}
void flashing_green()  { 
  if (millis() - previousBlink >= flashTime) {
     if (greenFlash == 0) { // if light is off
        greenFlash = 255; // turn light on
     }else {
      greenFlash = 0; // or else turn off
    }
     lights (0, greenFlash);
     previousBlink = millis();
     //Serial.println("flashing green");
   }
   
}

void flashing_red()  {
  if (millis() - previousBlink >= flashTime) {
     if (redFlash == 0) { // if light is off
        redFlash = 255; // turn light on
     }else {
      redFlash = 0; // or else turn off
    }
     lights (redFlash, 0);
     previousBlink = millis();
     //Serial.println("flashing red");
   }
}

void lights(int red, int green)  {
  analogWrite (redRGB, red);
  analogWrite (greenRGB, green);
}

void servo()  {
 switch (servoState)  {
     case 0:
     {
      lights(255, 0);
      Serial.println("waiting for tag");
     break;
     }
     case 1:
     {
      flashing_green();
      Serial.println ("flashing green");
      if (millis() - previousTime >= servoDelay)  {
         pos += increment;
        myservo.write(pos);
        Serial.println(pos);
        //Serial.println ("case 1");
        previousTime = millis();
        if (pos > 80) {
          servoState = 2;
        }
       }
     break;
     }
     case 2:
     {
      lights(0, 255); // light is green to enter
        Serial.println ("green light on");
      if (millis() - previousTime >= interval) { //if 2secs have passed, go to state 3
       previousTime = millis();
       servoState = 3;
      }
     break;
     }
     case 3:
     {
        flashing_red();
        Serial.println("flashing red, closing");
      if (millis() - previousTime >= servoDelay)  {
         pos -= increment;
        myservo.write(pos);
        Serial.println(pos);
        previousTime = millis();
          if (pos == 0) {
            lights (0,0);
            Serial.println ("door closed");
          previousTime = millis(); 
          servoState = 0;
        }
       }
      break;
      }
     }
    }

Move your call to the 'servo()' function to BEFORE:

//don't run the loop if card is not detected

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.