AccelStepper how to finish a motor.run()

Looking at this code, I want motor1 and motor2 to switch directions at the same time (without worrying for their position), but it doesn't work since when I change the positions of motor1 and motor2, motor2 is still running and it will finish its movement before starting the new one.

So how can I finish that movement? I guess I could add a delay, but is there a better way to force a motor.run() to stop for good?

*At first I was under the impression that the rotors didn't work in parallel, this was not the case and I have reedited the question entirely

#include <AccelStepper.h>
// #include <MultiStepper.h>

// 3rd gear
#define IN31 2
#define IN32 3
#define IN33 4
#define IN34 5

// 2nd gear
#define IN21 6
#define IN22 7
#define IN23 8
#define IN24 9

// 1st gear
#define IN11 A3
#define IN12 A2
#define IN13 A1
#define IN14 A0

// steps
int steps=2038; // byj28
#define FULLSTEP 4
#define HALFSTEP 8

int motor1_pos;
int motor2_pos;
int motor3_pos;

AccelStepper motor1 (HALFSTEP, IN11, IN13, IN12, IN14);
AccelStepper motor2 (HALFSTEP, IN21, IN23, IN22, IN24);
AccelStepper motor3 (HALFSTEP, IN31, IN33, IN32, IN34);

bool running = true;
bool first_time = true;

void setup()
{
  Serial.begin(9600);
  Serial.println("GO");
  
  pinMode(IN11, OUTPUT);
  pinMode(IN12, OUTPUT);
  pinMode(IN13, OUTPUT);
  pinMode(IN14, OUTPUT);
  pinMode(IN21, OUTPUT);
  pinMode(IN22, OUTPUT);
  pinMode(IN23, OUTPUT);
  pinMode(IN24, OUTPUT);
  pinMode(IN31, OUTPUT);
  pinMode(IN32, OUTPUT);
  pinMode(IN33, OUTPUT);
  pinMode(IN34, OUTPUT);

  // slow movement
  motor3_pos = -100;
  motor3.setMaxSpeed(1000);
  motor3.setAcceleration(200);
//  motor3.setSpeed(25);
  motor3.moveTo(motor3_pos);

  // coordinated and fast movement
  motor1_pos = 200;
  motor1.setMaxSpeed(10000);
  motor1.setAcceleration(1000);
//  motor1.setSpeed(1000);
  motor1.moveTo(motor1_pos);

  motor2_pos = -200;
  motor2.setMaxSpeed(10000);
  motor2.setAcceleration(10000);
//  motor2.setSpeed(1000);
  motor2.moveTo(motor2_pos);

  rotate();
}

int rotations = 0;
void rotate()
{
  motor3.run();

  if (rotations == 20)
  {
    Serial.println("rotations is 20");
    running = false;
  }

  // change of direction
  if (motor3.distanceToGo() == 0) {
    Serial.println("change of direction");
    if (motor3.currentPosition() != 0)
      motor3.moveTo(0);
    else
      motor3.moveTo(motor3_pos);
    rotations++;
    first_time = false;

    // if a cycle has completed, call pullout()
    if (!first_time && rotations % 2 == 0) {
      Serial.println("cycle completed");
       pullout(); // rotate() will be called by pullout() once it finishes
    } else {
      if (running)
        rotate();
    }
  } else {
      if (running)
        rotate();
  }
}

int pullouts = 0;
void pullout()
{
  motor1.run();
  motor2.run();
  
  if (motor1.distanceToGo() == 0) {
    pullouts++;
    if (motor1.currentPosition() != 0) {
      motor1.moveTo(0);
      motor2.moveTo(0);
    } else {
      motor1.moveTo(motor1_pos);
      motor2.moveTo(motor2_pos);
    }

    if (pullouts == 4) {
      pullouts = 0;
      rotate();
    } else 
      pullout();
  } else
    pullout();
}

void loop()
{
//  delay(2000);
}

motor.stop() doesn't work, but just checking for both motors to have reached the point does work

  if (motor1.distanceToGo() == 0 && motor2.distanceToGo() == 0) {

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.