Moving 2 Stepper Motors Simultaneously with Arduino Feather M0

I am currently trying to get two stepper motors to move to 2 different inputted angles (which are converted to steps) and reach that angle at the same time. I've messed with the example stepper motor code and was able to get both stepper motors to move simultaneously but for some reasons, they won't get to the angle I'd like them to get. I was able to get the stepper motors move to the angle properly but that was with a different format of the code where the stepper motors would move one at at a time. My guess is that its possible that the step function doesn't treats floats as integers but the problem is that I need an accurate angle to step converter so that the stepper motors reach the desired angle at the same time.

#include <Adafruit_MotorShield.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_StepperMotor *myMotor2 = AFMS.getStepper(200, 2);
Adafruit_StepperMotor myMotor = AFMS.getStepper(200,1);
float angle;
float angle_2;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  while (!Serial);
  Serial.println("Stepper test!");

  if (!AFMS.begin()) {         // create with the default frequency 1.6KHz
  // if (!AFMS.begin(1000)) {  // OR with a different frequency, say 1KHz
    Serial.println("Could not find Motor Shield. Check wiring.");
    while (1);
  }
  Serial.println("Motor Shield found.");

  myMotor->setSpeed(40);  // 10 rotations per minute
  myMotor2->setSpeed(40);
  while (Serial.available() == 0) {

  }
  angle = Serial.parseInt();
  Serial.println("The number inputed was: ");
  Serial.println(angle);
  while (Serial.available() == 0) {

  }
  angle_2 = Serial.parseInt();
  Serial.println("The number inputed was: ");
  Serial.println(angle_2);
}

void loop() {

  float step1Inc = angle / 360.0;
  float step2Inc = angle_2 / 360.0;

  float step1Pos = 0.0;
  float step2Pos = 0.0;
  for (int i=0; i < 200; i++)
  {
    step1Pos += step1Inc;
    step2Pos += step2Inc;
    Serial.println("Black Motor Movement.");
    myMotor->step(step1Pos, FORWARD, DOUBLE);
    /myMotor->step(step1Pos, BACKWARD, DOUBLE);/
    Serial.println("Grey Motor Movement");
    //Give or take, 200 steps is typically a full rotation.
    myMotor2->step(step2Pos, FORWARD, DOUBLE);
    /myMotor2->step(step2Pos, BACKWARD, MICROSTEP);*/
  }
}```

Thanks for your time! Let me know if there's any more information I can give to make the problem clearer if needbe.

Have you looked at the MultiStepper class of the AccelStepper library?

shouldn't the code determine the # of steps for each motor to travel and determine a delay between the steps of each motor and a step each motor independently so that each motor reaches the final step at the same time?

Do you have an example of how that would work?

I will take a look at this, thank you!

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