AccelStepper Deceleration problem

Hi All, I have a problem which I hope is going to be very simple to solve. My requirement is very simple - I wish to accelerate a stepper to a particular speed and run at constant speed until a button press is detected at which point the motor decelerates back to zero. I specify values for speed and acceleration and my code works fine taking the motor up to speed, but I cannot achieve deceleration back to zero. I am running on an Uno and to keep things simple will only post a snip of my code that deals with the stepper section.

  stepper.setMaxSpeed(steps_per_sec); // SPEED = Steps / second  
  stepper.setAcceleration(ramp_rate);  // ACCELERATION = Steps /second^2
  stepper.setMinPulseWidth(15);
  stepper.enableOutputs(); // enable pins
  stepper.moveTo(10000000); // run indefinitely
  while(true)
  {
    stepper.run();  // step the motor (this will step the motor by 1 step each loop)
    if(chk_pin()) {   // function that detects button press, very quick circa 1 uSec
      break;
    }
  } // while true
  stepper.stop();  // this does not work, nor does anything else I try
// code continues...

The documentation says stepper.stop() "causes the stepper to stop as quickly as possible, using the current speed and acceleration parameters", but the motor stops instantly without deceleration.

I have the Uno in a case with a 16 x 2 display and a button rotary encoder which enables me to set the input parameters, and I use it for testing steppers and drivers, and checking max speeds vs theoretical max speeds. I did see the post by Heredea on June 9th "AccelStepper acceleration and deceleration help" with an answer by DRMPF as Post #2 on June 10th. DRMPF suggested using the modified Speed Stepper library, but I really don't want to install yet another library if there is a simple solution to my problem. Thanks in advance.

Is there not a library method like setAcceleration() that sets the decceleration?

No, as far as I know there is only the one method setAcceleration() which sets both.

What library are you using? Link? The Arduino Standard Library version doesn't even have acceleration setting functions.

AccelStepper Library
Please bear with me, this is my first post.

stepper.stop() still needs the repeated call of stepper.run(). Otherwise no steps are generated and the motor stops imediately.
When you leave your while loop with break; run is not called anymore and no steps are generated to decelerate the motor.
stepper.run() can and should be called always. It does not create steps, if the motor is stopped.

Excellent, thank you, that makes sense. I will give that a try and report back.

Thank you MicroBahner, problem solved. I added this code outside the first run loop:

  stepper.stop();
  while(stepper.distanceToGo() > 0)
  {
    stepper.run();
  }

The above is working like a charm. Thanks again.

AccelStepper is a position based library in which you can set the max speed to travel at between given positions.

If you want a speed base control then look at my modified library that controls speed Stepper Speed Control Library project with plotting

You stopped calling run(), so AccelStepper stopped doing anything. Call run() repeatedly and frequently all the time.

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