Stepper Motor Reverse A4988 AccelStepper

Hello

Im stuggling to get my stepper motor to go in reverse.

Here is the code I am using:

#include <AccelStepper.h>

#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
  
  stepper.setMaxSpeed(1000);
}
void loop() { 
 
  stepper.setCurrentPosition(0);
  
  while(stepper.currentPosition() != 400)
  {
    stepper.setSpeed(200);
    stepper.runSpeed();
  }
  delay(1000);
  
  stepper.setCurrentPosition(0);
}

When using this code, the motor moves in the forward directions as to should but I want to reset the motor position to zero (reversing its previous forward movement) it does move at all.

The only way in which I have got the motor to move forward then in reverse is by using the acceleration function of accelstepper.h : using

#include <AccelStepper.h>

#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {

  stepper.setMaxSpeed(200);
  stepper.setAcceleration(30);
}
void loop() {

  stepper.moveTo(600);

  stepper.runToPosition();
  delay(1000);
  
  stepper.moveTo(0);
  stepper.runToPosition();
}

etc

The problem with this though is that it accelerates and I want the speed to be constant.

Any help would be greatly appreaciated.

Thanks
Tom

accellstepper_test.ino (376 Bytes)

Your first program moves the motor to position 400 and then tells AccelStepper to treat that position as 0

To my mind the WHILE gets in the way of the logic and is unnecessary. Try something simpler like this

if (stepper.currentPosition == 0) {
   stepper.moveTo(400);]
}
if (stepper,currentPosition == 400) {
  stepper.moveTo(0);
}
stepper.setSpeed(200);
stepper.runSpeed();

It would probably make sense to put the setSpeed() line in setup()

...R

bases on a description of AccelStepper, calling setCurrentPosition() with a value resets the current reference point but does not move the motor. It moves in the one direction because runSpeed() is repeatedly called.

but after the delay, the code simply calls setCurrentPostion(0) to reset the reference point but never calls a function to move the motor to a new position.

i agree with Robin's suggestion

Thanks for the replies.
Makes a lot more sense now.

Is there any command within Accelstepper that can reset the motor’s position back to zero. I had a look on the Accelstepper website but couldn’t find anything.

Thanks again

tomdixo:
Is there any command within Accelstepper that can reset the motor’s position back to zero.

It depends whether you mean "treat the current position as 0" or "move the motor back to the zero position"

You have yourself used the function for the first case - stepper.setCurrentPosition()

And the code I posted shows how to do the second.

By the way there is no means for a stepper to know where it is in an absolute sense. It is usual to have code that runs at startup that moves the motor until it trips a limit switch so the Arduino can identify that position as HOME or ZERO. After that the Arduino can know where it is by keeping count of the steps - provided the motor never misses a step due to overloading.

...R

tomdixo:
Thanks for the replies.
Makes a lot more sense now.

Is there any command within Accelstepper that can reset the motor’s position back to zero. I had a look on the Accelstepper website but couldn’t find anything.

Thanks again

Yes, you just call motor.moveTo(0);

The problem with this though is that it accelerates and I want the speed to be constant.

Accelstepper always accelerates to speed and down again, that's a physically requirement of the motor itself to avoid miss-stepping. Perhaps you meant the acceleration was far too slow? You should always set the acceleration in setup() to the value that's appropriate to your motor setup. Try higher and higher values until the motor miss-steps, then back-off 30% or so.