Servo Control Problem & Help Needed

Happy 2008! Just got my hitec servos (HS-322HD) and arduino diecimila. Using windows vista and SW ver 0010.
I’ve read some documents from the playground section. Managed to install the Servo library.
But running the code below doesn’t work.

I need to move the servo 90deg clockwise then back 90deg counter-clockwise again, then repeat this process again and again.
Any kind souls able to help me out here?

Thanks!

#include <Servo.h>

Servo myservo;

void setup() {
myservo.attach(2); // attaches a servo connected to pin 2
}

void loop() {
myservo.write(90); // position the servo angle at 90 degrees
Servo::refresh();
}

Your loop just tells the servo to move to the 90 degree position.

After calling Myservo.write(90) you need to keep calling refresh() until the servo physically moves to 90 degrees. This amount of time will depend on the speed of your servos are but a second or so should be more than enough time for most servos.

You then need to move the servo back by calling Myservo.write(0) followed by similer calls to refresh while waiting for the servo to move back.

Your loop just tells the servo to move to the 90 degree position.

After calling Myservo.write(90) you need to keep calling refresh() until the servo physically moves to 90 degrees. This amount of time will depend on the speed of your servos are but a second or so should be more than enough time for most servos.

You then need to move the servo back by calling Myservo.write(0) followed by similer calls to refresh while waiting for the servo to move back.

Thanks mem!
This is what i wrote following what you suggested, but somehow the rotation is slightly OFF… as in its not exactly 90 deg, but around 93deg.

Some details of the servo:
HS-322HD
Speed - 0.19sec/60deg @ 4.8V
Torque - 3kg.cm @ 4.8V

Here’s my code:

#include <Servo.h>

Servo myservo;
int del;

void setup() {
myservo.attach(2); // attaches a servo connected to pin 2
}

void loop() {
del = 0;
myservo.write(90); // position the servo angle at 90 degrees

while(del != 10) {
myservo.refresh();
delay(90);
del++;
}

delay(1000);

myservo.write(0); // position the servo angle at 0 degrees
while(del != 0) {
myservo.refresh();
delay(90);
del–;
}
delay(1000);
}

Happy to hear you have it going :slight_smile:

Do you need the call to delay(90) while in the loop that calls refresh() ?
I would have thought the following would work:

  myservo.write(90);  // position the servo angle at 90 degrees  
  for( del = 0; del < 15 del++) {   // I expect 15 iterations to take 300ms 
    myservo.refresh();                    // this call should block for 20 ms 
  }

your servo is specified for 60 degrees in .19 secs so you need to wait closer to .3 seconds for 90 degrees.

93 degrees is pretty darn accurate. But if you want it perfect, try to eliminate the error mechanically by adjusting the linkage. You could also code a wrapper function for write that takes the desired angle and calls write with and adjusted angle that results in the desired movement.

BTW, I would expect the errors at 0 and 180 to be much greater, are they?