need help to slow down a servo,s spped

hi all !

I have a code that make servos go from 0 to 180 degrees but the problem that I have is that I cannot figure out how to slow down the servo,s speed... here is my code... any help would be extremely appreciated !

here is the code:

#include <Servo.h>
const byte switchPinA = 2; //active low
//const byte switchPinB = 3; //active low
const byte servoPin = 9;
Servo servo;
byte switchAstate = HIGH;
//byte switchBstate = LOW;
byte servoPos = 1;
byte posA = 1; // new
byte posB = 180; // new


void setup() {
  pinMode(switchPinA, INPUT_PULLUP);
//  pinMode(switchPinB, INPUT_PULLUP);
  servo.attach(servoPin); 
 
}

void loop () {
   readButtons();
   moveServo();
}

void readButtons() {
   switchAstate = digitalRead(switchPinA);
//   switchBstate = digitalRead(switchPinB);
}

void moveServo() {
   if (switchAstate == LOW) {
     if (servoPos == posA) {  // new
        servoPos = posB;
        delay(2000);
        
  
     }
     else { 
       servoPos = posA;
     }
     switchAstate = HIGH; // new
   }
//   if (switchBstate == LOW) {
//        servoPos = 1;
//   }


   servo.write(servoPos);
   delay(5); // modified
}

Instead of setting the servo's position from 1 to 180 all at once, change it from 1 to 2, wait a little while, then change it from 2 to 3, and so on. The amount of time you wait between changing the servo positions determines how fast it moves.

Take a look at the Servo Sweep example in the IDE

There's no real need (other than as a learning exercise) to hand code this sort of thing. Just try VarSPeedServo.h instead of Servo.h. That has a speed parameter in the write() command.

Steve