Go Down

Topic: simultaneous stepper motor control using accelstepper (Read 712 times) previous topic - next topic

barath1997

hi
Code: [Select]


#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

12Stepper

The code below is not written for you ;) 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...

Code: [Select]
//  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




Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

barath1997

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.

Robin2

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
Code: [Select]
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
Two or three hours spent thinking and reading documentation solves most programming problems.

barath1997

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



Code: [Select]


#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.




Code: [Select]




#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();
     
     
     }

     
   
}


 
 
 


     
   
   
 





barath1997

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.


Code: [Select]
#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();

    }
   


 
 
 


     
   
   
 




Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

HermannSW

#8
Dec 09, 2019, 09:34 pm Last Edit: Dec 09, 2019, 09:40 pm by HermannSW
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


αβ, xy & L₁/L₂ positioning systems with two 28BYJ48 stepper motors:
https://forum.arduino.cc/index.php?topic=649769.0
stepper PT camera system:
https://forum.arduino.cc/index.php?topic=647703.0
https://stamm-wilbrandt.de/en/Raspberry_camera.html

barath1997

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.

Code: [Select]
#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");
 
 
 
  }   


   
       

 
     
   
 
 
 


     
   
   
 

barath1997

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

HermannSW

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:
Code: [Select]
// ULN2003
AccelStepper stepper(AccelStepper::HALF4WIRE, 2, 4, 3, 5);
AccelStepper stepper2(AccelStepper::HALF4WIRE, 8, 10, 9, 11);
αβ, xy & L₁/L₂ positioning systems with two 28BYJ48 stepper motors:
https://forum.arduino.cc/index.php?topic=649769.0
stepper PT camera system:
https://forum.arduino.cc/index.php?topic=647703.0
https://stamm-wilbrandt.de/en/Raspberry_camera.html

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

barath1997

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.

Code: [Select]
#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");
 
 
 
  }   


   
       

 
     
   
 
 
 


     
   
   
 

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up