Activity 4: Ramping Maneuvers

Ramping is a way to gradually increase or decrease the speed of the servos instead of abruptly starting or stopping.  This technique can increase the life expectancy of both your BOE Shield-Bot’s batteries and your servos.

Programming for Ramping

The diagram below shows an example of how to ramp up to full speed.  The for loop declares an int variable named speed, and uses it to repeat the loop 100 times.  With each repetition of the loop, the value of speed increases by 2 because of the speed+=2 expression in the for loop’s increment parameter.  Since the speed variable is in each writeMicroseconds call’s us parameter, it affects the value each time the for loop repeats.  With the 20 ms delay between each repetition, the loop repeats at about 50 times per second.  That means it takes speed 1 second to get to 100 in steps of 2, and at that point, both servos will be going about full speed.

A for loop that increments the speed variable and increments in steps of two, with diagrams showing how speed is used in servo function calls within the for loops code block

Let’s take a closer look at the trips through the for loop from this diagram:

  • First trip: speed is 0, so both writeMicroseconds calls end up with us parameters of 1500. 
  • Second trip: speed is 2, so we have servoLeft.writeMicroseconds(1502) and servoRight.writeMicroseconds(1498).
  • Third trip: speed is 4, so we have servoLeft.writeMicroseconds(1504) and servoRight.writeMicroseconds(1496).
  • Keep on in this manner until the…   
  • 50th trip: speed is 100, with servoLeft.writeMicroseconds(1600) and servoRight.writeMicroseconds(1400).  Remember, that’s pretty close to full speed, 1700 and 1300 are overkill.

Example Sketch: StartAndStopWithRamping

  • Enter, save, and run StartAndStopWithRamping.
  • Verify that the BOE Shield-Bot gradually accelerates to full speed, maintains full speed for a while, and then gradually decelerates to a full stop.
// Robotics with the BOE Shield - StartAndStopWithRamping
// Ramp up, go forward, ramp down.

#include <Servo.h>                           // Include servo library
Servo servoLeft;                             // Declare left and right servos
Servo servoRight;
void setup()                                 // Built-in initialization block
  tone(4, 3000, 1000);                       // Play tone for 1 second
  delay(1000);                               // Delay to finish tone

  servoLeft.attach(13);                      // Attach left signal to pin 13
  servoRight.attach(12);                     // Attach right signal to pin 12
  for(int speed = 0; speed <= 100; speed += 2) // Ramp up to full speed.
    servoLeft.writeMicroseconds(1500+speed);   // us = 1500,1502,...1598,1600
    servoRight.writeMicroseconds(1500-speed);  // us = 1500,1498,...1402,1400
    delay(20);                                 // 20 ms at each speed
  delay(1500);                                 // Full speed for 1.5 seconds

  for(int speed = 100; speed >= 0; speed -= 2) // Ramp from full speed to stop
    servoLeft.writeMicroseconds(1500+speed);   // us = 1600,1598,...1502,1500
    servoRight.writeMicroseconds(1500-speed);  // us = 1400,1402,...1498,1500
    delay(20);                                 // 20 ms at each speed
  servoLeft.detach();                          // Stop sending servo signals

void loop()                                    // Main loop auto-repeats
{                                              // Empty, nothing to repeat

Your Turn – Add Ramping to Other Maneuvers

You can also create routines to combine ramping with other maneuvers.  Here’s an example of how to ramp up to full speed going backward instead of forward.  The only difference between this routine and the forward ramping routine is that the value of speed starts at zero and counts to –100.

  for(int speed = 0; speed >= -100; speed -= 2)// Ramp stop to full reverse
    servoLeft.writeMicroseconds(1500+speed);   // us = 1500,1498, 1496...1400
    servoRight.writeMicroseconds(1500-speed);  // us = 1500,1502, 1508...1600
    delay(20);                                 // 20 ms at each speed

You can also make a routine for ramping into and out of a turn.  Here is a right-turn ramping example.  Notice that instead of 1500+speed for one wheel and 1500–speed for the other, now they are both 1500+speed.  For left-turn ramping, they would both be 1500–speed.

  for(int speed = 0; speed <= 100; speed += 2) // Ramp stop to right turn
    servoLeft.writeMicroseconds(1500+speed);   // us = 1500,1502, 1508...1600
    servoRight.writeMicroseconds(1500+speed);  // us = 1500,1502, 1508...1600
    delay(20);                                 // 20 ms at each speed

  for(int speed = 100; speed >= 0; speed -= 2)// right turn to stop
    servoLeft.writeMicroseconds(1500+speed);   // us = 1600,1598, 1597...1500
    servoRight.writeMicroseconds(1500+speed);  // us = 1600,1598, 1597...1500
    delay(20);                                 // 20 ms at each speed
  • Open the sketch ForwardLeftRightBackward and save it as ForwardLeftRightBackwardRamping.
  • Modify the new sketch so your BOE Shield-Bot will ramp into and out of each maneuver. Hint: you might use the code snippets above, and similar snippets from StartAndStopWithRamping.