Having trouble changing speed with AccelStepper.h

Hi all, I've been attempting to use a stepper motor in a project. I am trying to understand how to change the speed at which the motor can move. I wrote a simple test function to try and change the speed of the motor, the code is below.

#include <AccelStepper.h>

// defines pins numbers

const int stepPin1 = 32;

const int directionPin1 = 34;

const int enablePin1 = 36;

const int stepPin2 = 22;

const int directionPin2 = 24;

const int enablePin2 = 26;
void setup()
{
  Serial.begin(19200);

    
  stepper2.setEnablePin(enablePin2);
  stepper2.setPinsInverted(false, false, true);
  stepper2.enableOutputs();
  stepper2.setMaxSpeed(18000.0);
  testing();

}

void testing(){
  stepper2.setAcceleration(10000);

  stepper2.setSpeed(1000);
  stepper2.moveTo(-5000);
  stepper2.runToPosition();

  delay(1000);

  stepper2.setSpeed(10000);
  stepper2.moveTo(0);
  stepper2.runToPosition();

}

The motor does run when executing this code, however no matter what I change the values of the setSpeed function to the motor always runs at the same speed.

18000 steps per second? On which Arduino? Read over this:

https://hackaday.io/project/183279-accelstepper-the-missing-manual/details

I am using an Arduino Mega. What is a reasonable max speed? I chose a number that was way above what I thought would ever be reached. I am experiencing troubles with speeds above 1500 steps per second. I have adjusted the testing function to look like this:

void testing(){
  stepper2.setAcceleration(10000);

  long target = -6050;
  stepper2.setSpeed(-1000);
  while(stepper2.currentPosition() > target){
    stepper2.runSpeed();
  }
  


  delay(1000);

  target = 3000;
  stepper2.setSpeed(1250);
  while(stepper2.currentPosition() < target){
    stepper2.runSpeed();
  }

}

When using acceleration ( .run() or .runToPosition() ) max Speed is the speed Accelstepper tries to accelerate to. You must not use setSpeed() when using acceleration. setSpeed() is used internally to change the speed during acceleration and deceleration. So setMaxSpeed() should be a reasonable speed that can be reached. On a Mega this cannot be more than about 5000 steps per second. If the sketch has to do other things as well while the motor is moving it may be much less.
Only if you don't use acceleration (runSpeed() or runSpeedToPosition() ) you must set the speed with setSpeed().

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