AccelStepper acceleration and deceleration help

Hello everyone, i need some help with AccelStepper library.
I would like to know how I can accelerate and decelerate the engine speed while it is running. For example, if I have 300 RPM and I want to reach 250, I will decelerate from the current speed and not stop and accelerate to 250 RPM. I tried to use the moveTo, setMaxSpeed functions, but I still don't realize it because it still stops and accelerates from 0 when I change its speed.

Here is the code:

#include <AccelStepper.h>

const int STEP = 3;
const int DIR = 6;

AccelStepper stepper(AccelStepper::DRIVER, STEP, DIR);

int actual_steps, old_steps;
int c = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

stepper.setAcceleration(100);

actual_steps = 1000;
old_steps = 0;
}

void loop() {

Serial_verify();
verify_steps();
stepper.run();

}

void verify_steps(){

if(actual_steps > old_steps){
stepper.setMaxSpeed(actual_steps);
stepper.moveTo(1000000);
}
else if(actual_steps < old_steps){
stepper.setMaxSpeed(actual_steps);
stepper.moveTo(-1000000);
}
}

void Serial_verify() {

if (Serial.available()) {
c = Serial.parseInt();
old_steps = actual_steps;
actual_steps = c;
Serial.println(actual_steps);
Serial.println(old_steps);
}
}

Thanks for help!!!
Paul

AccelStepper is position controlled not speed controlled.
For Speed control you need to use my modified Speed Stepper library
Stepper Speed Control Library project with plotting
Also see my tutorial on Multi-tasking in Arduino
which covers how to get the most speed out of your stepper and how to control it via user inputs and sensors

1 Like

Thanks , i will try. But there says library can be used just with mega and uno. It can be used with nano?

The project says
"This sketch is configured to work with Serial, e.g. UNO and Mega etc. For running on the SparkFun Redboard Turbo see below."
meaning is uses the Serial connection for debug and cmds.

Further down near the bottom it explains
When moving the SpeedStepperSetup sketch from the Uno/Mega to the Redboard Turbo you might naïvely just replace #define SERIAL Serial with #define SERIAL SerialUSB to suit the Redboard Turbo usb serial connection, however you would find that the resulting stepper latency is about 10mS. That is 10x slower then for the UNO. This due to how the Redboard cpu handles the USB connection.

So good for any board that has hardware Serial and does not use the main processor to handle the USB connection, i.e. most boards including Nano

1 Like