How to control servo speed using a potentiometer ? [SOLVED]

hello,
I have a servo and a potentiometer connected to arduino uno. What I am trying to do is control the speed of the servo but using a potentiometer. I want to know if that is possible ?
and I would like your help please. I am using Servo.h library

here is my code so far:

#include <Servo.h> // add servo library

// Define servo name
Servo throttleServo;

int servo_angle = 0;

int throttlePin = A1;
int servoPin = 5;

int previousPulse = 0;
int currentPulse = 0;

void setup() {
  Serial.begin(9600);
  // Define the servo signal pin, it must be PWM
  throttleServo.attach(servoPin);

  pinMode(servoPin, OUTPUT);
}

void loop() {

  int throttlePotValue = analogRead(throttlePin);
  int pulse = map(throttlePotValue, 0, 1023, 0, 180);
  throttleServo.write(pulse);
  delay(100);
  /* if(pulse == 180) {
    currentPulse = 180;
    previousPulse = 0;
  } else if(pulse == 0) {
    currentPulse = 0;
    previousPulse = 180;
  }
  if(currentPulse == 0) {
    if(pulse > 180) {
      pulse == 180;
    }
    throttleServo.write(pulse +=10); // Turned to 0 degree
  } else if (currentPulse == 180) {
    if(pulse < 0) {
      pulse == 0;
    }
    throttleServo.write(pulse -= 10);
  }*/
  
}
1 Like

By far the simplest way is to use VarSpeedServo.h instead of the standard Servo.h. It has an additional speed parameter in the write() command.

Other than that you will need to use the technique from the Sweep example code. To change the speed just change the value in the delay() command.

Steve

slipstick:
By far the simplest way is to use VarSpeedServo.h instead of the standard Servo.h. It has an additional speed parameter in the write() command.

Other than that you will need to use the technique from the Sweep example code. To change the speed just change the value in the delay() command.

Steve

slipstick !!! thank you so much it worked !
i did not know that library existed, i love it ! karma++