I am using ServoTimer2 library since the other libraries in my code were using same interrupt as Servo library. However, I observe that when I use the code below, the servo motor rotates smoothly
servo1.write(750); //min pulse width for 0 degree
delay(1000);
servo1.write(1500); //pulse width for 90 degree
delay(1000);
servo1.write(2250); //max pulse width for around 180 degree
delay(1000);
but when I try to do it as shown below, it works really slow although the delay is small
for (int i=750; i<1500; i++){
servo1.write(i);
delay(5);
}
for (int i=1500; i>750; i--){
servo1.write(i);
delay(5);
}
Any idea why is it so?
You say that the delay is small but 750 * 5 milliseconds is 3.75 seconds. Does that correspond to how long the servo takes to move or is it longer ?
I reduced the delay to 1 sec. It works fine but i don't know why it is not going smoothly.
You should probably use the writeMicroseconds() function to write those type of values, not write. It makes it more clear what you are doing.
Not with ServoTimer2.
Will your servos actually go to the position referenced by 750us? If not you could be stalling the servo against its stops and that can damage the servo. That applies to the high end, too. Not all servos will actually go to 0 or 180 without stalling.
Thanks for the suggestion!
Yes, the servo does not go to the position referenced.
When I use the writeMicroseconds function, I get the following error:
backservo:42:14: error: 'class ServoTimer2' has no member named 'writeMicroseconds'
servo1.writeMicroseconds(i);
My apologies. Somewhere between my eyes and brain the reference to ServoTimer2 got lost. I will wait for the coffee to be done before I post any more.
Do you mean that you reduced the delay to1 millisecond ?
The motor gives sudden jerks while rotating which is not the case when the first sketch of code is used
Your first sketch moves the servo directly to its target position and it moves as fast as possible, hence no jerks
Your second sketch moves the servo in steps hence I would expect a jerkier movement as it moves, stops, moves, stops and so on
How bad is the jerkiness ?
There are around 3 jerks from 0 to 180 degrees.
But this was not the case when I used Servo library...
It sounds like there is something going on in the background that is causing the jerkiness. Do you actually need the servos to work in steps or could you just move them to their target positions ?
Is that because you need the servo to move slowly ?
When you are testing ServoTimer2 have you got any other code in the sketch or just the 2 for loops, if so, what ?
Hmm.. preferably fast
I am actually using servo motor with lidar to detect obstacles. Hence, I also call the lidar function within the for loop.
Try the slow for loops with no other code
It sounds like you have swapped one problem for another. My guess is that the lidar code is interfering with the servo code
Hmm there are still many jerks 
When I am trying the same thing with Servo library it works fine..
Which Arduino are you using and how is the servo powered ?
I am using Arduino UNO. The servo motor is powered using power supply module connected to breadboard
I have just tried this sketch using a Nano and the servo moves smoothly. Please try it
#include <ServoTimer2.h>
ServoTimer2 aServo;
void setup()
{
aServo.attach(2);
}
void loop()
{
for (int i = 750; i < 1500; i++)
{
aServo.write(i);
delay(1);
}
for (int i = 1500; i > 750; i--)
{
aServo.write(i);
delay(1);
}
}
I presume that your servo and the Arduino have a common GND connection