Go Down

Topic: Controlling 3 stepper motors simultaneously (Read 9939 times) previous topic - next topic

the_plds

I have three stepper motors connected to the drv8825 drivers to an arduino uno.

I am using the example code below (from here), which works fine for moving multiple stepper motors at the same time BUT they all move for the same number of steps. I would like to tweak the code so that all of them move at the same time but each one for a different number of steps and in different directions. The motor with the lieast steps would probably have to wait for the motor with the most steps to finish its movement before going again.

int dirPin = 8;
int stepperPin = 7;
int bdirPin = 6;
int bstepperPin = 5;

void setup() {
 pinMode(dirPin, OUTPUT);
 pinMode(stepperPin, OUTPUT);
 pinMode(bdirPin, OUTPUT);
 pinMode(bstepperPin, OUTPUT);
}
 void step(boolean dir,int steps){
 digitalWrite(dirPin,dir);
 digitalWrite(bdirPin,dir);
 delay(50);
 for(int i=0;i<steps;i++){
   digitalWrite(stepperPin, HIGH);
   digitalWrite(bstepperPin, HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin, LOW);
   digitalWrite(bstepperPin, LOW);
   delayMicroseconds(800);
 }
}
void loop(){
 step(true,1600);
 delay(500);
 step(false,1600*5);
 delay(500);
}

Thank you in advance for your help!

Robin2

Have a look at the code in the second example in this Simple Stepper Code. If you want the motors to move as you say you need to use millis() or micros() to control the timing without blocking. The delay() function blocks the Arduino until it finishes.

It should not be difficult to extend the example to deal with 3 motors each with its own separate timing values. Have a go at that and if it does not work post your code. And please use the code button </>
Code: [Select]
so your code looks like this and is easy to copy to a text editor.

I am doing something similar with 3 motors on a small lathe.

...R
Stepper Motor Basics

Two or three hours spent thinking and reading documentation solves most programming problems.

the_plds

Thank you for your help. I tried playing with the code and only one stepper and I have come up with this (changed the millis to micros as well since the movement of the motor seems to be smoother controlled with micros):

Code: [Select]
unsigned long curMicros;
unsigned long prevStepMicros = 0;
unsigned long microsBetweenSteps = 2000;
int currentStep = 0;

  void singleStep() {
  if (curMicros - prevStepMicros >= microsBetweenSteps) {
    prevStepMicros += microsBetweenSteps;
    digitalWrite(stepperPin, HIGH);
    digitalWrite(stepperPin, LOW);
  }
}
void multipleSteps (){
if (currentStep<totalSteps) {
  singleStep();
} else {
digitalWrite(stepperEnablePin, HIGH);
}
}



So when calling the multipleSteps function it should repeat the singleStep function as long as the currentStep is less than the totalSteps and after currentStep is equal to the totalSteps it should deenergize the motor. But it doesn't it just keeps spinning on and on.

Robin2

#3
Feb 06, 2016, 09:25 pm Last Edit: Feb 06, 2016, 09:26 pm by Robin2
But it doesn't it just keeps spinning on and on.
You have not posted the complete program so I can't say what the problem is.

If you want the motors to hold their relative positions do not de-energize them.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

AndreasPercher

Hello the_plds,
I yust postet my version of a stepper driver here in the forum, it may be helpfull.

Robin2

I yust postet my version of a stepper driver
Your code is for the 28BYJ-48 unipolar steppers (and is no doubt useful for people with those motors) but the OP in this Thread is using bipolar motors and DRV8825 drivers.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up