Controlling Servo Speed

you posted the errors again.

please post the code

Greg.... I have managed to solve the issue along with your help.
This code below now allows me to control the speed of the servo in both directions from 0 to 90 and from 90 back to 0.

#include <Servo.h>

Servo servo;
const int analogInPin = A0;
int val=0;               
int servoPin=9;
int angle=90;



void setup() {
servo.attach(servoPin);
}

void loop() {
  val = analogRead(analogInPin);   // This reads the value of the sensor to see if an object is detected. Then saved in the val variable.          
 
  if(val < 600)
  {
  while (angle <= 180)    // command to move from 1 degrees to 90 degrees and 1 equals speed
  {
    servo.write(angle);
    angle++;
    delay(6);               // This controls the speed of the Servo (gate) opening. Lower# lower speed.
  }

  delay(3000);               // Sets the delay time. 3000 means the Servo (gate) stays open for 3 seconds before returning.

    while (angle >= 90)
    {
    servo.write(angle);     // If object detected the Servo rotates to 90 degrees.
    angle--;
    delay(18);               // This controls the speed of the Servo (gate) returning. Lower# lower speed.
    }                   
}
}

Greg, with your help I have managed to solve the problem. I posted the final working sketch. Not sure if you can see it in this forum. If not I can re post the correct working code for your files.

code looks fine

Just wanted to a BIG THANK YOU Greg for your kind help. I really do appreciate it very much. If I was face to face I would shake your hand but since I'm here in Hong Kong and you're there (?) all I can say is........... THANK YOU

Now try this

#include <Servo.h>

Servo servo;
const int analogInPin = A0;
int val = 0;
int servoPin = 9;

void setup()
{
  servo.attach(servoPin);
}

void loop()
{
  val = analogRead(analogInPin);   // This reads the value of the sensor to see if an object is detected. Then saved in the val variable.
  if (val < 600)
  {
    for (int angle = 90 ; angle <= 180; angle++)
    {
      servo.write(angle);
      delay(6);               // This controls the speed of the Servo (gate) opening. Lower# lower speed.
    }
    delay(3000);               // Sets the delay time. 3000 means the Servo (gate) stays open for 3 seconds before returning.
    for (int angle = 180; angle >= 90 ; angle--)
    {
      servo.write(angle);     // If object detected the Servo rotates to 90 degrees.
      delay(18);               // This controls the speed of the Servo (gate) returning. Lower# lower speed.
    }
  }
}

As I mentioned before, the code does not move the servo between 0 and 90 and back, rather it moves it from 90 to 180 and back, but as long as it does what you want, no matter

It should not use delay function for controlling the speed of servo motor. Use millis() instead.

void loop() {
  unsigned long progress = millis() - moveStartTime;

  if (progress <= MOVING_TIME) {
    long angle = map(progress, 0, MOVING_TIME, startAngle, stopAngle);
    myServo.write(angle); 
  }

  // TODO: other code
}

This code is from How to control speed of servo motor

Why not use delay() if it suits the application ?

delay() will make it more difficult to extend. Just imagine the delay() is ok now. But then you want to add something new that should not be blocked to the project, you will take more time to convert delay() to millis()

Using millis() for such a small task is a complication that you can do without

I agree that if you want the sketch to do something else then delay() may not be the right solution but why complicate things unnecessarily ?

To take another example. Would you use Serial.print() in a sketch when, in the future you might want to format the output of more information such that sprintf() might be more appropriate ?

yes, but you need to address the OPs immediate issue and in the context of the OP's level of understanding

there are tons of non-blocking thread on this forum that explain your point