# AccelStepper questions

Just got to messing around with steppers, and have something I don’t quite understand. In using the AccelStepper library, I dont understand the need to specify/call “setMaxSpeed” and pass a variable is I am also going to have to call the setSpeed function" and pass a variable there. If the speed is constant (which is what the setSpeed and runSpeed functions are for, as I understand it), why do I have to specify the MAX speed?

This is shown in the AccelStepper example aptly named “ConstantSpeed”, as shown below. Removing the call to setMaxSpeed causes the stepper to not step properly.

``````// ConstantSpeed.pde
// -*- mode: C++ -*-
//
// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
/// \author  Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2009 Mike McCauley
// \$Id: ConstantSpeed.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem \$

#include <AccelStepper.h>

AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()
{
stepper.setMaxSpeed(5000);
stepper.setSpeed(1000);
}

void loop()
{
stepper.runSpeed();
}
``````

/// Sets the maximum permitted speed. the run() function will accelerate
/// up to the speed set by this function.
/// \param[in] speed The desired maximum speed in steps per second. Must
/// be > 0. Speeds of more than 1000 steps per second are unreliable.
void setMaxSpeed(float speed);

Sorry if its that blatantly obvious to others, but I really dont understand still. What is the point of having to call both?

Moreover, as your snippet states, the run() function will accelerate up to the speed specified in setMaxSpeed(), BUT in the example below, we're using the runSpeed() function, which runs at the speed specified in setSpeed(). If I'm not mistaken, the difference here is whether or not to specify acceleration.

lynestelles: Just got to messing around with steppers, and have something I don't quite understand. In using the AccelStepper library, I dont understand the need to specify/call "setMaxSpeed" and pass a variable is I am also going to have to call the setSpeed function" and pass a variable there. If the speed is constant (which is what the setSpeed and runSpeed functions are for, as I understand it), why do I have to specify the MAX speed?

The speed is not constant, this is AccelStepper(!). The speed is ramped up at the acceleration rate till the minimum of abs(speed) and maxspeed is reached. The maxspeed is a hard limit that always applies to prevent miss-stepping (found by trial and error) - ie if you set the speed higher than maxspeed, maxspeed is the value used.

The speed set by setSpeed is the current speed you happen to want to run at now, which might change frequently and be handled by code that doesn't know the mechnical limits of the hardware (and its a signed value, it can be negative) - the hard limit maxSpeed is designed to be set & forget and is strictly positive.

Perhaps the library compares the value used with setSpeed() with the value for maxSpeed() ?

...R

If I’m not mistaken, the difference here is whether or not to specify acceleration.

Correct.