[HELP] Servo motor delayed by unknown programming problems

Hi there,

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)  
  {                                  
    myServo.write(pos);              
    delay(5);                       
  } 

  for(pos = 1500; pos>=1; pos-=1)     
  { 
    myServo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5);                      
  } 
}

Doing exactly what it’s supposed to do…

http://arduino.cc/en/Reference/ServoWrite

http://arduino.cc/en/Reference/ServoWriteMicroseconds

My guess for the mystery delay is … that you start out with a pulsewidth (PW) = 0 usecs. Most servos only work with PWs from 1000 to 2000 usec (corresponding to 0 and 180 degrees, ideally). Some servos will take a wider range to either get better resolution or a wider range of motion. So the servo is going to be non-responsive until pos increments into the working range. If the loop runs every 5ms, it’s going to take 1000 iterations, or 5 secs, before pos equals 1000.

Your second for() looks rather odd to me. I’m surprised it works.

for(pos = 1500; pos>=1; pos-=1)

Might not both for() statements been better as (keeping your general idea and library);

#include <ServoTimer2.h>

const int ServoTimer2Pin = 9;
const int PWmin = 1000; //minimum PW
const int PWmax = 1250; //maximum PW
const int PWincr = 1;   //amount to increment pos by each loop

ServoTimer2 myServoTimer2;    // create ServoTimer2 object to control a ServoTimer2   
int pos = PWmin;         //initial the ServoTimer2 position
boolean slope = true;    //set to positive slope

void setup()
{
  myServoTimer2.attach(ServoTimer2Pin);  // attaches ServoTimer2, pin 9 to ServoTimer2 object
}

void loop()
{
  if(slope){              //test to see if position should increase
    if(pos < PWmax){      //should increase, test to see if max position
      pos += PWincr;      //increment position by set amount
    }
    else{                 //max PW commanded, now reverse direction
      slope = false;
      pos-= PWincr;       //decrement position command
    }
  }
  else{                   //slope is negative, position should decrease
    if(pos > PWmin){      //test to see if min PW has been commanded
      pos-= PWincr;       //nope, still decrease position
    }
    else{                 //PW is at minimum, reverse direction.
      slope = true;
      pos+= PWincr;       //increment position by set amount
    }
  }
  myServoTimer2.write(pos);   //write out position command
  delay(5);                   //delay some msecs
}

Moreover the typical servo signal is a variable PW pulse happening every 20 msec. Changing the value every 5 msec would screwup the normal servo library function (I think). Perhaps your library “works” but the actual command is not changing every 5 msec. If you want to go from position X to Y in some specific amount of time, try using the VarSpeedServo library.

https://github.com/netlabtoolkit/VarSpe … /README.md

http://forum.arduino.cc/index.php?topic=61586.0

https://github.com/netlabtoolkit/VarSpeedServo

(Well, my point was that he was using the method which talks in degrees vs microseconds, therefore, 1500 degrees, while not between 0-180, does boil down to 1500 * 1500, which is about 2 1/4 seconds, which when swept back and forth comes out to about 5 seconds, and therefore, using the wrong command)

That ^^ could be true. I ASSumed the write command argument accepted usecs as it’s argument. He’s using an outdated library from what I could tell, so I don’t know what it accepts. The VarSpeedServo library is “smart”, an argument btw 0 and 180 is treated as degrees. A number btw 500 and 2500 is treated as usecs, otherwise it’s out of bounds.