I don't understand what's wrong with this servo!

I am trying to make a model of an automatic door, But for some reason my servo gets stuck after the first movement. What do I do?

code here:

//Including sounds and extra libraries for ultrasonic sensor and buzzer
#include "pitches.h"
#include <SR04.h>
#include <Servo.h>
// lights
#define RED 6
#define GREEN 5
//ultrasonic
#define TRIG_PIN 10
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;
int pos = 0;    // variable to store the servo position
Servo myservo;  // create servo object to control a servo

int maxnum = 5;     // Blink the LED 20 times
int count = 0;       // Our blink counter
int blinkSpeed = 1000;

void setup() {
  //lights
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  digitalWrite(RED, HIGH);
  digitalWrite(GREEN, HIGH);

  //ultrasonic sensor
  Serial.begin(9600);
  delay(1000);

  //set up servo
    myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}


void loop() {
  //ultrasonic
     a=sr04.Distance();
   Serial.print(a);
   Serial.println("cm");
   delay(100);

//notices someone within distance
     if(a >= 31){
        
        if (count < maxnum) {
          analogWrite(RED,0);
          analogWrite(GREEN,100);
          delay(blinkSpeed);
          analogWrite(RED,0);
          analogWrite(GREEN,0);
          delay(blinkSpeed);
          for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
           // in steps of 1 degree
          myservo.write(pos);              // tell servo to go to position in variable 'pos'
          delay(1000);
                      for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
          delay(100);                      
          }
          analogWrite(RED,100);
          analogWrite(GREEN,0);
          tone(8, 460, 1000);
          delay(500);
          analogWrite(RED,0);
          analogWrite(GREEN,0);
          tone(8, 260, 1000);
          delay(500);
          analogWrite(RED,100);
          analogWrite(GREEN,0);
          tone(8, 460, 1000);
          delay(500);
          analogWrite(RED,0);
          analogWrite(GREEN,0);
          tone(8, 260, 1000);
          delay(500);
        if (count = maxnum) {
                    analogWrite(RED,100);
          analogWrite(GREEN,0);
          delay(blinkSpeed);
          analogWrite(RED,0);
          analogWrite(GREEN,0);
          delay(blinkSpeed);

          count++;  // add one (1) to our count
}

            }        
        }

Be sure to power servos from a separate supply (4x AA batteries is usually good) and don't forget to connect the grounds.

Please use autoformat (ctrl-T) in the IDE before posting code. The servo movement loop structure looks wrong.

The code you've posted doesn't compile so it isn't the code that you're running. I can't see much point commenting on any problems/errors until you post the real code.

Steve

when i indent based on braces ({}), this code doesn't look right

           for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
                // in steps of 1 degree
                myservo.write(pos);

                // tell servo to go to position in variable 'pos'
                delay(1000);
                for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
                    myservo.write(pos);
                    // tell servo to go to position in variable 'pos'
                    delay(15);
                    // waits 15ms for the servo to reach the position
                    delay(100);

                }

AutoFormat (CTRL-T) is often useful for finding mismatched curly braces.
Your code snippet does not appear to have matching curly braces.

Files mentioned in code

HC-SR04-20200128T153220Z-001.zip (3.4 KB)

pitches.h (1.95 KB)

if (count = maxnum)Oops