How to run 2 stepper motors at the same time

Hello. I am working on my project and i ran on the problem. I want to spin 2 nema 17 motors at the same time in the same direction, but what now happening in my code is that the 1 motor spis for 200 steps and after 200 steps the motor number 2 spins for 200 steps. (they are not spining at the same time). Now i don’t know what I need to write in the code, can you help me?
I am using arduino UNO, arudino cnc shield and DRV8825 drivers.

#define EN        8  


#define X_DIR     5 
#define Y_DIR     6


#define X_STP     2
#define Y_STP     3


//DRV8825 - driver
int delayTime=500; //speed
int steps=1200;// 200 steps per revolution

void step(boolean dir, byte dirPin, byte stepperPin, int steps)

{

  digitalWrite(dirPin, dir);
 
  for (int i = 0; i < steps; i++) {

    digitalWrite(stepperPin, HIGH);
    
    delayMicroseconds(delayTime);

    digitalWrite(stepperPin, LOW);
    
    delayMicroseconds(delayTime);
  }

}

void setup(){

  pinMode(X_DIR, OUTPUT);
  
  pinMode(X_STP, OUTPUT);

  pinMode(Y_DIR, OUTPUT);

  pinMode(Y_STP, OUTPUT);

  pinMode(EN, OUTPUT);
  
  digitalWrite(EN, LOW);


}



void loop(){

step(false, X_DIR, X_STP, steps); step(false, Y_DIR, Y_STP, steps);
//clockwise

delay(1000); // 
  
 step(true, X_DIR, X_STP, steps);step(true, Y_DIR, Y_STP, steps); //counter clockwise

 delay(5000); // 
}

Step them one step at a time and keep count of the steps.

#define EN        8

#define X_DIR     5
#define Y_DIR     6

#define X_STP     2
#define Y_STP     3

//DRV8825 - driver
int delayTime = 500; //speed
int steps = 1200; // 200 steps per revolution

void step(boolean dir, byte dirPin, byte stepperPin)

{
   digitalWrite(dirPin, dir);
   digitalWrite(stepperPin, HIGH);
   delayMicroseconds(delayTime);
   digitalWrite(stepperPin, LOW);
   delayMicroseconds(delayTime);
}

void setup()
{
   pinMode(X_DIR, OUTPUT);
   pinMode(X_STP, OUTPUT);
   pinMode(Y_DIR, OUTPUT);
   pinMode(Y_STP, OUTPUT);
   pinMode(EN, OUTPUT);
   digitalWrite(EN, LOW);
}

void loop()
{
   for (int n = 0; n < steps; n++)
   {
      step(false, X_DIR, X_STP);
      step(false, Y_DIR, Y_STP);
   }
   delay(1000);
   for (int n = 0; n < steps; n++)
   {
      step(true, X_DIR, X_STP);
      step(true, Y_DIR, Y_STP);
   }  
   delay(5000); 
}

i’ll guess that you’ll next want to simultaneously move each motor a different # of steps and in different directions such that it takes the same amount of time for both?

if so, you’ll need to determine how long it will take and then step each motor at a specific time so that they move synchronously. millis() can be used to determine when to step each motor

That would work but I see the function step (...) as the problem. It takes one direction, one direction pin, and one stepper pin. I would be tempted to generalize it to take an array of structs where each struct specifies a direction, a direction pin, and a stepper pin. The function would now take the array of structs, the length of the array, and the number of steps. The function would use the structs in the array to set the appropriate direction of all of the direction pins, and step all of the specified steppers more or less simultaneously... but that is just me. YMMV

@groundFungs

groundFungus:
Step them one step at a time and keep count of the steps.

#define EN        8

#define X_DIR    5
#define Y_DIR    6

#define X_STP    2
#define Y_STP    3

//DRV8825 - driver
int delayTime = 500; //speed
int steps = 1200; // 200 steps per revolution

void step(boolean dir, byte dirPin, byte stepperPin)

{
  digitalWrite(dirPin, dir);
  digitalWrite(stepperPin, HIGH);
  delayMicroseconds(delayTime);
  digitalWrite(stepperPin, LOW);
  delayMicroseconds(delayTime);
}

void setup()
{
  pinMode(X_DIR, OUTPUT);
  pinMode(X_STP, OUTPUT);
  pinMode(Y_DIR, OUTPUT);
  pinMode(Y_STP, OUTPUT);
  pinMode(EN, OUTPUT);
  digitalWrite(EN, LOW);
}

void loop()
{
  for (int n = 0; n < steps; n++)
  {
      step(false, X_DIR, X_STP);
      step(false, Y_DIR, Y_STP);
  }
  delay(1000);
  for (int n = 0; n < steps; n++)
  {
      step(true, X_DIR, X_STP);
      step(true, Y_DIR, Y_STP);
  } 
  delay(5000);
}

Thank you for that realy!!! but now i have another question. i Want to add 2 limit switches one on the left and one on the right side. I am trying to figur it out how to write that in arduino program but i don’t have andy idea (I have been programming for barely 2 weeks now) The motor will rotate in one direction until it reaches the limit switch. And when the limit switch will be presed the motor will change direction and rotate to another limit switch. When he will hit limit switch will he rotate just like before. And that will be doing in loop . Can you wrote code like this? limit switch one is one pine 9 and another one is on 10 and limith switch has normal open contact

Try using the accelSteper library. See the example in my tutorial Multi-tasking in Arduino which also covers checking your loop is running fast enough to keep the steppers running at the required speed.

I will not write the code for you. To do what you want, step the motors, check the switches, if the switch is closed reverse the direction, if not step the motor, check the switches, rinse, repeat.

Try writing the code. If you get stuck, post the code and tell us what the code actually does and how that is different from what you want.

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