Cannot decelerate by lowering setMaxSpeed

45 views
Skip to first unread message

Michaël Bruneau

unread,
Jul 15, 2025, 3:21:03 PMJul 15
to accelstepper
I am trying to control the speed of a step motor using an acceleration and a Max Speed. 
If I increase the MaxSpeed, the motor accelerates smoothly, but if I lower the MaxSpeed, it does not decelerate, reaching right away the new lower speed.

If I Stop instead, the deceleration works normally to zero. Is there a way to decelerate without stopping?

Here is my test code:

#include <AccelStepper.h>
#include <elapsedMillis.h>

// Motor Connections (constant current, step/direction bipolar motor driver)
const int dirPin = 4;
const int stepPin = 3;

// Creates an instance
AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin);           // works for a4988 (Bipolar, constant current, step/direction driver)

elapsedMillis printTime;

void setup() {
  Serial.begin(115200);
  // set the maximum speed, acceleration factor, and the target position.
  myStepper.setMaxSpeed(0.0);   // the motor accelerates to this speed exactly without overshoot.
  myStepper.setAcceleration(4000.0);  
  myStepper.moveTo(48000);

}
int count = 0;    // tracks seconds to trigger an action if desired.
void loop() {
  float mSpeed;
  if (printTime >= 1000) {    
    printTime = 0;  

    switch (count++){
      case 1:
        myStepper.setMaxSpeed(4000.0);
        break;
      case 3:
        myStepper.setMaxSpeed(0.0);
        break;
      case 5:
        myStepper.setMaxSpeed(1500.0);
        break;
      case 7:
        myStepper.setMaxSpeed(4000.0);
        break;
      case 9:
        myStepper.stop();
        break;
    }
  }

Thanks in advance,
Michael

gregor christandl

unread,
Jul 15, 2025, 4:27:49 PMJul 15
to accelstepper
Not in vanilla AccelStepper. But this is a common question, forks which support this should exist. 

--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/accelstepper/81f1bb38-ed47-4c4f-9c81-fd1020491241n%40googlegroups.com.
Message has been deleted
Message has been deleted

Jim Larson

unread,
Jul 29, 2025, 4:40:07 PMJul 29
to accelstepper
Here is one way to do the requested action. Please note that I have provided code that uses speeds for my motor/driver combination. Also, the use of HardwareSerial won't be required for most Arduino or Arduino-like processors.

#include <AccelStepper.h>
#include <elapsedMillis.h>

// Motor Connections (constant current, step/direction bipolar motor driver)
const int dirPin = 16;
const int stepPin = 14;
AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin); // works for a4988 (Bipolar, constant current, step/direction driver)
HardwareSerial Serial(23, 24); // only needed for G431 // for G431
elapsedMillis printTime;

void setup() {
Serial.begin(115200);
myStepper.setMaxSpeed(0.0); // the motor accelerates to this speed exactly without overshoot.
myStepper.setAcceleration(400.0);
myStepper.moveTo(48000);
}
int count = 0; // tracks seconds to trigger an action if desired.
float targetSpeed = 0.0; // target speed to slow to.
bool slowing = false; // set this to do a slow down. Set targetSpeed first.
void loop() {
float mSpeed;
if (printTime >= 1000) {
printTime = 0;

switch (count++) {
case 1:
myStepper.setMaxSpeed(400.0);
break;
case 3:
targetSpeed = 100.0;
slowing = true;
myStepper.stop();
break;
case 5:
myStepper.setMaxSpeed(200.0);
break;
case 7:
myStepper.setMaxSpeed(400.0);
break;
case 9:
myStepper.stop();
break;
}
}
myStepper.run();
if (slowing) {
if (myStepper.speed() <= targetSpeed) {
myStepper.moveTo(48000);
myStepper.setMaxSpeed(targetSpeed);
slowing = false;
}
}
}

HTH
           -jim
Reply all
Reply to author
Forward
Message has been deleted
0 new messages