FastAccelstepper library for Multiple steppermotors

I'm trying to control two Nema 23 stepper motors and DM542 drivers using FastAccelstepper library by the Mega2560. The problem is that only one of the motors runs and the other one doesn't move at all.
Any help or suggestion is appreciated.

#include "FastAccelStepper.h"
#define dirPinStepper1 11
#define stepPinStepper1 6

#define dirPinStepper2 12
#define stepPinStepper2 7

FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepper1 = NULL;
FastAccelStepper *stepper2 = NULL;

void setup() {
  engine.init();
  stepper1 = engine.stepperConnectToPin(stepPinStepper1);
  if (stepper1) {
    stepper1->setDirectionPin(dirPinStepper1);
    stepper1->setAutoEnable(true);
    stepper1->setSpeedInUs(1000);  
    stepper1->setAcceleration(100);
    stepper1->move(1000);
  }
  stepper2 = engine.stepperConnectToPin(stepPinStepper2);
  if (stepper2) {
    stepper2->setDirectionPin(dirPinStepper2);
    stepper2->setAutoEnable(true);
    stepper2->setSpeedInUs(1000);  
    stepper2->setAcceleration(100);
    stepper2->move(1000);
  }
}

void loop() {}

I tried the code; it runs as expected on my Mega.

Fault is in hardware or external components. Usual debugging technique applies, swap known good components with suspected faulty ones to isolate source.

3 Likes

Thanks for your response. I've checked the hardware. Everything is ok, i.e. when I change the order of motors, now stepper2 rotates but stepper1 doesn't move.

So one of your steppers or drivers is faulty or misconfigured.

As I said both motors and drivers are fine. With other libraries such as accelstepper both work fine. But in Fastaccelstepper it depends on the order of motors in the code.
with the previous code, stepper1 rotates and stepper2 doesn't move.
but with the following code, stepper2 rotates and stepper1 doesn't move.
It seems there is a programming issue with the Fastaccelstepper library.

#include "FastAccelStepper.h"

#define dirPinStepper1 11
#define stepPinStepper1 6

#define dirPinStepper2 12
#define stepPinStepper2 7

FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepper1 = NULL;
FastAccelStepper *stepper2 = NULL;

void setup() {
  engine.init();
  stepper2 = engine.stepperConnectToPin(stepPinStepper2);
  if (stepper2) {
    stepper2->setDirectionPin(dirPinStepper2);
    stepper2->setAutoEnable(true);
    stepper2->setSpeedInUs(1000);  
    stepper2->setAcceleration(100);
    stepper2->move(1000);
  }
  stepper1 = engine.stepperConnectToPin(stepPinStepper1);
  if (stepper1) {
    stepper1->setDirectionPin(dirPinStepper1);
    stepper1->setAutoEnable(true);
    stepper1->setSpeedInUs(1000);  
    stepper1->setAcceleration(100);
    stepper1->move(1000);
  }
}

void loop() {}

Oh sorry I misunderstood, my bad.

The code appeared to work correctly when I tried it, but I only looked at the output with a scope, I didn't try actual motors. If I have a moment I can try with some real motors.

Just tried with two A4988 drivers, Nema17 motors, same code. Both motors turn as expected.

1 Like

Same here:

It seems to work fine with either code.

I did notice there is a silent failure mode. I would put a print statement after engine.stepperConnectToPin() to check that a step pin was claimed.

From the symptoms described, it could be that whichever stepper is initialised first is able to claim a step pin, but the second one fails.

FastAccelStepper is quite a complex beast.

1 Like

Thank you guys for your helping me. Actually, there was a thoughtless mistake in the main code of my project since I was trying to turn the motors with the max speed and acceleration. In this way, the Mega controller is not able to support both motors at the same time. As mentioned in the library documentation, it allows up to 50000 generated steps per second for single stepper operation, 37000 for dual stepper, and 20000 for three steppers.

#include "FastAccelStepper.h"

#define dirPinStepper1 11
#define stepPinStepper1 6

#define dirPinStepper2 12
#define stepPinStepper2 7

FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepper1 = NULL;
FastAccelStepper *stepper2 = NULL;

void setup() {
  engine.init();
  stepper2 = engine.stepperConnectToPin(stepPinStepper2);
  if (stepper2) {
    stepper2->setDirectionPin(dirPinStepper2);
    stepper2->setAutoEnable(true);
    stepper2->setSpeedInUs(50000);  
    stepper2->setAcceleration(10000);
    stepper2->move(1000);
  }
  stepper1 = engine.stepperConnectToPin(stepPinStepper1);
  if (stepper1) {
    stepper1->setDirectionPin(dirPinStepper1);
    stepper1->setAutoEnable(true);
    stepper1->setSpeedInUs(50000);  
    stepper1->setAcceleration(10000);
    stepper1->move(1000);
  }
}

void loop() {}

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