AccelStepper smooth faster/slower speed while running


I managed to modify the speed every ~x sec while running with this code :

#include <AccelStepper.h>

#define MotorInterfaceType 4

AccelStepper stepper = AccelStepper(MotorInterfaceType, 0, 2, 1, 3);

unsigned long previousMillis = 0;
int interval = 1200;

void setup()

void loop()

  unsigned long currentMillis = millis();
  (currentMillis - previousMillis >= interval) {
  previousMillis = currentMillis;

  interval = random(2000,5050);


But It is not fluid as I would like it to be.

If it does have to go faster, the acceleration is working. But if it does have to slow, It slows down roughly.

Any idea how I could get the speed smoothly slow down ?

how can you tell if you are randomly setting the speed and the time between changes in speed?

I think this is because max speed is not supposed to change while a motor is moving.

Maybe use setSpeed() to change speed, and runSpeed() rather than run()

Didnt work out, maybe someone could help me with a fading system or something ? smooth increment instead of changing the speed directly

It is not very difficult to write your own acceleration code without needing the library. Simple acceleration code.