Need to remove acceleration with AccelStepper driven motor

I have the following code to move a motor in one direction or another based on serial input. It uses the accelstepper library and the motor is driven by an EasyDriver. The code works fine to drive the motor to the desired positions with acceleration. However, I would like to not use acceleration and move the stepper at full speed from one position to the other.

I’ve tried probably 10 different approaches to get this to work, based on code samples I’ve found on the internet, and reviewed the official accelstepper documentation but so far, I’ve been unable to make it work.

There must be something I’m overlooking. Any suggestions or links to code examples that will move the motor to a specific position without acceleration would be great.

#include <AccelStepper.h>
AccelStepper stepper(1,2,3);

int DIR = 3;
int STEP = 2;
int MS1 = 13;
int MS2 = 9;

void setup()
{
stepper.maxSpeed(3000);

//Easy Driver
pinMode(DIR, OUTPUT);
pinMode(STEP, OUTPUT);
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);

//Set Stepping Mode for Easy Driver to full stepping
digitalWrite(13, HIGH);
digitalWrite(9, HIGH);

Serial.begin(9600);
while (!Serial)
        {
        ;//wait for serial port to connect
        }
LastMotorState = 1;
}


void loop()
{
        if(Serial.available())
        {
            TriggerByte = Serial.read();
        }
        if(TriggerByte == 65 //ASCI "A"
           &&
           LastMotorState == 1)
         {
            stepper.runToNewPosition(-800);
            LastMotorState = 2;
        }
        if(TriggerByte == 66 //ASCI "B"
           &&
           LastMotorState == 2)
         {
            stepper.runToNewPosition(800);
            LastMotorState = 1;
        }
}

AccelStepper: AccelStepper Class Reference runSpeedToPosition might be what you want.

There must be something I'm overlooking

It might be that your motor is not capable of running at the speed you want without using acceleration. Try a lower speed.

groundfungus:
AccelStepper: AccelStepper Class Reference runSpeedToPosition might be what you want.

...I thought so too and this was the first thing I tried, but I couldn't get it to work.

Grumpy_Mike:

There must be something I'm overlooking

It might be that your motor is not capable of running at the speed you want without using acceleration. Try a lower speed.

The motor works at this speed... I tested it earlier with the Constant Speed example code for the AccelStepper

Arduino-x:
The motor works at this speed... I tested it earlier with the Constant Speed example code for the AccelStepper

So why not use that in your project?

...R

There must be something I'm overlooking.

Physics ?
Object at standstill must accelerate to reach any velocity ?

Robin2:
So why not use that in your project?

…R

…I tried the following , but the motor doesn’t turn at all.

void loop()
{
        if(Serial.available())
        {
            TriggerByte = Serial.read();
        }
        if(TriggerByte == 65 //ASCI "A"
           &&
           LastMotorState == 1)
         {
            stepper.setSpeed(3000);
            stepper.setCurrentPosition(800);
            stepper.moveTo(-800);
                 while(stepper.currentPosition() != -800)
                 stepper.runSpeed();
            LastMotorState = 2;
        }
        if(TriggerByte == 66 //ASCI "B"
           &&
           LastMotorState == 2)
         {
            stepper.setSpeed(3000);
            stepper.setCurrentPosition(-800);
            stepper.moveTo(800);
                 while(stepper.currentPosition() != 800)
                 stepper.runSpeed();
            LastMotorState = 1;
        }
}

There must be something I am overlooking, but I can’t figure out what.

raschemmel:
Physics ?
Object at standstill must accelerate to reach any velocity ?

Indeed, accelerate they must! :wink: …but the documentation of Accelstepper insists that stepping the motor without using the algorithmic acceleration is possible.

Have you tried a very short sketch without IFs or FORs that just gets the stepper to move as far as you want using the Accelstepper library?

…R

The second post today from some one using this library without reading the instructions:-

void AccelStepper::setCurrentPosition ( long position )

Resets the current position of the motor, so that wherever the motor happens to be right now is considered to be the new 0 position. Useful for setting a zero position on a stepper after an initial hardware positioning move. Has the side effect of setting the current motor speed to 0.

Spot what is wrong with your code now?

@Arduino-x,

click the MODIFY button in the upper right of the post window.
Highlight all you code.
click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button.
click SAVE (at the bottom).
When you post code on the forum, please make a habit of using the code tags "#" button.

Try increasing the acceleration value until the motor mis-steps and then backing
off a bit. Don't imagine a real motor can accelerate at an infinite rate, but rather
find out how fast you can get yours to accelerate in practice - that's the best
you can do.

Robin2:
Have you tried a very short sketch without IFs or FORs that just gets the stepper to move as far as you want using the Accelstepper library?

...R

The code posted at the top of this thread uses the Accelstepper library with acceleration and it works flawlessly, it's just that I had hoped to have the option to do it without acceleration.

Grumpy_Mike:
Spot what is wrong with your code now?

I'm not sure that I do. I altered the code so that the speed would be set after the current position was defined (below), but still the motor doesn't turn.

            stepper.setCurrentPosition(800);
            stepper.setSpeed(3000);
            stepper.moveTo(-800);
                 while(stepper.currentPosition() != -800)
                 stepper.runSpeed();

MarkT:
Try increasing the acceleration value until the motor mis-steps and then backing
off a bit. Don't imagine a real motor can accelerate at an infinite rate, but rather
find out how fast you can get yours to accelerate in practice - that's the best
you can do.

I think you may be right Mark. It seems like the most practical solution at this point.

Do so while your motor is on load.
If it is running free (without the load), you'll be doing work for nothing.

Arduino-x:

Robin2:
Have you tried a very short sketch without IFs or FORs that just gets the stepper to move as far as you want using the Accelstepper library?
...R

The code posted at the top of this thread uses the Accelstepper library with acceleration and it works flawlessly, it's just that I had hoped to have the option to do it without acceleration.

I meant a short sketch that uses the Accelstepper library without acceleration - to learn what is possible.

...R

Yes, I went through all of the code examples on th Accelstepper website, and they worked. Unfortunately, I couldn't adapt them successfully to what I wanted to do.

For the sake of consistency and easy adaptability I'd hoped to use Accel. But for this part of the sketch, I've decided to just use the the EasyDriver move the motor 'manually'.

Thanks everyone for the help you've given.

Arduino-x:
Unfortunately, I couldn't adapt them successfully to what I wanted to do.

For the sake of consistency and easy adaptability I'd hoped to use Accel.

RTFM a few more times. I find manuals start to make sense at about the 10th reading.

...R

Robin2:
RTFM a few more times. I find manuals start to make sense at about the 10th reading.

...R

..You can say that again Robin!!! XD