Stepper synchronisation

Hi I could really do with some advice on the following.

I have built a simple two link X,Y plotter powered by two stepper motors, one at the shoulder, a nema 17 (200 step per rev) and one at the elbow 28BYJ-48 (2038 steps per rev). I am controlling them using an Adafruit Motorshield V2, attached to a Arduino Mega 2560. I am trying to get both motors to turn a different number of steps, but to start and finish together so,that the arm they are controlling can draw a straight line on an X,Y coordinate. I have been trying to use the Accel library, but cant achieve this, either the steps are shared alternately producing a zigzag out put, or as with my latest attempt (see code below) result in only one step each with the higher stepping motor working alone at the end. How can I overcome this?

#include <Wire.h>
#include <AccelStepper.h>
#include <Adafruit_MotorShield.h>

Adafruit_MotorShield AFMS; // Default address, no jumpers


Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(2038, 1);// 28BYJ-48
Adafruit_StepperMotor *myStepper2 = AFMS.getStepper(200, 2);// Nema 17



void forwardstep1() {  
  myStepper1->step(FORWARD, SINGLE);
}
void backwardstep1() {  
  myStepper1->step(BACKWARD, SINGLE);
}
// wrappers for the second motor!
void forwardstep2() {  
  myStepper2->step(FORWARD, SINGLE);
}
void backwardstep2() {  
  myStepper2->step(BACKWARD, SINGLE);
}

//  wrap the steppers in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);

// Used to establish starting position
long Travel1; // for stepper1
long Travel2; //for stepper2



void setup()
{  
  AFMS.begin(); // Start the shield
  Serial.begin(9600);

   
  stepper1.setMaxSpeed(500.0);
  stepper1.setAcceleration(500.0);
  stepper1.moveTo(200); // number of steps for stepper1
  
    
  stepper2.setMaxSpeed(500.0);
  stepper2.setAcceleration(500.0);
  stepper2.moveTo(10); // number of steps for stepper2
  

 
  
}

void loop()
{

    stepper1.run();
    stepper2.run();
    Serial.print("stepper1.distanceToGo ");
    Serial.println(stepper1.distanceToGo());// Used to monitor stepper1 progress
    Serial.print("stepper2.distanceToGo ");
    Serial.println(stepper2.distanceToGo()); // Used to monitor stepper2 progress
    //delay(1000); // delay for monitoring purposes
   
    // return to starting position
        
    if (stepper1.distanceToGo() == 0)
    stepper1.moveTo(Travel1);
    
    if (stepper2.distanceToGo() == 0)
    stepper2.moveTo(Travel2);
    
    



   

}

I am trying to get both motors to turn a different number of steps, but to start and finish together

The AccelStepper library is specifically NOT suited to your requirement. The MultiStepper version of the library is, but it does not do acceleration.

And it is not all that difficult to write your own acceleration code if you need. This Simple acceleration code may help, however it is written on the assumption that the user has a specialised stepper driver that takes step and direction signals - which the 28BYJ drivers do not. You could write some functions to control your motors that produce the same effect as calling step() and direction().

...R
Stepper Motor Basics
Simple Stepper Code

I have used AccelStepper to run a 28BYJ-48 with ULN2003, IIRC, it gives 4096 PPR (half step mode), don’t remember what this sketch was meant to do, maybe nothing, but it worked. :slight_smile:

#include <AccelStepper.h>

// Define a stepper and the pins it will use, for Nano, Uno
AccelStepper stepper(8, 8, 10, 9, 11);
uint32_t pos = 2048;
void setup()
{  
  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(1000.0);
  stepper.setSpeed(341.333);
  stepper.setAcceleration(100.0);
}
void loop()
{
    stepper.run();
    stepper.setSpeed(341.333);
     //If at the end of travel go to the other end
   
      stepper.move(pos);
     //stepper.setSpeed(4.55364);
      //delay(500);
     if (stepper.distanceToGo() == 0)
       stepper.move(pos);
     
   if (stepper.distanceToGo() == 0)
   {
      stepper.moveTo(0);
      stepper.setSpeed(-4.55364);
      delay(500);
   }   
}

JCA34F:
I have used AccelStepper to run a 28BYJ-48 with ULN2003, IIRC,

That will work fine.

What the AccelStepper library will not do is run several motors in a synchronized fashion - which is what the OP wants.

...R