simultaneous stepper motor control using accelstepper

hi

#include<AccelStepper.h>
   
AccelStepper stepper1 = AccelStepper(1, 3, 4);
                                  
AccelStepper stepper2 = AccelStepper(1, 12, 13);

int nos;
int noos;
void setup() {

  
  stepper1.setMaxSpeed(2000);
  stepper2.setMaxSpeed(2000);
  
  
  Serial.begin(9600);
  
  
  

}

void loop() {
  
  while (Serial.available()>0)
  {
    Serial.println("enter nos");
     
      nos = Serial.parseInt();
      noos = Serial.parseInt();
     stepper1.moveTo(nos);
     stepper1.runToPosition();
     stepper2.moveTo(noos);
       stepper2.runToPosition();

   }
     
    }

here after one motor stops the other moves,i want tomove them both simultaneously.
how to do it?pls help

The code below is not written for you :wink: but is something I wrote a while back, and may give you some ideas. It runs 2 steppers simultaneously, with blink without delay on pin 14 to prove nothing blocks, and even a button to change the speed on the fly...

//  inspired by https://www.makerguides.com/28byj-48-stepper-motor-arduino-tutorial/
//  19 august 2019

// the accelstepper library... non-blocking
// trying 2 motors

/*
   Example sketch to control a 28BYJ-48 stepper motor with ULN2003 driver board, AccelStepper and Arduino UNO.
  More info: https://www.makerguides.com
*/

// Include the AccelStepper library:
#include <AccelStepper.h>
// Motor pin definitions:
//motor 1
#define motorPin1_1  8      // IN1 on the ULN2003 driver
#define motorPin1_2  9      // IN2 on the ULN2003 driver
#define motorPin1_3  10     // IN3 on the ULN2003 driver
#define motorPin1_4  11     // IN4 on the ULN2003 driver
//motor 2
#define motorPin2_1  4      // IN1 on the ULN2003 driver
#define motorPin2_2  5      // IN2 on the ULN2003 driver
#define motorPin2_3  6     // IN3 on the ULN2003 driver
#define motorPin2_4  7     // IN4 on the ULN2003 driver
// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1_1, motorPin1_3, motorPin1_2, motorPin1_4);
AccelStepper stepper2 = AccelStepper(MotorInterfaceType, motorPin2_1, motorPin2_3, motorPin2_2, motorPin2_4);

//bwod to see if it blocks or not
unsigned long previousBlink;
int blinkTerval = 250;
bool blinkState = false;
byte blinkPin = 14;

// going to use a button to change speed on the fly
int basicSpeed = 500;
int motor1speed;
int motor2speed;
byte speedPin = 2;

void setup()
{
  Serial.begin(9600);
  Serial.println(".... Accelstepper constant speed, 2 motors  ....");
  Serial.print("Compiler: ");
  Serial.print(__VERSION__);
  Serial.print(", Arduino IDE: ");
  Serial.println(ARDUINO);
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);

  //turn off L13
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  pinMode(blinkPin, OUTPUT);
  digitalWrite(blinkPin, blinkState);

  pinMode(speedPin, INPUT_PULLUP);

  // Set the maximum steps per second:
  stepper.setMaxSpeed(1000);
  stepper2.setMaxSpeed(1000);

  Serial.println("\nsetup() done\n");

} //setup

void loop()
{
  getSpeed(); //depends on button
  doSteps();
  doPulse();

} //loop

void doPulse()
{
  if (millis() - previousBlink >= blinkTerval)
  {
    previousBlink = millis();
    blinkState = !blinkState;
    digitalWrite(blinkPin, blinkState);
  }
}//pulse

void getSpeed()
{
  if (digitalRead(speedPin)) //not pressed, normal speed
  {
    motor1speed = basicSpeed;
    motor2speed = basicSpeed;
    blinkTerval=500;
  }
  else  //pressed, motor 1 slow, motor 2 fast
  {
    motor1speed = 0.2 * basicSpeed;
    motor2speed = 2.0 * basicSpeed;
    blinkTerval=100;
  }
  // Set the actual speed of the motor in steps per second:
  stepper.setSpeed(motor1speed);
  stepper2.setSpeed(motor2speed);
}//getspeed

void doSteps()
{
  // Step the motor with constant speed as set by setSpeed():
  stepper.runSpeed();
  stepper2.runSpeed();
}//steps

barath1997:
here after one motor stops the other moves,i want tomove them both simultaneously.

You should be using run() rather than runToPosition().

If you read the AccelStepper documentation you will see that runToPosition() blocks until it completes.

Study the examples that come with the AccelStepper library.

...R

yes i get it but using run() or runSpeed() instead of runToposition() doesnt make the motor move simultaneously.

iam making some mistakes in the loops i guess.

using run() inside while loop makes the motor turn only a single step.
if i use run() outside while loop and inside void loop then motor turns continously.

barath1997:
yes i get it but using run() or runSpeed() instead of runToposition() doesnt make the motor move simultaneously.

Without seeing the code where you tried run() I can't figure out what mistake you are making.

A common way is to have

stepper0.run();
stepper1.run();

as the last lines in loop(). But you must make sure that loop() can repeat much faster than the required step rate.

Both WHILE and FOR create blocking loops and should not be used.

...R

this code makes only one motor move( stepper1) the given number of steps.

#include<AccelStepper.h>
   // 1 = Easy Driver interface
                                  // UNO Pin 2 connected to STEP pin of Easy Driver
                                  // UNO Pin 3 connected to DIR pin of Easy Driver
AccelStepper stepper1 = AccelStepper(1, 3, 4);
                                  
AccelStepper stepper2 = AccelStepper(1, 6, 7);

int nos;
int noos;
void setup() {

  
  stepper1.setMaxSpeed(2000);
  stepper2.setMaxSpeed(2000);
  
  
  Serial.begin(9600);
  
  }

void loop() {
  
  while (Serial.available()>0)
  {
    Serial.println("enter nos");
     
      nos = Serial.parseInt();
      noos = Serial.parseInt();
      stepper2.moveTo(nos);
      stepper1.moveTo(noos);
      
    
     
     }

     stepper1.run();
     stepper2.run();
      
    
}

this code makes both motors move only one step.

#include<AccelStepper.h>
   // 1 = Easy Driver interface
                                  // UNO Pin 2 connected to STEP pin of Easy Driver
                                  // UNO Pin 3 connected to DIR pin of Easy Driver
AccelStepper stepper1 = AccelStepper(1, 3, 4);
                                  
AccelStepper stepper2 = AccelStepper(1, 6, 7);

int nos;
int noos;
void setup() {

  
  stepper1.setMaxSpeed(2000);
  stepper2.setMaxSpeed(2000);
  
  
  Serial.begin(9600);
  
  }

void loop() {
  
  while (Serial.available()>0)
  {
    Serial.println("enter nos");
     
      nos = Serial.parseInt();
      noos = Serial.parseInt();
      stepper2.moveTo(nos);
      stepper1.moveTo(noos);
      
    stepper1.run();
     stepper2.run();
      
     
     }

     
    
}

this one is able to rotate only one motor,i dont know why?i should be able to move 2 motors simultaneously with this code.

#include<AccelStepper.h>

AccelStepper stepper1 = AccelStepper(1, 3, 4);
                                  
AccelStepper stepper2 = AccelStepper(1, 6, 7);

int nos;
int noos;
void setup() {

  
  stepper1.setMaxSpeed(2000);
  stepper2.setMaxSpeed(2000);

    
  
  Serial.begin(9600);
  
  }

void loop() {
  
  while (Serial.available()>0)
  {
    Serial.println("enter nos");
    Serial.println("enter noos");
     
     nos = Serial.parseInt();
     noos = Serial.parseInt();

     

      
    
    
    }
    
     
     stepper1.moveTo(nos);
     stepper2.moveTo(noos);
      
     stepper1.run();
     stepper2.run();

    }

In your second program take the lines stepper.run() out of the WHILE loop. Recall what I said about WHILE in Reply #4

For the future, please just post one program - the one that represents your best attempt. That makes it much easier to help.

...R

I learned recently how to control steppers with AccelStepper library myself.

For my self-built stepper Pan Tilt camera system (with 4 drops of superglue) I used the (AccelStepper library based) sketch in this posting to move both motors together based on joystick input:
https://forum.arduino.cc/index.php?topic=647703.msg4369656#msg4369656

Later I did control the steppers completely alone with self contained program running on Raspberry Pi (only using pigpio library). There you can see how to do everything yourself (because there was no AccelStepper library on Raspberry):
https://www.raspberrypi.org/forums/viewtopic.php?f=33&t=256740&p=1565748#p1565748

after i upload this code both motors move one step back and forth continuosly.if i enter two values in the serial monitor (30,50) or (30,80)
both motors are not moving.

if i enter only one value say 30 or 50 then only one motor moves to the given steps and other is not moving.

#include<AccelStepper.h>

AccelStepper stepper1 = AccelStepper(1, 3, 4);
                                  
AccelStepper stepper2 = AccelStepper(1, 6, 7);

int nos;
int noos;


 
void setup() {

  
  stepper1.setMaxSpeed(200);
  stepper2.setMaxSpeed(200);
  Serial.begin(9600);

  stepper1.setCurrentPosition(0);
   stepper2.setCurrentPosition(0);
  
 }

void loop() 
  
  {
 
  while (Serial.available()>0)
  {
    
    Serial.println("enter nos");
    Serial.println("enter noos");
     
     nos = Serial.parseInt();
     noos = Serial.parseInt();

    
  

  }


stepper2.setSpeed(30);
  stepper2.runToNewPosition(nos);
  Serial.println("motor2 runs");
  stepper1.setSpeed(20);
  stepper1.runToPosition(noos);
  Serial.println("motor1 runs");
  
 
  
  }

if i use run() instead of runtonewposition() the motors both move fast continuously.

I don't know why you do not get an error.
Please see the AccelStepper api, caling with 3 numbers just seems wrong.
https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html

This is how I call the API, depends on what stepper you use and its number of wires:

// ULN2003
AccelStepper stepper(AccelStepper::HALF4WIRE, 2, 4, 3, 5);
AccelStepper stepper2(AccelStepper::HALF4WIRE, 8, 10, 9, 11);

barath1997:
if i use run() instead of runtonewposition() the motors both move fast continuously.

That looks like progress.

Post the program that behaves like that.

...R

hermannsw i use a stepper driver a4988 for my project so i connect motor to the driver not to the adruino directly.

robin2 pls see the codes.

#include<AccelStepper.h>

AccelStepper stepper1 = AccelStepper(1, 3, 4);
                                  
AccelStepper stepper2 = AccelStepper(1, 6, 7);

int nos;
int noos;


 
void setup() {

  
  stepper1.setMaxSpeed(200);
  stepper2.setMaxSpeed(200);
  Serial.begin(9600);

  stepper1.setCurrentPosition(0);
   stepper2.setCurrentPosition(0);
  
 }

void loop() 
  
  {
 
  while (Serial.available()>0)
  {
    
    Serial.println("enter nos");
    Serial.println("enter noos");
     
     nos = Serial.parseInt();
     noos = Serial.parseInt();

    
  

  }


  
   stepper2.setSpeed(30);
  
   stepper1.setSpeed(20);
  stepper2.run();
  stepper1.run();
   Serial.println("motor2 runs");
  Serial.println("motor1 runs");
  
  
  
  }

barath1997:
robin2 pls see the codes.

Your program gets values from the user into two variable nos and noos. As the names are meaningless I have no idea what you want to do with those values.

Always use meaningful variable names - for example stepper1NumberOfSteps or stepper1MillisBetweenSteps. Apart from anything else it makes the logic more obvious.

...R

1 Like
#include<AccelStepper.h>

AccelStepper stepper1 = AccelStepper(1, 3, 4);
                                 
AccelStepper stepper2 = AccelStepper(1, 6, 7);

int stepper1numberofsteps;
int stepper2numberofsteps;


 
void setup() {

 
  stepper1.setMaxSpeed(200);
  stepper2.setMaxSpeed(200);
  Serial.begin(9600);

  stepper1.setCurrentPosition(0);
   stepper2.setCurrentPosition(0);
 
 }

void loop()
 
  {
 
  while (Serial.available()>0)
  {
   
    Serial.println("enter stepper1numberofsteps");
    Serial.println("enter stepper2numberofsteps");
     
     stepper1numberofsteps = Serial.parseInt();
     stepper2numberofsteps = Serial.parseInt();

   
 

  }


 
   stepper2.setSpeed(30);
 
   stepper1.setSpeed(20);
  stepper2.run();
  stepper1.run();
   Serial.println("motor2 runs");
  Serial.println("motor1 runs");
 
 
 
  }

ok i have changed the variable names.then what is the problem with the code now?
why isnt it working properly?
why are the motors not working together?
can u find the mistakes in the logics?

barath1997:
ok i have changed the variable names.then what is the problem with the code now?
why isnt it working properly?
why are the motors not working together?
can u find the mistakes in the logics?

There seems to be a huge gap in your understanding of how a program works. Changing the names of variables makes no difference to the Arduino. That is only to make things easier for the programmer.

You have the two variables that get data from the user, but you have no code within your program to use that data. How can you expect the program to respond to the user input?

You need lines like stepperX.move(stepperXnumberofsteps); to make use of the data from the user

Have you carefully studied the examples that come with the AccelStepper library so you can see how the programs should be constructed?

...R

PS ... It is not a sin if you don't want to learn programming. If you just want someone to write a program for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay.

#include<AccelStepper.h>

AccelStepper stepper1 = AccelStepper(1, 3, 4);
                                 
AccelStepper stepper2 = AccelStepper(1, 6, 7);

int stepper1numberofsteps;
int stepper2numberofsteps;


 
void setup() {

 
  stepper1.setMaxSpeed(200);
  stepper2.setMaxSpeed(200);
  Serial.begin(9600);

  stepper1.setCurrentPosition(0);
   stepper2.setCurrentPosition(0);
 
 }

void loop()
 
  {
 
  while (Serial.available()>0)
  {
   
    Serial.println("enter stepper1numberofsteps");
    Serial.println("enter stepper2numberofsteps");
     
     stepper1numberofsteps = Serial.parseInt();
     stepper2numberofsteps = Serial.parseInt();

   
  stepper1.moveTo(stepper1numberofsteps);
  stepper1.moveTo(stepper2numberofsteps);


  }


 
   stepper2.setSpeed(30);
 
   stepper1.setSpeed(20);
  stepper2.run();
  stepper1.run();
   Serial.println("motor2 runs");
  Serial.println("motor1 runs");
 
 
 
  }

ya done,but still the issue is not resolved one one motor moves and the other rests still.

barath1997:
ya done,but still the issue is not resolved one one motor moves and the other rests still.

Your description of how the program works is not very detailed. For example you don't say how the data entry part of the program is working.

My guess is that it is not actually taking in two numbers for the two motors. What do you see if you print the values of stepperXnumberofsteps immediately after the moveTo() lines?

Using Serial.print() statements to see how variables change is a normal part of debugging a problem.

NOTE also that I suggested stepper.move() rather than stepper.moveTo(), though it won't make any difference in the short term.

...R

#include "AccelStepper.h" 
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/

// AccelStepper Setup
AccelStepper stepperX(1, 3, 4);   // 1 = Easy Driver interface
                                  // UNO Pin 2 connected to STEP pin of Easy Driver
                                  // UNO Pin 3 connected to DIR pin of Easy Driver
                                  
AccelStepper stepperZ(1, 6, 7);   // 1 = Easy Driver interface
                                  // UNO Pin 5 connected to STEP pin of Easy Driver
                                  // UNO Pin 6 connected to DIR pin of Easy Driver

// Stepper Travel Variables
long TravelX;  // Used to store the X value entered in the Serial Monitor
long TravelZ;  // Used to store the Z value entered in the Serial Monitor

int move_finished=1;  // Used to check if move is completed


void setup() {
  
  Serial.begin(9600);  // Start the Serial monitor with speed of 9600 Bauds
  
// Print out Instructions on the Serial Monitor at Start
  Serial.println("Enter Travel distance seperated by a comma: X,Z ");
  Serial.print("Enter Move Values Now: ");

//  Set Max Speed and Acceleration of each Steppers
  stepperX.setMaxSpeed(500.0);      // Set Max Speed of X axis
  stepperX.setAcceleration(500.0);  // Acceleration of X axis

  stepperZ.setMaxSpeed(250.0);      // Set Max Speed of Y axis slower for rotation
  stepperZ.setAcceleration(250.0);  // Acceleration of Y axis

}


void loop() {

while (Serial.available()>0)  { // Check if values are available in the Serial Buffer

  move_finished=0;  // Set variable for checking move of the Steppers
  
  TravelX= Serial.parseInt();  // Put First numeric value from buffer in TravelX variable
  Serial.print(TravelX);
  Serial.print(" X Travel , ");
  
  TravelZ= Serial.parseInt();  // Put Second numeric value from buffer in TravelZ variable
  Serial.print(TravelZ);  
  Serial.println(" Y Travel ");
  
  stepperX.moveTo(TravelX);  // Set new move position for X Stepper
  stepperZ.moveTo(TravelZ);  // Set new move position for Z Stepper
  
  delay(1000);  // Wait 1 seconds before moving the Steppers
  Serial.print("Moving Steppers into position...");
  }

// Check if the Steppers have reached desired position
  if ((stepperX.distanceToGo() != 0) || (stepperZ.distanceToGo() !=0)) {
    
    stepperX.run();  // Move Stepper X into position
    stepperZ.run();  // Move Stepper Z into position
    
  }

// If move is completed display message on Serial Monitor
  if ((move_finished == 0) && (stepperX.distanceToGo() == 0) && (stepperZ.distanceToGo() == 0)) {
    Serial.println("COMPLETED!");
    Serial.println("");
    Serial.println("Enter Next Move Values (0,0 for reset): ");  // Get ready for new Serial monitor values
    move_finished=1;  // Reset move variable
  }
}

exactly,the codes work if i input only one value in the serial monitor and one motor moves,but if i enter 2 values both motors are not moving.

pls tell me how to do i clear up this variable problem?

this code i took from onnline,here is see that while i enter two values separated by commas the motors are stationary.

whereas if i enter one value then motor is moving and the value is displayed.

pls see the attachment where u can find the output of this code.

thanks