 // Speed in  full steps per second. For 200 steps/rot and 8mm/rot this is 10 mm/sec
 #define STEP_ACCEL 1000
 // Accel in full steps per second per second
+#define TIMEOUT_MILLIS 60000
+// Timeout (in milliseconds) for the stepper drive loop
 
 #define DIRECTION_OPEN +1
 #define DIRECTION_CLOSE -1

int move_to_limit(int dir, float* elapsed_sec) {
  int continu = 1;
  int retcode, dpin_limit;
  unsigned long startmillis;

  startmillis = millis();
  // Setup digital pin for sought limit switch

  stepper.enableOutputs();
  // start motion towards sought limit
  stepper.move(dir * STEPS_FULLMOVE * USTEP_DIVISOR);
  // set timeout TIMEOUT_MILLIS milliseconds after current time
  timeout = startmillis + TIMEOUT_MILLIS;
  // run the motor
  retcode = FAULT_MOTOR_SHORT;  // default return code is that the motor didn't achieve limit
  while ( (stepper.distanceToGo() != 0) && (millis() < timeout) ) {
    if (digitalRead(dpin_limit) == LOW) {
      retcode = FAULT_NONE;
      stepper.stop();  // tell the stepper to stop via decelleration ramp
    }
    stepper.run();  // keep running the motor
  }
  // Disable the driver (turn current off)
  stepper.disableOutputs();
