Need help with servo control

Hi, I’m trying to control an SG90 servo motor using a PN532 NFC module.
The servo should rotate 180° when the NFC module detects an NFC card. After 4 seconds, if the NFC module detects another card, it should revert the servo motor to its original position. Now, once the NFC module detects a card, it does move the servo but the servo gets stuck and won’t move.
Am I doing something wrong?

Thanks in advance

Here’s my code

#include <Servo.h>
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>

PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);

Servo myservo;  // create servo object to control a servo
int pos=1; // variable to store the servo position

void setup() {
  nfc.begin();
  nfc.SAMConfig(); // configure board to read RFID tags 
} 

void loop() {

  boolean success;
  uint8_t uid[] = {0, 0, 0, 0, 0, 0, 0}; 
  uint8_t uidLength;
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);

if ((success) and (pos==1)){
  myservo.attach(3); 
  myservo.write(180);
  pos=2;
  delay(4000);

}
else if ((success) and (pos==2)) {
  myservo.attach(3);
  myservo.write(0);
  pos=1;
  delay(4000);

}
}

P.S. I’m new to programming, so please excuse the crappy code.

Why do you think you need to keep attach()ing the already attached servo?

It's silly to test success twice.

if(success)
{
   if(pos == 1)
   {
      // move servo
      pos = 2;
   }
   else if(pos == 2)
   {
      // move servo back
      pos = 1;
   }
}

Using pos, containing 1 or 2, instead of a boolean containing true or false, is less-than-straightforward code.

bool open = false;


if(success)
{
   if(open)
   {
      moveServoToClosePosition();
      open = false;
   }
   else
   {
      moveServoToOpenPosition();
      open = true;
   }
}

See how intuitive that code is?

When you say "detects another card" do you mean "detects a different card" or "detects the same card again"? It may be that it won't show you the same card again.

Do you have two cards? Do they both work for the first step?