Multiple Stepper Motors - how to make different amount of steps in the same time

Hey there.

My project contains 2 stepper motors. Those 2 need to be able different amount of steps.
I know there are libraries like AccelStepper but they dont work that well for me.
Im using 2 Nema 17 motors with a4988 driver (one pin to make the direction, other pin to make one step).

So right now my code (below) makes the stepper do their steps, but as you can see. when the first one is done with his steps, the other one needs to do a few more.
i want those to take the same time to make different amoounts of steps. I guess i have to do something with the delay adjustement, that the one with lower stepscount has more waitung time between each step.

you have any improvements?
i know i shouldnt use delay, but because while the mototrs are moving, nothing else is happening, thats fine for me.

void setup() {

  pinMode(2, OUTPUT); //direction stepper1
  pinMode(3, OUTPUT); //step Stepper1
  pinMode(4, OUTPUT); //Dir Stepper2
  pinMode(5, OUTPUT); //Step stepper2

  digitalWrite(2, HIGH);
  digitalWrite(4, HIGH);
  int count1 = 0;
  int count2 = 0;
  int stepstogo1 = 20;
  int stepstogo2 = 50;
}

void loop() {
  while ( count1 != stepstogo1 || count2 != stepstogo2)
  {
    if count1 < stepstogo1)
  {
    digitalWrite(3, HIGH);
      delay(2);
      digitalWrite(3, LOW);
      delay(2);
      count1 = count1 + 1;
    }

    if ( count2 < stepstogo2)
  {
    digitalWrite(5, HIGH);
      delay(2);
      digitalWrite(5, LOW);
      delay(2);
      count2 = count2 + 1;
    }
  }


}




}

The MultiStepper class of AccelStepper library does exactly what you want (without blocking).

In what way does AccelStepper not work for you?

The AccelStepper library is not designed for the coordinated movement of two (or more) stepper motors. By "coordinated" I mean that the motors should start and stop at the same time but do different numbers of steps - as on a 3D printer.

The MultiStepper variation of the AccelStepper does do coordinated movement but it cannot do acceleration.

If you want to write your own code to do coordinated movements then you need to figure out the intervals between steps for the two motors and make each motor move one step at the appropriate time. A trivial example is one motor moving 120 steps while the other moves 40 steps. If the faster motor moves one step very 5 millisecs then the slower motor would move one step every 15 millisecs.

If you need to use acceleration it is not actually difficult to implement yourself.

...R
Stepper Motor Basics
Simple Stepper Code
simple acceleration code

Here’s a crude way to do it:

int count1 = 0;
int count2 = 0;
int stepstogo1 = 20;
int stepstogo2 = 50;

void setup()
{
  Serial.begin(115200);
  pinMode(2, OUTPUT); //direction stepper1
  pinMode(3, OUTPUT); //step Stepper1
  pinMode(4, OUTPUT); //Dir Stepper2
  pinMode(5, OUTPUT); //Step stepper2
  digitalWrite(2, HIGH);
  digitalWrite(4, HIGH);
}

void loop()
{
  if (count1 < stepstogo1 && ((float)count1/stepstogo1 < ((float)count2/stepstogo2)))
  {
    Serial.println("Step 1");
    digitalWrite(3, HIGH);
    delay(2);
    digitalWrite(3, LOW);
    delay(2);
    count1 = count1 + 1;
  }
  if ( count2 < stepstogo2)
  {
    Serial.println("Step 2");
    digitalWrite(5, HIGH);
    delay(2);
    digitalWrite(5, LOW);
    delay(2);
    count2 = count2 + 1;
  }
}

wildbill:
Here's a crude way to do it:

I don't see how that can work as the step interval for the two motors seems to be the same.

...R

“i want those to take the same time to make different amoounts of steps.”

So your loop could be time based,
Write to pins based on elapsed millis and state variables, this will also allow you to stop using delay(2)s

Remember the delay is for each stepper motor to start to move.
So you can set pin 3 and pin 5 back to back then loop until 2 mills passed and then write to both again.

Robin2:
I don't see how that can work as the step interval for the two motors seems to be the same.

...R

Try it - it just ensures that the slower motor gets stepped less often. I confess that I didn't bother to hook up any steppers - just relied on the serial prints to show the sequence.