stepper.runSpeedToPosition() is moving at 1/10th the speed I set when running inside a while loop - AccelStepper library

I am using the AccelStepper library and 1 single stepper motor. I home the stepper using two limit switches and the following code

long getEndpoints() {
  stepper1.setSpeed(-2500);
  while (!digitalRead(startSwitch)) {
    stepper1.runSpeed();
  }
  stepper1.setCurrentPosition(0);
  long startPoint = stepper1.currentPosition();
  
  Serial.println(startPoint);
  stepper1.setSpeed(2500);
  while (!digitalRead(endSwitch)) {
    stepper1.runSpeed();
  }

  long endPoint = stepper1.currentPosition();
  Serial.println(endPoint);

  return startPoint, endPoint;
}

The steppers move and the selected speed during this.
But when I go to move the stepper to a select position it runs very very slowly. it seems to be defaulting to the speed set in the moveTo function, unless I take it out of a while loop and just run it in the void loop function

the following does not working

void loop() {
  stepper1.moveTo(250);
  while(stepper1.distanceToGo() != 0) {
    stepper1.setSpeed(1000);
    stepper1.runSpeedToPosition();
  }
}

but this does

void loop() {
  stepper1.moveTo(250);
  stepper1.setSpeed(1000);
  stepper1.runSpeedToPosition();
}

Sorry for the drawn-out explanation. If anyone can help here that would be awesome! The documentation for the library is hard to read.

edit: horrible gramar

Here is the entire project as it exists right now incase anyone is curious.

#include <AccelStepper.h>
#include <elapsedMillis.h>


#define directionPin 2
#define stepPin 4
#define startSwitch 13
#define endSwitch 12

elapsedMillis printTime;
AccelStepper stepper1(AccelStepper::DRIVER, stepPin, directionPin);

long sliderStart;
long sliderEnd;

void Home() {
  sliderStart, sliderEnd = getEndpoints();
}

long getEndpoints() {
  stepper1.setSpeed(-2500);
  while (!digitalRead(startSwitch)) {
    stepper1.runSpeed();
  }
  stepper1.setCurrentPosition(0);
  long startPoint = stepper1.currentPosition();
  
  Serial.println(startPoint);
  stepper1.setSpeed(2500);
  while (!digitalRead(endSwitch)) {
    stepper1.runSpeed();
  }

  long endPoint = stepper1.currentPosition();
  Serial.println(endPoint);

  return startPoint, endPoint;
}

void stopProcess() {
  while (true) {}
}

void setup() {
  Serial.begin(9600);
  stepper1.setMaxSpeed(10000);
  stepper1.setSpeed(-1000);

  pinMode(startSwitch, INPUT);
  pinMode(endSwitch, INPUT);

  pinMode(3, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(3), stopProcess, CHANGE);

  Home();
  Serial.println(sliderStart);
  Serial.println(sliderEnd);
}

void loop() {
  stepper1.moveTo(250);
  stepper1.setSpeed(1000);
  stepper1.runSpeedToPosition();
}

Have You inspected any of the example codes for AccelStepper?
Lots of members use the library functions the wrong way. There's surely a manual/document explaining how the different functions work.

Yeah I have been over it a few times.

I did find a solution.

 location = sliderStart + 250;
  stepper1.moveTo(location);
  while(stepper1.distanceToGo() != 0) {
    stepper1.moveTo(location);
    stepper1.setSpeed(motorSpeed);
    stepper1.runSpeedToPosition();  
  }

putting the moveTo function inside of the while loop seems to fix it. I read over the examples and documentation. I'm not 100% sure why it works but I won't question it.

You cannot jump from 0 to 2500 steps per second (750 RPM for a 200 step motor) instantly.

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