Using AccelStepper to accelerate to a constant speed

I want to make my stepper motor gradually speed up to a particular rate and then continue spinning at that speed. I thought the AccelStepper library might allow me to do this, with something like:

void setup()
{  
  stepper.setMaxSpeed (200);
  stepper.setAcceleration(10);
}

void loop()
{
  stepper.run();
}

But looking at the library more carefully, it appears that it only allows the motor to accelerate as it moves to a target position, not when it moves up to a constant speed.

If I try to use "setSpeed" and "runSpeed", the load on my motor has too much inertia. I have to spin it by hand to get it moving.

Is there anything in the AccelStepper library that will help me out, or do I need to write my own routine to achieve this acceleration?

Okay, I don’t see anything in the AccelStepper library to do this directly, so this is my workaround. If someone knows a better way or a library call that does this directly, post up.

Thanks.

// Accelerates up to MaxSpeed. Does this by setting a low initial speed,
// then increasing the speed a small amount every "interval" number of
// milliseconds.

#include <AccelStepper.h>

AccelStepper stepper; // Defaults to 4 pins on 2, 3, 4, 5

float maxSpeed = 250;  // # of steps per second to speeed up to
unsigned long time;
unsigned long previousAccel = 0;
int interval = 100;  // # of milliseconds between speed increases

void setup()
{
  stepper.setMaxSpeed(maxSpeed);
  stepper.setSpeed(10);
}

void loop()
{
  while (stepper.speed() < maxSpeed) {
    time = millis();
    if (time > previousAccel + interval) {
      previousAccel = time;
      stepper.setSpeed(stepper.speed() + 2);
    }
    stepper.runSpeed();
  }
  stepper.setSpeed(maxSpeed);
  stepper.runSpeed();
}

I was just searching for the same thing, thanks!

Just use motor.moveTo(motor.currentPosition()+1000000); ?

1 Like

I am doing the same thing, 2 motors accelerating to speed and then running indefinitely. When I ran with one motor I was getting about 20rpm.Adding the second motor slowed it WAY down. Am I hitting the wall for processing speed?

#include <AccelStepper.h>

AccelStepper stepper1(AccelStepper::FULL2WIRE, 52, 50);
AccelStepper stepper2(AccelStepper::FULL2WIRE, 27, 26);

void setup()
{
stepper1.setMaxSpeed(2200);
stepper1.setAcceleration(500.0);
stepper1.setSpeed(2000);

stepper2.setMaxSpeed(2200.0);
stepper2.setAcceleration(500.0);
stepper2.setSpeed(50);
}

void loop()
{

stepper1.runSpeed();
stepper2.runSpeed();

}