Basic servo example 'Sweep' misleading?

I'm an Arduino and a servo novice, certainly no expert. But it seems to me that the example sketch from the Arduino library which is frequently recommended as a learning start point is rather misleading.

Specifically, the line 25 comment:
// waits 15ms for the servo to reach the position

I interpreted this as the time for the servo arm to complete its rotation in one direction. (And the same for the subsequent reversal in line 30.)

I assume that what the author meant was:
// waits 15ms for the servo to reach each of the positions specified by the function

Or perhaps something like:
// The time for a single sweep is proportional to this setting.

As I develop my servo project I've added this expanded comment to my sketches:

This is the 'pulse' width. So the time taken for the arm to complete the specified sweep will roughly equal the product of the following three factors:

  1. The angle specified by the difference between the start and finish 'pos' values. (180 degs in the default sketch.)

  2. The pulse width (15 ms in the default sketch.)

  3. The inverse (reciprocal) of the increment value for 'pos' (1 in the default sketch.)

The SG90 hobby servo that came with my starter kit gave these approximate results, changing only the delay() time:

delay(ms)       Single sweep (secs) (approx)

15                2.5 (default)
5                 1.0
20                3.5
30                 5
50                 9
100               17

(Obviously the actual sweep is under 180 for most servos. In this case it was about 150.)

BTW, I'm also puzzled by the following suggestions by some respondents:
"make sure the delay is long enough for the servo to reach its position."

Every value from 1 ms upward that I tried resulted in a full sweep in both directions, albeit very fast at the extremely low values.

The SG90 servo will NOT be able to do a full sweep from 0 to 180 in 1ms - that is impossible.

#define SERVO_DELAY 1

void loop()
{
  servo.write(0);
  delay(SERVO_DELAY);
  servo.write(180);
  delay(SERVO_DELAY);
}

In order for this code to do full sweeps, the delay most likely will need to be at least 500ms.

Danois90:
The SG90 servo will NOT be able to do a full sweep from 0 to 180 in 1ms - that is impossible.

#define SERVO_DELAY 1

void loop()
{
  servo.write(0);
  delay(SERVO_DELAY);
  servo.write(180);
  delay(SERVO_DELAY);
}




In order for this code to do full sweeps, the delay most likely will need to be at least 500ms.

I took that as obvious. This particular servo was sweeping about 150 degs.

You also apparently didn’t read my post in full before rattling off your response. The theoretical sweep time with that absurdly low setting of 1 ms, based on my analysis, would be about 180 ms. Which was confirmed by observing it sweeping about five times per second.

The example is not misleading and you are probably not understanding it as it was intended.

I interpreted this as the time for the servo arm to complete its rotation in one direction. (And the same for the subsequent reversal in line 30.) Your interpretion is wrong - the delay is used to complete each of the 181 steps in the for loop, not the entire sweep/rotation from 0..180.

This is the 'pulse' width The delay between movements has nothing to do with pulse width, so your added comments are misleading at best. The delay is about allowing the servo to complete the movement written to it.

The "rattled response" was about eliminating the steps and allowing actual speed testing of the servo's full sweeps.

Danois90:
The example is not misleading and you are probably not understanding it as it was intended.

I interpreted this as the time for the servo arm to complete its rotation in one direction. (And the same for the subsequent reversal in line 30.) Your interpretion is wrong - the delay is used to complete each of the 181 steps in the for loop, not the entire sweep/rotation from 0..180.

This is the 'pulse' width The delay between movements has nothing to do with pulse width, so your added comments are misleading at best. The delay is about allowing the servo to complete the movement written to it.

The "rattled response" was about eliminating the steps and allowing actual speed testing of the servo's full sweeps.

Please, slow down and try reading it in FULL!

"Your interpretion is wrong - the delay is used to complete each of the 181 steps in the for loop, not the entire sweep/rotation from 0..180."

That was exactly what I went on to say that I now understand. I explained why I believe it was "... rather misleading."

waits 15ms for the servo to reach the position

Is indeed wrong, but not for the reasons given.

The program does not wait for the servo to get to the position written to it. Rather what it does is to do nothing for 15ms which probably allows the servo time to move to the position written to it. Neither the average servo, and certainly not the servo library or the code in the Sweep example has any way of signalling or knowing that the servo has, in fact, reached the commanded position.

As the delay() is in a for loop of 180 steps I fail to see how anyone could interpret the delay() as the time taken for the servo to move from 0 to 180 degrees.

As to whether the servo actually moves through 180 degrees then all bets are off, particularly if you consider the case of sail winch servos which may turn through several full (360 degree) rotations when values ranging from 0 to 180 are written to them.

It is a terrible example because it uses delay(). It is not recommended by anyone except a teacher with a class of 30 kids and only 1 hour to show them something which moves.

Don't use it. Don't invest any time in it. Move on to some real working code.

It is a terrible example because it uses delay().

It is a perfect example of where the use of delay() is perfectly acceptable. The program has one purpose and one purpose alone, ie to sweep a servo back and forth, which it does perfectly well. Why introduce complications when they are not needed ?

It can, however, provide an example of why not to use delay() by introducing other requirements. Slow down the sweep by making the delay() longer then introduce a requirement to read an input and cause the sweep direction to change and the evils of using delay() become apparent, and the use of millis() for non blocking timing can be introduced.

It's also one of only two sketches provided in the Arduino library, presumably for beginners like me to learn the basics. ('Knob' the other.) And seems by far the most frequently used for sketch development in forum posts about servos, I assume mainly because of that.

But I think it could have been more carefully commented.

Is there perhaps a 'servo examples sticky' I've not yet found, which covers the basic code more thoroughly? IOW showing code using degrees, microseconds, millis, etc, ideally in one generously commented sketch?