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