Motor ramping up and down

As of late, I had added some ramping to my motor driving code. I started with just ramping up but have added ramping down to stop as well now. Most hi-end motor controllers like the Sabertooth and RoboClaw do this for you, but it's nice to have on cheaper controllers to.

Ramping gives a lot of benefits to your bot. One, you save on ware-n-tare on your motors and gears. Two, it gives better traction on hard to drive on surfaces. This is more noticeable on 4 wheel or more bots, but 2wd as well. This method is easy to apply to most motor drivers. The only thing you are doing is chopping up the PWM (speed) into chunks. In my example, I just divide the speed by 4 and loop that many times adding the quotient to the speed at each interval. 

Here is a video of the code in action.

This is the example ramp function:

 void ramp_it (uint8_t pwm_pin, uint8_t speed, uint8_t stop)

{
   uint8_t ramp_val = 0, step_val = 0;

step_val = abs (speed / 4);

// If stopping, then ramp down.
if (!stop) {
step_val = -step_val;
speed = 0;
}

for (uint8_t i = 0; i < 4; i++) {
ramp_val += step_val;
analogWrite (pwm_pin, ramp_val);
delay (25);
}

   // Get to final FULL speed.
analogWrite (pwm_pin, speed);
}

https://www.youtube.com/watch?v=7Y4unGA5bR8