Controlling the speed of a servo

I am using the servo library and a program to move a 180 degree servo from the 0 position to the 90 degree position and then back to 0. I use a delay to sort of determine the time it takes to do so. I would like to come up with a way perhaps a pot that I can change the delay to speed up or slow down that action. Is there a way? Thanks in advance.

Have a look at the servo sweep example that comes with the Arduino IDE. There is also servo code in the demo Several Things at a Time

You could change the interval between movements based on the value from analogRead()

...R

I am using the servo library and a standard 180 servo in an up / down motion. In other words I am using write commands and having the servo move from the "o" position to the 90 degree position and back to the "o". I use a delay to sort of control the speed in which it does so. I would like to be able to vary that speed perhaps with a pot or some other method. I would like to " dial in" different up and down speeds. Is this possible. Thank you

Cross posted here.

@rayluc, do not cross-post. Threads merged.

Sorry about the cross reference . I am new. In any case I have looked at the SWEEP. I dont see how that gets at my answer. It is using delays or pulses as well. An example… servoLeft. write(0);
delay(300); servoLeft.write(90);
delay(300);
servoLeft write(0); delay (300); Some the servo moves from o to 90 and back to o with a delay of 300. Now if I had some sort of way to change that delay value with a pot a knob something I would have what I need. Any ideas? I want to do an up and down motion but a way to change the speed in which it does it.

Here is an example to show how to vary a delay time using a potentiometer.

const byte analogInPin = A0;
const byte ledPin = 4;

void setup()
{
   Serial.begin(115200);
   pinMode(ledPin, OUTPUT);
}

void loop()
{
   int desiredDelay = analogRead(analogInPin); // read pot.  will reuturn 0 to 1023
   digitalWrite(ledPin, !digitalRead(ledPin));  // toggle LED state
   delay(desiredDelay);  // delay is variable (from 0 to 1023 milliseconds).
}

A better way would be to use the blink without delay to do the timing in a non blocking fashion. See the beginner's guide to millis.

I have to admit that I do not understand the code to control a servo delay or speed with a pot. Could we call the servo servo. servoLeft and could we use pin 9? If you show me the code that will move a 180 degree standard servo for o degree to 90 degree and back to o using a pot. maybe I will get it. I also don,t know how to set uo \p the arduino R3 uno board with the pot etc. Any reading and any instructions will greatly help and thank you

rayluc:
Sorry about the cross reference . I am new. In any case I have looked at the SWEEP. I dont see how that gets at my answer. It is using delays or pulses as well. An example… servoLeft. write(0);
delay(300); servoLeft.write(90);
delay(300);
servoLeft write(0); delay (300); Some the servo moves from o to 90 and back to o with a delay of 300. Now if I had some sort of way to change that delay value with a pot a knob something I would have what I need. Any ideas? I want to do an up and down motion but a way to change the speed in which it does it.

Where did you get that version of servo sweep?
I googled the phrase “servo sweep”
The very first hist took me to https://www.arduino.cc/en/tutorial/sweep

The code found there looks like this:

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

Can you see in the 2 loops, where it uses delay(15)? If you alter that delay, it will alter the speed of the sweep.

Ok I understand the sweep. I have taken it farther than that. I have my servo moving from 0 to 75 and back with delays. I have the servo going to 0 to 45 with a delay and then to 75 with a delay and back. But that is not quite what I need. I can use code to go to an angle with a delay but I need a pot to let the guy controlling the servo to be able to change the delay to have a slower or faster o to 75 motion.

Here is the program

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

Servo servoLeft; // Declare left servo

int pos = 0;

void setup() // Built-in initialization block
{

I read how to add a pot but dont get the connection between the two.

servoLeft.attach(9);

//Jig
for (int i=0; i <= 45; i++){

servoLeft.write(10);
delay(300);
servoLeft.write(45);
delay(300);
servoLeft.write(75);
delay(200);
servoLeft.write(45);
delay(200);
servoLeft.write(75);
delay(200);
servoLeft.write(45);
delay(200);
servoLeft.write(10);
delay(400);

servoLeft.detach();

}
servoLeft.detach(); // Stop sending servo signals
}

void loop() // Main loop auto-repeats
{ // Empty, nothing needs repeating
}

Now is there a way to use a pot to vary the delay? Again turn the pot ,increase to delay time. Thank you

Analogread the pot and store the value you get in a variable- global for simplicity. Use that variable in your calls to delay.

I totally dont understand that. I am not versed enough for that level. Can you write the code from what I have written and show me what you mean? Please. I do wish to understand this.

I gave you the code in reply #6.

int desiredDelay = analogRead(analogInPin); // read pot.  will reuturn 0 to 1023   
delay(desiredDelay);  // delay is variable (from 0 to 1023 milliseconds).

How to analogRead a pot.

Thank you. My plan is to attach a servo to (9) Then use AO for the pot. If I do so I will be able to control the servo speed. Is this correct?

With proper code, yes.

I dont get it. The code you provided is not the code I need to have a servo move from the o position to 90 and back to o?

Here’s your code with the analogRead suggested added in and the delays are now based off it:

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

const byte analogInPin=0;

Servo servoLeft;                             // Declare left servo

int pos = 0;

void setup()                                 // Built-in initialization block
{
  servoLeft.attach(9);
  for (int i = 0; i <= 45; i++)
  {
    int desiredDelay = analogRead(analogInPin); // read pot.  will reuturn 0 to 1023

    servoLeft.write(10);
    delay(desiredDelay*3);
    servoLeft.write(45);
    delay(desiredDelay*3);
    servoLeft.write(75);
    delay(desiredDelay*2);
    servoLeft.write(45);
    delay(desiredDelay*2);
    servoLeft.write(75);
    delay(desiredDelay*2);
    servoLeft.write(45);
    delay(desiredDelay*2);
    servoLeft.write(10);
    delay(desiredDelay*4);
  }
  servoLeft.detach();  // Stop sending servo signals
}

void loop()                                  // Main loop auto-repeats
{
  // Empty, nothing needs repeating
}

WildBill I get it!!!!!!!!! This I understand. One exception can you explain what the *3 ,*2 and *4 mean?

The asterisk is the sign for multiply (in this case).

...R

You had delays of different sizes but all in multiples of 100. The multiplication is intended to keep them in the same proportion as your original code even as the size of the delay changes. Its not clear to me whether that is actually necessary.