Slowing servo movement

Hello to all
I was looking for some help with a kind of similar problem, and came across this topic, and hoped that you could help me.
What I am doing is sending trough wireless the reading from a joystick, and on the other side I am trying to control a servo, but it moves too fast.
What I was trying to do it make it move slower, is it possible ?

Bascaly what I am looking for is something like analogWriteResolution(); but for a servo.

Hope to be of some help too in the future.

I am suggesting to the Moderator to move your question to its own Thread. I don't seem much similarity with the dead Thread you have found.

In any case, if you need help the first thing is to post your code so we can see what you are talking about.
And please use the code button </> so your code looks like this and is easy to copy to a text editor

I have never seen a function called analogWriteResolution() - what library is it in?

Have you successfuly figured out how to send the data wirelessly?

The code in Several Things at a Time illustrates how to manage the speed of a servo.

...R

Thank you for your answer

I am sending the information through wireless without any problem.
The "analogWriteResolution()" i got it from a friend and i'm not using any library for it, so it must be embedded in the ide itself.

The emitter is doing a sampling of the potentiometer readings, and send it over an RF transciever.
On the reciever side, i get the values and map them out, and the use them over the servo library, but what I would like is to get the servo to move slower.

EMITTER:

void sampling() {
  flag_sampling = false;
  soma_x = 0;
  soma_y = 0;
  somaservo_x = 0;
  somaservo_y = 0;
  
  for (int i = 0; i < sizesampling; i++) {

    analogRead(JOYSTICK_X);
    joystickx = analogRead(JOYSTICK_X);
    analogRead(JOYSTICK_Y);
    joysticky = analogRead(JOYSTICK_Y);

    analogRead(SERVO_X);
    servox = analogRead(SERVO_X);
    analogRead(SERVO_Y);
    servoy = analogRead(SERVO_Y);

    soma_x = soma_x + joystickx;
    soma_y = soma_y + joysticky;
    somaservo_x = somaservo_x + servox;
    somaservo_y = somaservo_y + servoy;
    contsampling++;
  }

  if (contsampling > sizesampling - 1) {
    joystick[1] = soma_x / sizesampling;
    joystick[2] = soma_y / sizesampling;
    joystick[3] = somaservo_x / sizesampling;
    joystick[4] = somaservo_y / sizesampling;
    
    flag_sampling = true;
    contsampling = 0;
    soma_x = 0;
    soma_y = 0;
    somaservo_x = 0;
    somaservo_y = 0;
  }
  else
    flag_sampling = false;
}

RECIEVER:

void servo()
{
  sx = map(sx, 0, 1023, 180, 0);      // scale it to use it with the servo (value between 0 and 180)
  myservox.write(sx);                 // sets the servo position according to the scaled value
  //Serial.print(sx);
 
  sy = map(sy, 0, 1023, 180, 0);      // scale it to use it with the servo (value between 0 and 180)
  myservoy.write(sy);                 // sets the servo position according to the scaled value
  //Serial.print(sy);
}


void radio_rx()
{
  delay(25);
  if ( radio.available() ) {
    radio.read( &joystick, sizeof(joystick) );

    if (joystick[0] == 'A') {
      eixo_x = joystick[1];
      eixo_y = joystick[2];
    }
  }

  else
  {
    Serial.println("No radio available");
  }

  x = joystick[1] - eixo_x;
  y = eixo_y - joystick[2];
  
  sx = joystick[3] - servo_x;
  sy = joystick[4] - servo_y;
  
  state = 1;
  flag_rx = false;
}

analogWriteResolution() is an extension of the Analog API for the Arduino Due and Zero.

https://www.arduino.cc/en/Reference/AnalogWriteResolution

Staypuff:
Thank you for your answer

Please post complete programs - not just snippets.

What Arduino(s) are you using?

...R

Hello
The code is too long, it doesnt fit
My emitter is an UNO
The reciever is an DUE

I posted some joystick controlled servo control code in this post.

I think there are a couple functions from the code which you could use to ramp the speed of your servos.

long getJoystickBasedPosition(byte servoIndex, long averageJoystickInput)
{
  long targetSpeed;
  long targetPosition = MIN_PULSE[servoIndex] + ((averageJoystickInput - MIN_POT[servoIndex]) * SERVO_PULSE_RANGE[servoIndex] / (MAX_POT[servoIndex] - MIN_POT[servoIndex]));
  long distanceToGo = targetPosition - servoPosition[servoIndex];

  targetSpeed = computeMaxSpeedWhileStillStopping(ACCELERATION[servoIndex], distanceToGo);
  targetSpeed = constrain(targetSpeed, servoSpeed[servoIndex] - ACCELERATION[servoIndex], servoSpeed[servoIndex] + ACCELERATION[servoIndex]);
  servoSpeed[servoIndex] = constrain(targetSpeed, -MAX_SPEED[servoIndex], MAX_SPEED[servoIndex]);  
  servoPosition[servoIndex] = constrain(servoPosition[servoIndex] + servoSpeed[servoIndex], MIN_PULSE[servoIndex], MAX_PULSE[servoIndex]);
}

long computeMaxSpeedWhileStillStopping(long acceleration, long distance)
{
  long maxSpeed = sqrt(2 * acceleration * abs(distance));
  if (distance < 0)
  {
    maxSpeed *= -1;
  }
  return maxSpeed;
}

You can see there's a bunch of "constrain" commands. The position of the servo is constrained by acceleration, a max speed value and the servo's position limits.

The speed of the servo is also constrained by the max allowed speed with will permit the servo to stop at the desired position. IMO this sort of ramping of speed greatly improves the motion of servos.

The position of the servo is advanced once every 20ms. Here's the function I use to see if it's time to advance the servo's position.

void checkServoTime()
{
  if (micros() - lastServo > SERVO_PERIOD)
  {
    lastServo += SERVO_PERIOD;
    controlServo();
  }
}

The constant "SERVO_PERIOD" is an unsigned long set to the value of 20000. I use the "micros()" micro to keep track of time since time based on "millis()" isn't likely to produce as smooth of motion as the more precise "micros()".

Staypuff:
The code is too long, it doesnt fit

Then add it as an attachment.

...R