servo library "pause"

hi :slight_smile:

i'm working with the software servo library, and i cant find a way to pause the servo, or do what i want it to! lets say for example, i want the servo to just turn to 90 degrees, then go to 180 degrees, then back again, and keep looping that, but if i ever add a "delay" then my servo flips out and just tics, and seizes...

how do i create a puase between a command to make the servo turn how i tell it to?

You have to refresh the servo ALL the time; 50 times a second or so. You don't have to change the position in between refreshes. So you might have something like:

void loop()
{
  servo::refresh();  // refresh servo position, EVERY TIME.
  if (millis() >= nextmovement) {
    /*
     * it's time to move to the next position.  Update index
     * to the next desired position and time.
     */
     currentPositionIndex += 1;
     if (currentPositionIndex > MAXPOSITIONINDEX) {
        currentPositionIndex = 0;  // back to beginning of table
      }
     servo1.write(servoPositions[currentPositionIndex];  // write new position
     nextmovement = millis() + servoTimes[currentPositionIndex];  // update time for next movement
  }
  // refresh will get called the next time through loop, so we don't need to do it now.  loop()
  // needs to run at least 50 times a second or so, so don't call delay() anywhere!
  //  :
  //  Other code here.
  //  :
}

I was writing the following while westfw posted. I hope it helps

The delay needs to be between calls that determine the actual pulse width, not in the calls (to refresh for example) that send pulses to the servo.

In other words, a servo wants to get a 1 to 2 millisecond control pulse every 20 milliseconds or so. If the pulse width is increasing or decreasing over time then the servo will move. Sending pulses of constant duration will make the servo hold its current position.

i'm still confused what i have to write in the code... is the code above the exact example i talked about before?

westfw's code needs the variables declared.

You need the arrays, something like:

#define NBRELEMENTS 4 // how many servo position in your arrays
#define MAXPOSITIONINDEX (NBRELEMENTS -1)

int servoPositions[NBRELEMENTS] = { 90 ,120 ,180,0 }; // servo positions
int servoTimes[NBRELEMENTS] = {1000,2000,500,1500}; // times in ms

and there are a few other working variables that you need to declare.

btw, you can remove the define for MAXPOSITIONINDEX by changing the code as follows:
if (currentPositionIndex >= NBRELEMENTS) {