I am new to programming, and am trying to program my arduino UNO to control a servo motor. I want the servo sweeping forward and backward between [0, 45] degree. But I found my code leads to response delay. It cost 4-5 second for the motor start running, and after one return (1 forward + 1 backward) it takes another 4-5 seconds for the motor to start another loop. I have no idea what's wrong with my code. Please help me. Thanks a lot.
please check my code:
#include <ServoTimer2.h>
#define servoPin 9
ServoTimer2 myServo; // create servo object to control a servo
int pos = 0; // initial the servo position
void setup()
{
myServo.attach(servoPin); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 1500; pos += 1) //forward
{
myServo.write(pos);
delay(5);
}
for(pos = 1500; pos>=1; pos-=1) //backward
{
myServo.write(pos);
delay(5);
}
}
What kind of servo is it? If it is a standard hobby servo it will accept numbers from 0 to about 180 with 90 being center. So to move the servo to 45 degrees you servo.write(45). The reason that it takes so long is the 5 millisecond delay multiplied by 1500 times through the loop (7.5 seconds).
Your FOR loop loops 1500 times, in which you are adding a 5 millisecond delay per loop. With some simple math, 1500 * 5 milliseconds = 7500 milliseconds or 7.5 seconds.
So it takes 7.5 seconds for just that one FOR loop to end, and to make it worse, You have done the exact same thing for the second FOR loop.
Why is your range 0 - 1499, when that library already takes care of that and made it so that you only need to enter 0 - 179?
I understand now.
I use the ServoTimer2 library because I want to use it with VirtualWire library. Neither the 'Servo' library nor 'SoftwareServo' library cannot work with 'VirtualWire'.
I tried to input degree directly in myServo.write(), but failed to make the desired movement.
Is there any way to avoid using delay in servo motor control?
HazardsMind:
EDIT: I was beaten to it.
for(pos = 0; pos < 1500; pos += 1)
.
delay(5);
Your FOR loop loops 1500 times, in which you are adding a 5 millisecond delay per loop. With some simple math, 1500 * 5 milliseconds = 7500 milliseconds or 7.5 seconds.
So it takes 7.5 seconds for just that one FOR loop to end, and to make it worse, You have done the exact same thing for the second FOR loop.
Why is your range 0 - 1499, when that library already takes care of that and made it so that you only need to enter 0 - 179?
it's a normal servo I got with the Arduino Starter Kit, with "SM-S2309S" label on it.
I use the ServoTimer2 library because I want to use it with VirtualWire library. Neither the 'Servo' library nor 'SoftwareServo' library cannot work with 'VirtualWire'.
I tried to input degree directly in myServo.write(), but failed to make the desired movement.
Is there any way to avoid using delay in servo motor control?
groundfungus:
What kind of servo is it? If it is a standard hobby servo it will accept numbers from 0 to about 180 with 90 being center. So to move the servo to 45 degrees you servo.write(45). The reason that it takes so long is the 5 millisecond delay multiplied by 1500 times through the loop (7.5 seconds).
You may need to look at the particulars of the ServoTimer2.h library. if it is a version of the Servo.h library, then there may be embedded us limits. The normal servo response range is usually 500us to 2500us, but your code below seems to use 0us as an end point. The Servo.h library has a lower us limit of something like ~522us, so the ServoTimer2.h library may have the same, resulting for a period of time the servo just being sent 522us when you think a lower value is being sent. Anyway, pulse durations of less than ~500us may not be recognized by the servo.