     {
       lastStepTime = micros();
 
-      // The NXT minimum high pulse width is 2 microseconds.
+      // Send the SPI command to change the direction, but only
+      // if it is needed.
+      if (desiredDirection != stepper.getDirection())
+      {
+        stepper.setDirection(desiredDirection);
+      }
+
       digitalWrite(amisStepPin, HIGH);
       delayMicroseconds(3);
       digitalWrite(amisStepPin, LOW);

from various errors that can occur when using the AMIS-30543,
including an interruption of the motor power supply.

Before using this example, be sure to change the parameter to
setCurrentMilliamps to an appropriate current limit for your
system.  Also, see this library's documentation for information
about how to connect the driver:

    http://pololu.github.io/amis-30543-arduino/
*/

    return;
  }

  uint16_t m = millis();

  // We will switch directions after each pause.
  bool desiredDirection = m >> 10 & 1;

  if ((m & 1023) >= 774)
  {
    // Pause for 250 ms once per second.
  }
