I want the servo to be slower ! how do i control the speed of the servo? (servo speed control)

here is the code:

#include <Servo.h>
Servo myservo;
void setup() {
  pinMode(13, OUTPUT );//led and buzzer
  pinMode(2, INPUT);//start Switcher
  pinMode(8, OUTPUT );//relay
  myservo.attach(9);//servo
  myservo.write(90);//it should be slow here
  digitalWrite(8, LOW);
  digitalWrite(13, HIGH);
  delay(1000 * 0.5);
  digitalWrite(13, LOW);


}
void loop() {
  if (digitalRead (2) == HIGH) { 

    digitalWrite(13, HIGH); 
    delay(1000 * 0.5);
    digitalWrite(13, LOW);
    delay(1000 * 0.5);

    delay(1000 * 0.5);


    myservo.write(0);//it should be slow here
    digitalWrite(8, HIGH); 
    delay(3000); 
    digitalWrite(13, HIGH);
    delay(1000 * 25);
    digitalWrite(13, LOW);
    myservo.write(90);//it should be slow here
    delay(1000 * 0.2);
    digitalWrite(8, LOW);
    delay(1000 * 0.5); 

    digitalWrite(13, HIGH);
    delay(1000 * 0.5);
    digitalWrite(13, LOW);
    delay(1000 * 0.5);
    digitalWrite(13, HIGH);
    delay(1000 * 0.5);
    digitalWrite(13, LOW);
    delay(1000 * 0.5);
  

  }



}

:upside_down_face: :space_invader:

What I undestand, myservo.write(pos) is used to move the servo to de position you want (in degrees).

I think there isnt any specific instruction to modify velicity, but you can do something like this:

int pv = 0; //present value i recomend u to set servo on 'home' position and then set pv there
int sp = 90; //set point, the position you want to go
int step_delay = 10; //step delay, time betwen degree and degree

if(pv < sp) { //positive direction
  while (pv < sp) {
    myservo.write(++pv); //first increase pv and then write position to servo
    delay(step_delay);
  }
} else { //negative direction
  while (pv > sp) {
    myservo.write(--pv); //first decrease pv and then write position to servo
    delay(step_delay);
  }
}

That just an example and i haven't tried, then maybe have some buggs.

simple idea - instead just of

myservo.write(90);

do it in few steps

myservo.write(30);
delay(100);
myservo.write(60);
delay(100);
myservo.write(90);

What the others are suggesting is that you gradually approach the new position, not Leap from A to B, then from B to A.
There are many ways to do this, maybe look in the Servo example for "sweep" for hints to try.
Then search this forum and elsewhere for "non blocking delay" to learn about the pitfalls of the delay() function.
C

thanks i will try it :slight_smile:

not a bad idea i will try it :slight_smile:

you could write your own class using the servo library to make a "slow" servo:

/*
   Slow Servo Movements
   based on request of https://forum.arduino.cc/t/i-want-the-servo-to-be-slower-how-do-i-control-the-speed-of-the-servo-servo-speed-control/1012695/
   
   by noiasca https://werner.rothschopf.net/microcontroller/202207_millis_slow_servo.htm
   2022-07-15
*/
#include <Servo.h>

constexpr byte openPin = 4;    //  open door
constexpr byte closePin = 3;   // force close
constexpr byte servoPin = 8;   // GPIO for Servo

// make your own servo class
class SlowServo {
  protected:
    uint16_t target = 90;       // target angle
    uint16_t current = 90;      // current angle
    const uint8_t interval = 50; // delay time
    uint32_t previousMillis = 0;
  public:
    Servo servo;

    void begin(byte pin)
    {
      servo.attach(pin);
    }

    void set(uint16_t newTarget)
    {
      target = newTarget;
    }

    void update()
    {
      if (millis() - previousMillis > interval)
      {
        previousMillis = millis();
        if (target < current)
        {
          current--;
          servo.write(current);
        }
        else if (target > current)
        {
          current++;
          servo.write(current);
        }

      }
    }
};

SlowServo myservo;



void setup() {
  Serial.begin(115200);
  myservo.begin(servoPin);
  pinMode(openPin, INPUT_PULLUP);
  pinMode(closePin, INPUT_PULLUP);
}

void doorOpen()
{
  Serial.println(F("open"));
  myservo.set(90);
  //myservo.servo.write(90); // hardcoded write
}

void doorClose()
{
  Serial.println(F("close"));
  myservo.set(0);
  // myservo.servo.write(0); // hardcoded write
}

void loop() {
  if (digitalRead(openPin) == LOW)
  {
    doorOpen();
  }
  if (digitalRead(closePin) == LOW)
  {
    doorClose();
  }
  myservo.update();  // call the update method in loop
}

servo_slow.ino - Wokwi Arduino and ESP32 Simulator

edit: here is a short description of the slow servo sketch

in general this is exactly how a "Blink without delay" is done.
In other words - get rid of all your delays.
delay = BLOCK
you don't want to BLOCK your code.

There are also other libraries than Servo.h, that allow to set the speed of the servo. E.g. the MoToServo class of my MobaTools library is widely compatible to the Servo.h ( same methods ), but allows you to set the speed of the servo by a simple call to 'myservo.setSpeed(speed)'
You must take into accout, that the methods like 'write' are also not blocking - like the Servo.h calls. So the servo moves, while your sketch moves on. There are special methods to check when the servo has reached its target position ( or where it is positioned at the moment )

There are libraries "MobaTools", "ServoEasing", "ServoSmooth", and "SlowMotionServo", any of which perform servo speed control.

ez sokat segit megpróbálom. de nem teljesen értem mert google forditót használok(Magyarországon lakom ) és az néha furcsán fordit :slight_smile: @MicroBahner

Yes, thanks, I'll try, but which of the four is the best? @johnwasser

Did you try

to translate? In my opinion its much better than google, especially for technical texts.
I also translated your hungarian answer with deepl.com :wink:

They don't come with ratings. Read the descriptions to see which one you want to try first. If that one doesn't do what you want, try another one.

yes I use that too but sometimes it turns out weird:)

ok thanks :slight_smile:

thanks for all the help :slight_smile:
THANK YOU: @MicroBahner , @johnwasser , @noiasca , @camsysca , @b707 , @henuk

There are things to learn here.

  • how to code your approach "better"
  • how to use libraries
    I got the impression from your original post that you wanted to learn the former, not necessarily the latter. The latter will no doubt address the need to "make the servo go slower", but will teach you very little about "how to code a slower approach".
    YMMV, it's all good!
    C