user35534: I'm starting to learn the AccelStepper library for controlling stepper motors for a project I'm working on. I'm able to get the stepper motors running with the basic examples that use acceleration.
I know for parts or all of project I'll want to use constant speed, so I popped up the constant speed demo: https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library/blob/master/examples/Accel_ConstantSpeed/Accel_ConstantSpeed.ino
#include <Wire.h>
#include <AccelStepper.h>
#include <Adafruit_MotorShield.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(200, 2);

void forwardstep1() {  
  myStepper1->onestep(FORWARD, SINGLE);
}
void backwardstep1() {  
  myStepper1->onestep(BACKWARD, SINGLE);
}

AccelStepper Astepper1(forwardstep1, backwardstep1); 

void setup()
{  
   Serial.begin(9600);
   Serial.println("Stepper test!");
  
  AFMS.begin();  // create with the default frequency 1.6KHz

  Astepper1.setSpeed(50);   
}

void loop()
{  
   Astepper1.runSpeed();
}

(I'm using the adafruit stepper motor wing)
I uploaded it to my arduino and it's working, the shaft is rotating, but it's doing it really slow. I figured this made sense because it's set at a speed of 50, so I tried turning up the speed:
Astepper1.setSpeed(200);

But the speed still seems to be the same speed. Considering this is a 200 step motor and setSpeed is steps per second you'd think this would whip the motor around fairly quick.
I'm positive that this is due to my misunderstanding of how the library works, but I'm having a hard time trying to figure out how to properly use the constant speed methods.

user35534: So I ended up doing some source code diving for the AccelStepper library to figure out what the problem was and it turns out there was a missing method call in their example code, specifically a call to setMaxSpeed in the setup function:
void setup()
{
    Serial.begin(9600); // set up Serial library at 9600 bps
    Serial.println("Stepper test!");

    AFMS.begin(); // create with the default frequency 1.6KHz

    // ! Note that you need to set the max speed, otherwise the stepper motor will
    // ! only step once per interval regardless of what `setSpeed` value you give
    Astepper1.setMaxSpeed(1000);

    Astepper1.moveTo(targetDistance);
    Astepper1.setSpeed(targetSpeed);
}

If you look at the setSpeed method in the library you'll notice a constraint of the speed value:

The constrain method takes in a speed value and then a low and high, which the accelstepper library sets to the private _maxSpeed variable. If the speed is outside of that range it will set the value to the low or high value passed in.

This means, if you don't call setMaxSpeed the library will use the default _maxSpeed value assigned during construction which issss 1.0:

So, to correctly use the constant speed methods, you need to be sure to include a call to setMaxSpeed in your code before calling setSpeed, likely in the setup function.
I also shot a video explanation to share with my co-workers. You can see it here: https://vimeo.com/438272826

35534: <p>I'm starting to learn the AccelStepper library for controlling stepper motors for a project I'm working on. I'm able to get the stepper motors running with the basic examples that use acceleration.</p>
<p>I know for parts or all of project I'll want to use constant speed, so I popped up the constant speed demo: <a href="https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library/blob/master/examples/Accel_ConstantSpeed/Accel_ConstantSpeed.ino" rel="nofollow noreferrer">https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library/blob/master/examples/Accel_ConstantSpeed/Accel_ConstantSpeed.ino</a></p>
<pre><code>#include &lt;Wire.h&gt;
#include &lt;AccelStepper.h&gt;
#include &lt;Adafruit_MotorShield.h&gt;

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(200, 2);

void forwardstep1() {  
  myStepper1-&gt;onestep(FORWARD, SINGLE);
}
void backwardstep1() {  
  myStepper1-&gt;onestep(BACKWARD, SINGLE);
}

AccelStepper Astepper1(forwardstep1, backwardstep1); 

void setup()
{  
   Serial.begin(9600);
   Serial.println(&quot;Stepper test!&quot;);
  
  AFMS.begin();  // create with the default frequency 1.6KHz

  Astepper1.setSpeed(50);   
}

void loop()
{  
   Astepper1.runSpeed();
}
</code></pre>
<p>(I'm using the adafruit stepper motor wing)</p>
<p>I uploaded it to my arduino and it's working, the shaft is rotating, but it's doing it really slow. I figured this made sense because it's set at a speed of 50, so I tried turning up the speed:</p>
<pre><code>Astepper1.setSpeed(200);
</code></pre>
<p>But the speed still seems to be the same speed. Considering this is a 200 step motor and <code>setSpeed</code> is steps per second you'd think this would whip the motor around fairly quick.</p>
<p>I'm positive that this is due to my misunderstanding of how the library works, but I'm having a hard time trying to figure out how to properly use the constant speed methods.</p>
 35534: <p>So I ended up doing some source code diving for the AccelStepper library to figure out what the problem was and it turns out there was a missing method call in their example code, specifically a call to <code>setMaxSpeed</code> in the setup function:</p>
<pre><code>void setup()
{
    Serial.begin(9600); // set up Serial library at 9600 bps
    Serial.println(&quot;Stepper test!&quot;);

    AFMS.begin(); // create with the default frequency 1.6KHz

    // ! Note that you need to set the max speed, otherwise the stepper motor will
    // ! only step once per interval regardless of what `setSpeed` value you give
    Astepper1.setMaxSpeed(1000);

    Astepper1.moveTo(targetDistance);
    Astepper1.setSpeed(targetSpeed);
}
</code></pre>
<p>If you look at the <code>setSpeed</code> method in the library you'll notice a constraint of the speed value:</p>
<p><a href="https://i.stack.imgur.com/2aLkC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2aLkC.png" alt="constraining the speed" /></a></p>
<p>The <code>constrain</code> method takes in a speed value and then a low and high, which the accelstepper library sets to the private <code>_maxSpeed</code> variable. If the speed is outside of that range it will set the value to the low or high value passed in.</p>
<p><a href="https://i.stack.imgur.com/8MNoN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8MNoN.png" alt="constrain code" /></a></p>
<p>This means, if you don't call <code>setMaxSpeed</code> the library will use the default <code>_maxSpeed</code> value assigned during construction which issss <code>1.0</code>:</p>
<p><a href="https://i.stack.imgur.com/tPfHa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tPfHa.png" alt="default max speed" /></a></p>
<p>So, to correctly use the constant speed methods, you need to be sure to include a call to <code>setMaxSpeed</code> in your code before calling <code>setSpeed</code>, likely in the <code>setup</code> function.</p>
<p>I also shot a video explanation to share with my co-workers. You can see it here: <a href="https://vimeo.com/438272826" rel="nofollow noreferrer">https://vimeo.com/438272826</a></p>
