Servo keeps spinning

Hi, im trying to create a lock using an rfid reader and a servo.
I want the servo to turn 90 degrees when the rfid reader reads the right tag. My problem is that the servo starts spinning around and around as soon as i plug the arduino in. Is my code wrong or do i have a broken servo because the servo is not supposed to be able to spin more than 180 degrees but mine is doing 360s.

#include "SPI.h"
#include "MFRC522.h"
#include "Servo.h"

Servo lock;

#define SS_PIN 10
#define RST_PIN 9
#define SP_PIN 8

MFRC522 rfid(SS_PIN, RST_PIN);

MFRC522::MIFARE_Key key;

int pos = 0;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
  lock.attach(3);
  lock.write(pos);
}

void loop() {
  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial())
    return;

  // Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  // Serial.println(rfid.PICC_GetTypeName(piccType));

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }

  String strID = "";
  for (byte i = 0; i < 4; i++) {
    strID +=
    (rfid.uid.uidByte[i] < 0x10 ? "0" : "") +
    String(rfid.uid.uidByte[i], HEX) +
    (i!=3 ? ":" : "");
  }
  strID.toUpperCase();

  if (strID.substring(1) == "B3:D9:DD:2E")
    {
      lock.write(90);

      delay(5000);

      lock.write(0);
    }
 


  Serial.print("Tap card key: ");
  Serial.println(strID);

  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();
}

There is nothing you can do in code to make a normal servo rotate continuously so if that's what yours is doing then either it is not the servo you think it is or it is broken.

Without any details of what servo you have that's about all I can say.

Steve

The servo I´m using is the SG90 micro servo

Then it is definitely a bad one.

Steve

okay, thanks for the answer

There have been reports of SG90 servos that work as expected when given a signal inside of some range, but when given a signal outside of that range, they spin continuously.

The servo attach command has optional parameters for range limits. I think they are something like 600 to 2400. Try attaching with 1000 and 2000 as the limits. Does the servo still spin continuously? Do a servo.writemicroseconds of 1500. Does it spin?