Hi,
I'm experiencing a problem keeping a servo steady. I'm working with an arduino pro mini and a home made shield with a servo on it. With a very, very basic sketch that just generates servos pulses at the correct interval the servo remains in position and is absolutely rock-steady - it doesn't move a jot.
As soon as I write to the serial port (just a single line writing the current millis()) then the servo starts to judder, not by much, maybe half a degree, and not very often, on average one judder every 3 seconds. Although it's not enough to cause a problem, it is enough to be annoying!
unsigned long period = 22 ; // period (in milliseconds) between pulses
unsigned long prevPulse = 0 ; // moment last pulse was generated.
unsigned long halfPeriod = period / 2; // half period.
bool elevatorPulse = false;
Servo _elevator;
Servo _rudder;
void setup()
{
Serial.begin(115200); // start serial communication at 115200
Serial.println("#################### START ###############################");
_elevator.attach(6);
_rudder.attach(5);
}
void loop()
{
// NOTE - to try to 'smooth out' any battery drain, this code deliberately attempts to seperate
// the pulses for elevator and rudder by a time equal to half the period - ie about 11 milliseconds
// Decide if it's time for a servo pulse, EITHER for the elevator, OR for the rudder
if ( millis() > (prevPulse + halfPeriod) )
{
if (elevatorPulse == false)
{
// RUDDER
// Generate a PWM signal of the appropriate duration to set the servos in the required position.
_rudder.writeMicroseconds(1490);
elevatorPulse = true;
Serial.print("time=<");Serial.print(millis());Serial.println(">"); // The addition of this line causes the judder.
}
else
{
// ELEVATOR
_elevator.writeMicroseconds(1490);
elevatorPulse = false;
}
// Record the moment this pulse has been applied
prevPulse = millis();
}
}
The above sketch is written just to illustrate the problem, the fact that it works without the print() statement suggests there's nothing wrong with the servo itself, or the power supply to it. I get similar results with different servo positions, and different servos. As far as I can make out somehow the width of the pulse being generated by writeMicroseconds() is being effected by the presence of the print() statement.
My servo libraries are more complex than that shown above, but the still suffer from the same problem - the addition of one of more print() statements causes servo judder. The problem for me is that I need to print() statements because I want to transmit the servo position to a 2nd arduino down the serial port.
Any ideas?
thanks