Interrupting/Pausing stepper motors during moveTo() in Multistepper


I am using MultiStepper.h to coordinate the movement of two stepper motors so they can rotate different steps and finish at the same time.

#include "AccelStepper.h"
#include "MultiStepper.h"

const int stepsPerRevolution = 400;

#define X_STEP_PIN         54
#define X_DIR_PIN          55
#define X_ENABLE_PIN       38
#define X_MIN_PIN           3
#define X_MAX_PIN           2

#define Y_STEP_PIN         60
#define Y_DIR_PIN          61
#define Y_ENABLE_PIN       56
#define Y_MIN_PIN          14
#define Y_MAX_PIN          15

AccelStepper stepperX(1 , X_STEP_PIN, X_DIR_PIN, X_MIN_PIN, X_MAX_PIN); 
AccelStepper stepperY(1 , Y_STEP_PIN, Y_DIR_PIN, Y_MIN_PIN, Y_MAX_PIN); 

MultiStepper steppers;

bool first_run = true;
long positions[2];

void setup() {
    pinMode(X_ENABLE_PIN    , OUTPUT);
    pinMode(Y_ENABLE_PIN    , OUTPUT);
    stepperX.setCurrentPosition (0); 

void loop() {
  if(first_run == true) {
    positions[0] = 830.0;
    positions[1] = 7761.0;

    first_run = false;

The above code snippet shows an example of what I'm currently doing and it is fully functional. However, I am wondering if there is a way to implement the ability to pause both steppers given an external input (i.e. user command) at any point, without reducing the number of steps to be achieved at once. For instance, is there a way to somehow pause the execution of the moveTo() function, or is it blocked in such a way that makes it impossible to interrupt?

For context, the two stepper motors are part of a X-Y movement stage where step positions of the motors correspond to linear movement distances. The reason for specific positions[0] (X) and positions[1] (Y) is that by matching the arrival time via MultiStepper, the stage connected to the steppers will perform an approximately straight, diagonal motion to a specified (X,Y) coordinate. It may be unlikely, but I was hoping there was a solution to pause the movement without reducing a single motion into looped incremental motion for ease of control, since a position # may not be nicely divisible (or is prime).


The function runSpeedToPosition() is a blocking function that continues until it completes.

If you use run() you need to call it frequently - more often than the fastest step rate - and if you stop calling it the motors will stop. That's probably the simplest way to interrupt a movement. If you resume calling run() the motors will continue their movement.