2

I want to run two stepper motors one after the other. Stepper 1 has to move 5 rotations, and stepper 2 has to move 1 rotation. Here is the code.

Stepper 1 is nema 34 And stepper 2 is nema 24

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper1 (1, 3, 2); // Defaults to 4 pins on 2, 3, 4, 5 mortor (A)
AccelStepper stepper2 (1, 5, 4); // Defaults to 4 pins on 2, 3, 4, 5 motor (B)

long Steps_For_Distance1 = 3; // 3cm
long Steps_For_Distance2 = 5; // 5cm
long currentPos = 0;
long targetPos = 200;
long targetPos_Reverse = -200;
long Motor_Speed1 = 50;
long Motor_Speed2 = 400;
long Motor_Speed3 = 800;
int Op_Sw = 0;
const int buttonPin = 2;     // the number of the pushbutton pin

const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void checkStepper1_For_DT(AccelStepper &astepper)
{
  if (astepper.distanceToGo() == 0)
  {
    // Random change to speed, position and acceleration
    // Make sure we dont get 0 speed or accelerations
    astepper.setCurrentPosition(currentPos);
    astepper.moveTo(targetPos);
    astepper.setSpeed(Motor_Speed1);
    astepper.setAcceleration(100);
  }
}
void checkStepper2_For_360(AccelStepper &astepper)
{
  if (astepper.distanceToGo() == 0)
  {
    // Random change to speed, position and acceleration
    // Make sure we dont get 0 speed or accelerations

    astepper.moveTo(targetPos);
    astepper.setMaxSpeed(Motor_Speed2);
    astepper.setAcceleration(100);
  }
}
void PatternToBeLooped_For_Cycle1()
{
  int count = 0;

  for (count == 0; count <= 5; count ++)
  {
    checkStepper1_For_DT(stepper1);
    stepper1.runSpeedToPosition();
  }

  checkStepper2_For_360(stepper2);
  stepper2.runSpeedToPosition();
}
void loop() {
    PatternToBeLooped_For_Cycle1();
  }

I ran the code, but it didn't work. Both motors are running at the same time.

Please help me. Where did I make a mistake?

Thank you in advance.


I tried with delay(1000);, but But still both steppers are moving at the same time and get delay at the same time.

4
  • 1
    please show the wiring diagram
    – jsotola
    Commented Feb 10, 2018 at 16:42
  • May it be that they are not moving at the same time but moving one after another very fast, so that you cannot see the difference? Try putting a delay inbetween of the two stepper run statements in your looped function and see what happens
    – chrisl
    Commented Feb 11, 2018 at 12:00
  • -Dir pin to arduino gnd Commented Feb 12, 2018 at 2:52
  • -Dir pin , - pulse pin to arduino gnd. And + dir to arduino 3 pin ,5pin. And +pulse to pin 2,4 Commented Feb 12, 2018 at 5:06

1 Answer 1

1

I looked into the source of the AccelStepper library here, which may be your library or one similar to it. The function runSpeedToPosition() does not block until the desired position is reached. Instead inside the function you see code that executes a step, when the right time (according to millis()), calculated through the speed, is reached, else it will return false. You are supposed to call this function very often while the motor moves. This enables you to drive more than one motor simultaneously with different speeds.

Since you don't want to drive both motors at the same time, you have to loop, until the first motor have reached the desired position; then doing the same with the second motor. Try something like this:

void PatternToBeLooped_For_Cycle1()
{
  int count = 0;
  for (count == 0; count <= 5; count ++)
  {
    checkStepper1_For_DT(stepper1);
    while(stepper1.currentPosition() != stepper1.targetPosition()) stepper1.runSpeedToPosition();
  }

  checkStepper2_For_360(stepper2);
  while(stepper2.currentPosition() != stepper2.targetPosition()) stepper2.runSpeedToPosition();
}

Note: With this change the function PatternToBeLooped_For_Cycle1() blocks the execution of the loop function, until the motors have done their movement. To avoid this you can use an if-statement, which executes the code for stepper1 or stepper2 depending on the millis() function. This works the same way as the BlinkWithoutDelay example in the Arduino IDE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.