How to move multi stepper motor with constant speed to different position?

Hi, i have 4 stepper motors driving by DRV8825, and i want to make them run to a specific position and stop when they reach it but with constant speed, and not necessarily the all motors stop in the same time but they start all in one time .

exp : M1: 62 steps, M2 : 0 steps, M3: 259 steps, M4: 9 steps
I want use accelsteper library ,but I found problems with programming

aymannox:
I found problems with programming

OK. Did you want help with that? But you don't want to tell what the problems are? How do you think that is going to work out?

Delta_G:
OK. Did you want help with that? But you don’t want to tell what the problems are? How do you think that is going to work out?

I use multiple functions in the accelstepper library, sometimes the motors accelerate when moving

#include <AccelStepper.h>

AccelStepper mystepper(1, 4,3);


void setup() {
  
 mystepper.setMaxSpeed(1000);
 mystepper.move(500);
 mystepper.setSpeed(100);

}

void loop() {
  // put your main code here, to run repeatedly:
mystepper.run();

 }

and other time when i use mystepper.runSpeed() the motors run at constant speed but don’t stop when they reach the target position

#include <AccelStepper.h>

AccelStepper mystepper(1, 4,3);


void setup() {
  
 mystepper.setMaxSpeed(1000);
 mystepper.move(500);
 mystepper.setSpeed(100);

}

void loop() {
  // put your main code here, to run repeatedly:
mystepper.runSpeed();

 }

I don’t know how to code the program to solve my problem

It is too confusing when you present us with two separate programs. Choose the one that most closely meets your needs and then we can try to help fix the parts that are not exactly what you want.

Have you investigated runSpeedToPosition() ?

It would also be a big help if you describe the project you are trying to implement because there might be a simple solution if we can see the overall picture.

...R

I try runSpeedToPosition() function but the motors don't run in same tame , motor 1 turn and stop when he reach the target position the n the second motor start ..., But i want all motors start in the same time and each one stops when he reaches his final position

aymannox:
I try runSpeedToPosition() function but the motors don't run in same tame , motor 1 turn and stop when he reach the target position the n the second motor start ..., But i want all motors start in the same time and each one stops when he reaches his final position

You have not responded to my request to describe your project - without that it is too difficult to know what to suggest. For example maybe there is no need to use the AccelStepper library at all.

If you set a very high acceleration rate maybe it is the same as having no acceleration?

...R
Stepper Motor Basics
Simple Stepper Code

I will use 4 motors , Is it essential that the motors move in a co-ordinated manner, they start in the same time, as i tell you, for example, M1: 62 steps, M2 : 0 steps, M3: 259 steps, M4: 9 steps but is not necessary to Stop at the same time

The AccelStepper library has the MultiStepper class to do what you want. There is an example to show how the class is used.

aymannox:
I will use 4 motors , Is it essential that the motors move in a co-ordinated manner, they start in the same time, as i tell you, for example, M1: 62 steps, M2 : 0 steps, M3: 259 steps, M4: 9 steps but is not necessary to Stop at the same time

If you don't need acceleration then there is no need to use the AccelStepper library to achieve that. And the MultiStepper variant of the AccelStepper library does not use acceleration anyway.

If you create a repeating interval using millis() then you can move each motor one step after each interval and each motor can be stopped when it has moved the required number of steps. Have a look at the second example in my Simple Stepper Code.

And it is not a great deal more difficult to make the step timing vary so that all the motors finish their moves at the same time.

...R

groundFungus:
The AccelStepper library has the MultiStepper class to do what you want. There is an example to show how the class is used.

MultiStepper class is good if you want all motors to go to the same position so is not working for me .

Robin2:
If you don't need acceleration then there is no need to use the AccelStepper library to achieve that. And the MultiStepper variant of the AccelStepper library does not use acceleration anyway.

If you create a repeating interval using millis() then you can move each motor one step after each interval and each motor can be stopped when it has moved the required number of steps. Have a look at the second example in my Simple Stepper Code.

And it is not a great deal more difficult to make the step timing vary so that all the motors finish their moves at the same time.

...R

AccelStepper library is easy to handle and work fine with multiple steppers motors

aymannox:
AccelStepper library is easy to handle and work fine with multiple steppers motors

That sounds like you have solved your problem.

...R

Robin2:
That sounds like you have solved your problem.

...R

No,i want all stepper motors start in the same time and When each one reaches its desired position, it stops And others complete the way to their target position .
I haven't found the right code to solve this problem

aymannox:
No,i want all stepper motors start in the same time and When each one reaches its desired position, it stops And others complete the way to their target position .
I haven't found the right code to solve this problem

The regular AccelStepper will do that.

When it is time to start a move set the move() or moveTo() values for each motor. And as the last thing in loop() call run() for each motor. Make sure that loop() repeats much more often than the fastest step rate.

...R

aymannox:
No,i want all stepper motors start in the same time and When each one reaches its desired position, it stops And others complete the way to their target position .
I haven’t found the right code to solve this problem

That’s exactly what AccelStepper library is good at doing.

Don’t use runToPosition or runSpeedToPosition, just use the basic standard method:

Call run() for every motor in loop(). Don’t call delay(), don’t use blocking functions.
Call move() or moveTo() when you want to start a motor moving to a destination.

That’s it, the rest is automatic…

Problem solved :slight_smile:

#include <AccelStepper.h>
AccelStepper stepper1(1,11,10);
AccelStepper stepper2(1,8,7);

boolean one,two;
void setup() {
 
  // put your setup code here, to run once:
   stepper1.setMaxSpeed(1000);

   stepper2.setMaxSpeed(1000);
  
 
}

void Turn(int MoveOne ,int MoveTwo ){

    stepper1.move(MoveOne);
    stepper1.setSpeed(600); 
   
    stepper2.move(MoveTwo);
    stepper2.setSpeed(600);
     one = 0 ;
     two = 0 ;
   while( one == 0 || two == 0){
   if(stepper1.distanceToGo()!= 0){
   stepper1.runSpeed();
   }
   else{
    one=1;
   }
   if(stepper2.distanceToGo()!= 0){
  stepper2.runSpeed();
   }
   else{
    two=1;
   }
  
   }
}


void loop() {
  Turn(1000,300);
 delay(3000);
}