Help: driving two stepper motors with AccelStepper library

Hey,

I’m trying to drive two wheels with stepper motors and BigEasy drivers. I’m using AccelStepper library

because I feel like it’s pretty beginner-friendly but I’m down to other suggestions too. So far through my

code, the robot can only go forward a certain distance and turn right. What I wanted to do was, after

turning right to avoid an obstacle, to turn left and continue moving forward.

Here’s my code:

#include <AccelStepper.h>

AccelStepper stepperL(1,22,23); //step pin = 22, dir pin = 23
AccelStepper stepperR(1,24,25);

AccelStepper* steppers[2] = {
  &stepperL,
  &stepperR
  };


void setup() {
  // put your setup code here, to run once:
  for(int i = 0; i<2; i++){
    steppers[i]->setMaxSpeed(800);
    steppers[i]->setAcceleration(100);
    }
}

void loop() {
  // put your main code here, to run repeatedly:
  steppers[0]->moveTo(10000);
  steppers[1]->moveTo(10000);
  steppers[0]->run();
  steppers[1]->run();

  steppers[0]->moveTo(18000);
  steppers[1]->moveTo(11000);
  steppers[0]->run();
  steppers[1]->run();
  
  steppers[0]->moveTo(20000);
  steppers[1]->moveTo(25000);
  steppers[0]->run();
  steppers[1]->run();

In the third part of the loop() I wanted to do the turning left part, but the steppers were stuttering

and wouldn’t go back to the previous speed in setup().

I’ve tried this too but the stepper motors just completely stopped after calling setCurrentPosition

if(steppers[0]->currentPosition()==18000){
     //steppers[0]->setCurrentPosition(0);
     steppers[0]->moveTo(23000);     
  }
  if(steppers[1]->currentPosition()==11000){
     //steppers[1]->setCurrentPosition(0);
     steppers[1]->moveTo(21000);         
  }
  steppers[0]->run();
  steppers[1]->run();

Any suggestion would be appreciated. Many thanks =)

I don't think you have grasped how the AccelStepper library works.

The run() function must be called fast and often (every iteration of loop() ). When it is called it calculates whether it is time for that motor to take a step. Most of the time it won't be.

It will keep figuring that out and making steps at the appropriate intervals until the motor reaches its destination. In your program you are changing the destination before it has even had time to do the first step.

Study the examples that come with the library.

There is also a function runToPosition() that will block everything until all the steps are done - but that is not appropriate for your project.

The BigEasydriver just requires step and direction signals so you could probably control the motors without any library. Have a look at the 2nd example in this Simple Stepper Code

...R Stepper Motor Basics

...R