How to prevent stall on servo when returning to position 0

cubangt:
Since I'm kinda new to the servo controlling portion of Arduino, how would you gradually increase the PWM pulse? if the code provided above is based on position? is there a change I would need to make in order to control it more gradually?

Here's some code that I used with a MEGA2560 and a typical servo motor MG966R. I modified someone's code (that I found on the internet). It requires the arduino 'Servo' library.

While I used a MEGA2560, it should work on UNO too. If it doesn't work immediately for UNO, then might need to change the code to use a different pin number ..... such as use digital pin 6 (INSTEAD of digital pins 9 or 10).

When it says 'press a key' (in serial monitor), it usually means something like pressing the space bar ONCE, or pressing a button on the keyboard (such as pushing the 'a' key), FOLLOWED BY pushing the ENTER button.

/*
 * Simple Arduino sketch to find the full pulsewidth range of a servo
 *
 * INSTRUCTIONS
 *
 * Watch the serial port, and press a key each time the servo starts/stops moving regularly.
 * 
 * If you're using the Arduino IDE "Serial Monitor", you'll need to type something into the 
 * text field and then press "Send"
 *
 * When you're done, the sketch will output the low and high pulse widths, a sample attach() line, and then
 * start sweeping from top to bottom so you can check it works and/or measure the total angle.
 *
 * Ignore any massive jumping movements when searching, servos often seem to move to a random angle when
 * you send a really out-of-range signal to them.
 *
 *
 * This sketch is released under the GNU General Public License v2, (C) 2009 Angus Gratton 
 */

// Assuming the angle position servo motor is already powered, and digital pin 10 arduino is connected to the servo's PWM control signal INPUT line.
// Use the serial communications monitor. The software starts with pulse width modulation using short pulse-width duration, eg 100 microsecond width to start with.
// No motor response expected due to widths being outside the active range. Iteratively increase the pulse width (increments of +64 microseconds)...eg 164, 228 microseconds etc.
// Eventually the servo will move. This is when we need to press a key (or type a letter in the serial monitor, and press ENTER). This stores the minimum duration required to make the servo respond.
// The ramping continues until we get to a width level where the motor stops responding. At this point, press a key again. This stores the maximum pulse width.

// MG966R angle servos typically have pulse width 1.5 millisecond (1500 MICROsecond) for a mid-angle position 

#include <Servo.h>
#include <stdio.h>

char buffer[128]; // This wastes a bunch of RAM, but we don't need it for this sketch
#define printfLn(format, args... ) \
  snprintf(buffer, sizeof(buffer), format, ## args); \
  Serial.println(buffer);


/*
 * Set this to the pin your servo is hooked to (9 or 10.)
 */
const int servopin = 10;

Servo myservo;

void setup() 
{ 
  int del = 64;
  Serial.begin(9600);
  
  Serial.println("Searching for low pulse width");
  int low = search_low(100, del);  //starting pulse duration, incremental additive value, boolean value to trigger a print statement word
  
  Serial.println("Got low pulse width...");
  delay(2000);
  
  Serial.println("Searching for high pulse width");
  int high = search_high(low, del) - del;  //the minus is for removing the latest recorded value, as the latest one is invalid.

  Serial.println();
  Serial.println();
  printfLn("Got low pulse width of %d", low);
  printfLn("Got high pulse width of %d", high);
  Serial.println();
  printfLn("Example code: myservo.attach(%d, %d, %d);", servopin, low, high);
  
  Serial.println();
  Serial.println("Done (sweeping.)");
  
  myservo.attach(servopin, low, high);
} 

int search_low(int value, int delta)  //
{
   Serial.println("Press any key when the servo %s moving starts"); 
   delay(5000);
   value = sweep(value, delta);  // this is a looping function
   return value;
   delay(5000);  
}

int search_high(int value, int delta)  //
{
   Serial.println("Press any key when the servo %s moving stops - ie when it doesn't respond to the most recent pulse width command"); 
   delay(5000);
   value = sweep(value, delta);  // this is a looping function
   return  value;
   delay(5000);
}

int sweep(int value, int delta)
{
   int value_store = value;
   value = value - delta;
   while(Serial.available()<=0)
   {
     value = value + delta;
     myservo.attach(servopin);
     myservo.writeMicroseconds(value);
     Serial.println(value);
     delay(5000);   // put the delay here.... not after the detach line, since it is important to attach servos before they move, and to detach after they have finished moving.... ie.. not during transitions or activity...otherwise leads to operating problems.
     myservo.detach(); 


     if (value >= 3000) {  // start the pulse width ramping cycle again once we've gone past a certain level.
      value = value_store;
     }
   }
   while(Serial.available()>0)
     Serial.read();
  return value;
} 

void loop() 
{ 
   // Just loop through a few working values
   while(true)
   {
     myservo.writeMicroseconds(700);
     delay(4000);
     myservo.writeMicroseconds(1500);
     delay(4000);
     myservo.writeMicroseconds(2400);
     delay(4000);
     myservo.writeMicroseconds(1500);
     delay(4000);
   }
}