Accelstepper decelerate to speed

Hello Everyone.

I want to accelerate and decelerate a stepper motor between 2 different speeds using a button.
The acceleration works like a charm however it does not decelerate but instantly takes the lower speed if selected.

What am I missing?
Greetings from the Netherlands
Bart

#include <AccelStepper.h>
#define knop 2

AccelStepper stepper(1,6,7);

void setup()
{  
  stepper.setMaxSpeed(150);
  stepper.setAcceleration(20);
  stepper.moveTo(500);
  pinMode(knop,INPUT_PULLUP);
}

void loop()
{
  int bState=digitalRead(knop);
   if (bState == 0){  
    stepper.setMaxSpeed(150.0);
    stepper.move(500);
  }
   if (bState == 1){  
    stepper.setMaxSpeed(30.0);
    stepper.move(500);
  }
    stepper.run();
}

Hi Delta_G.

If I do that it also does not accelerate but instantly changes.

Your challenge is that AccelStepper is position controlled - it will decide when to accelerate or decelerate based on the current position and target position. It's not really speed controlled

there are alternative libraries like

Mobatools which might be an option.

you could also have a look at a less used / referenced library ➜ Stepper Speed Control Library for Arduino

see the documentation and make note of

This SpeedStepper library is a rewrite of the AccelStepper library to allow speed control of the stepper motor. The SpeedStepper library lets you change the set motor speed and then accelerates/decelerates to the new set speed using the same algorithm as AccelStepper library. The SpeedStepper library also allow you to set plus and minus limit and a 'home' position. There is a goHome command to return to the home position. You can also set up a 'speed profile' for the stepper to follow and then call startProfile() to execute it.

Limitations: The SpeedStepper library only drives direction and step outputs and so it needs to be connected to a motor driver, such as Easy Driver, to actually drive the stepper motor. The AccelStepper library provides more driving options which could be copied to this library if required.

Simple example with MobaTools:

Thank you, Mobatools does exactly what I need!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.