motor hard stop vs braking

I'm using this motor driver with 2 of these motors to control a skid steer robot. To get the robot to coast to a stop, I use this code:

  analogWrite(M1PWM, 0);
  analogWrite(M2PWM, 0);
  digitalWrite(M1INA, HIGH);
  digitalWrite(M2INA, HIGH);
  digitalWrite(M1INB, HIGH);
  digitalWrite(M2INB, HIGH);

to get the robot to hard stop, I use this code:

    analogWrite(M1PWM, 0);
    analogWrite(M2PWM, 0);
    digitalWrite(M1INA, LOW);
    digitalWrite(M2INA, LOW);
    digitalWrite(M1INB, LOW);
    digitalWrite(M2INB, LOW);

I would like for the robot to stop and/or brake with something in between so it isn't so hard on the gears. Is that possible with these small motors and this motor controller?

Ramp down the PWM in synchronous rectification mode, that gives full control. If you use slow-decay mode
acceleration is good but deceleration isn't.

In synchronous rectification mode the motor is always driven or braking, the PWM switches
between the two.

OK. Any suggestions how I do that? Is that possible with the type of motor controller that I'm using?
I have searched this term and haven't found anything meaningful or relevant to slowing down a motor by means of synchronous rectification.

An H-bridge motor driver has 4 switches. Most often one switch is held on, another is PWM'd. This
switches from current to open-circuit, and allows the free-wheel diodes to come into play and is called
slow-decay mode.

You can switch both switchs on and off in synchrony, which is fast-decay mode as the current has to
go through 2 free-wheel diodes at full supply voltage, causing much fast current decay.

But what you need to do is hold one side of the winding constant, and switch the other with PWM between 0V to +V. Thus one switch is held on, two others are alternating (with a little dead time between to avoid shoot
through).

So you need to see what the inputs to your H-bridge do to each of the switchs (on or off), and figure out
how you can arrange this mode.

Some H-bridges have only two input pins and can only do synchronous rectification mode.

The name "synchronous rectification" is not a good name, but there doesn't seem to be a better one. Perhaps
"no decay mode"...

OK. Any suggestions how I do that? Is that possible with the type of motor controller that I'm using?

I tried this code to let the motors coast for 1/4 second to slow down before a hard stop, but it just goes straight to the hard stop.

void allStop () {

  analogWrite(M1PWM, 0);  //coast for 1/4 second before fast stop
  analogWrite(M2PWM, 0);
  digitalWrite(M1INA, HIGH);
  digitalWrite(M2INA, HIGH);
  digitalWrite(M1INB, HIGH);
  digitalWrite(M2INB, HIGH);

  if ((millis() - previousCoastMillis) > 250) {
    analogWrite(M1PWM, 0);   //hard stop
    analogWrite(M2PWM, 0);
    digitalWrite(M1INA, LOW);
    digitalWrite(M2INA, LOW);
    digitalWrite(M1INB, LOW);
    digitalWrite(M2INB, LOW);
    previousCoastMillis = millis();
  }
}

can anybody make some suggestions on this?

TeslaIaint:
OK. Any suggestions how I do that? Is that possible with the type of motor controller that I'm using?

Well it looks like you can control all the inputs how you like, so yes, but you probably need
to program the Arduino timer units directly to get antiphase PWM drive.

Can you post more of your code? It's kind of hard to tell what's going on.

However, I suspect some implementation of a do...while loop might solve this problem.

This probably won't integrate directly with your sketch, but might give you an idea how to implement it.

void allStop() {
  do {
    analogWrite(M1PWM, 0);  //coast for 1/4 second before fast stop
    analogWrite(M2PWM, 0);
    digitalWrite(M1INA, HIGH);
    digitalWrite(M2INA, HIGH);
    digitalWrite(M1INB, HIGH);
    digitalWrite(M2INB, HIGH);
  }
  while ((millis() - previousCoastMillis) < 250);
  analogWrite(M1PWM, 0);   //hard stop
  analogWrite(M2PWM, 0);
  digitalWrite(M1INA, LOW);
  digitalWrite(M2INA, LOW);
  digitalWrite(M1INB, LOW);
  digitalWrite(M2INB, LOW);
  previousCoastMillis = millis();
}

In the previous code, the 250ms of previousCoastMillis always already pass before allStop is ever called so the hard stop only is engaged when the function is called. Now every time the allStop is called, the motors coast while stuck in the while loop for 150ms. That's just enough to slow down and split the difference between coasting too far and locking up the motors.

void allStop () {
previousCoastMillis = millis();
while (millis() - previousCoastMillis < 150) {
analogWrite(enA, 0);
analogWrite(enB, 0);
digitalWrite(in1, HIGH); //COAST
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, HIGH);
}

analogWrite(enA, 0); //HARD STOP
analogWrite(enB, 0);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}``