Code Doesn't do What I Want it to do

I am working on a project that uses the MFRC522 RFID scanner. I have got everything wired correctly and i know it all works fine. Originally, I had it to only make a green LED come on and Buzz if the card was allowed, or if the card isn’t allowed, it would make a red LED come on and buzz. That all worked fine until I tried to add on a servo that would rotate to “unlock” something and then wait 10 seconds before closing again. This is where I began to run into problems. I was lazy so I used code from an example code. That code made the servo rotate 180 degrees and then go back. My end goal was to be able to put a card to the scanner and (if it was allowed), a green LED would light up and the buzzer would go off and a servo motor would rotate allowing you to open the door. Here is my code:

  //System setup: 
  //SDA - Pin 53
  //SCK - Pin 52
  //MOSI - Pin 51
  //MISO - Pin 50
  //RQ - N/A
  //GND - Ground Pin on the "DIGITAl" pins
  //RST - Pin 5
  //3.3 V - 3.3 V pin on the "POWER" pins

  //Card Number - 99 A2 C5 99
  //Tag Number - 39 1E 03 B9

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
 
#define SS_PIN 53
#define RST_PIN 5
#define LED_G 6 //define green LED pin
#define LED_R 4 //define red LED
#define BUZZER 2 //buzzer pin
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
Servo myservo;

int pos = 0;

void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  Serial.println("Put your card to the reader...");
  Serial.println();
  myservo.attach(9);
}
void loop() 
{
  // 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();
  if (content.substring(1) == "99 A2 C5 99"  )
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    {for (pos = 180; pos >= 0; pos -= 1) {
    myservo.write(pos);
    delay(10000);
  }}
    digitalWrite(LED_G, HIGH);
    tone(BUZZER, 500);
    delay(300);
    noTone(BUZZER);
    for (pos = 180; pos >= 0; pos -= 1) {
    myservo.write(pos);
    delay(10000);
    {for (pos = 0; pos <= 180; pos += 1) { 
    myservo.write(pos); 
  }}
  }
     for (pos = 0; pos <= 180; pos += 1) { 
    myservo.write(pos); 
  }
    delay(5000);
    digitalWrite(LED_G, LOW);
  }
 
 else   {
    Serial.println(" Access denied");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(1000);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }
}

So the minimum what you can do yourself ist to describe what happens if you run your code.

I began to run into problems.

And if you describe those problems we may be able to help.

Are you powering the servo from the Arduino supply or an external supply that has the correct voltage and current capacity?

My problem is that when I scan the card, the servo will go where it needs to but then it all stops and nothing else happens. And I am running the servo off of the 3.3V pin on my Arduino 2560 Mega

" I am running the servo off of the 3.3V pin on my Arduino 2560 Mega"
That only supplies 150mA, typically way insufficient for a servo.

I suggest that you get an external supply that has the correct voltage and current capacity to drive a servo?

But that's not the problem, the servo works great, it's my code that isn't working properly. That is what I am asking about.

the servo works great,

Please mark the topic “solved”

The servo was never the problem. The code is the problem. I don’t understand what is wrong with the code. The problem is not solved.

for (pos = 180; pos >= 0; pos -= 1)
         {
            myservo.write(pos);
            delay(10000);
         }

How long will it take for this for loop to execute? 10 times 180 seconds. Is that on purpose?

for (pos = 180; pos >= 0; pos -= 1)
      {
         myservo.write(pos);
         delay(10000);    //  another 10 * 180 seconds wait.
         {
            for (pos = 0; pos <= 180; pos += 1)
            {
               myservo.write(pos);
            }
         }
      }

What does that do?

You have some extra { and }. If you use the IDE autoformat tool (ctrl-t or Tools, Auto Format) the code will be easier to follow. One should never see stuff like }}.

Ok, thanks, I'll check that out.

for (pos = 180; pos >= 0; pos -= 1)
      {
         myservo.write(pos);
         delay(10000);    //  another 10 * 180 seconds wait.
         {
            for (pos = 0; pos <= 180; pos += 1)
            {
               myservo.write(pos);
               delay(5);
            }
         }
      }

I have edited @groundFungus’s code. It needs a delay in the second for() function. Or the servo won’t get time to first get to its initial position. See I have added the delay which is safe for the servo. It doesn’t add much delay. It delays only until the servo gets to its initial position.

@ArnavPawarAA - You may wish to reconsider nested loops using the same loop control variable.
And “for” is not a function.

I have edited @groundFungus’s code

Wrong again