Servo loops 5 times then turns continuously

I am trying to make a continuous servo turn to a specific position. It will be turning a threaded rod that will move a nut up and down the shaft. The code below turns it for a specified time in either direction and stops and loops. The problem is it loops 5 times then it turns the servo continuously clockwise after that. How can I make it loop continuously. Thank you for your help.

#include

Servo myservo;

int start_time;

void setup()
{
myservo.attach(11);
pinMode (13,OUTPUT);
}

void loop()
{
start_time = millis();
while((millis() - start_time) < 1000)
{
myservo.attach(11);
delay(15);
myservo.write(0);
digitalWrite(13, HIGH);
}

start_time = millis();
while((millis() - start_time) < 500)
{
myservo.attach(11);
delay(15);
myservo.write(180);
digitalWrite(13, LOW);
}

start_time = millis();
while((millis() - start_time) < 1000)
{
myservo.attach(11);
delay(15);
myservo.write(0);
digitalWrite(13, HIGH);
}

start_time = millis();
while((millis() - start_time) < 500)
{
myservo.attach(11);
delay(15);
myservo.write(180);
digitalWrite(13, LOW);
}

start_time = millis();
while((millis() - start_time) < 5000)
{
myservo.detach();
delay(15);
myservo.write(180);
digitalWrite(13, LOW);
}
}

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

The first of your problems is right here. You save the millis() value and then immediately see if the difference between the saved value and the current value is less than 1000. It will ALWAYS be a very small value, 1 or 2 milliseconds, at the most. Likely to be 0,
Paul

Try this simplified version of your loop() function:

void loop()
{
    myservo.write(0);
    digitalWrite(13, HIGH);
    delay(1000);

    myservo.write(180);
    digitalWrite(13, LOW);
    delay(500);
}

Thank you so much for you help.

Oi @markarkark

Speaking of servo motors.
The servo motor knows its current position because it has an absolute encoder on the motor shaft.
It features an angle range from 0° to 180°.
The value of in the myservo.write(X) function; X defines the angle of movement that the motor must follow and remain stationary.
The continuous acting servo motor is not really a servo motor, it is a pseudo servo motor.
It does not have an absolute encoder on the motor shaft and, therefore, it does not know its position at every moment of movement.
It has a potentiometer for external adjustment to the central point.
As it is a motor in continuous rotation, it does not have position values in the myservo.write (X) function;

It has the "stop point" with X = 90, and above 90 it rotates clockwise and below 90 it rotates counterclockwise,

To set your engine's stop point at 90, use this sketch.

RV minirin

#include <Servo.h>
Servo myservo;
//--------------------------------------------------
void setup()
{
  Serial.begin(115200);
  myservo.attach(11);
}
//--------------------------------------------------
void loop()
{
  myservo.write(0);
  Serial.println("CW");
  delay(5000);
  myservo.write(90);
  Serial.println("Stop");
  delay(5000);
  myservo.write(180);
  Serial.println("CCW");
  delay(5000);
  myservo.write(90);
  Serial.println("Stop");
  delay(5000);
}