AccelStepper - runSpeed with moveTo

Hi,

Had a search and can’t find a solution so I thought Id give the forum a go…

Im using the accelstepper library but I want a constant speed - I see that I need to use runSpeed() - however this appears to run indefinitely… is there a way to use moveTo at the same time?

Or is the only way to use something like …

if(stepper.currentPosition== desired position){
stop()}

My current code looks like this if it helps

String incoming = "";  // Data received from the serial port
String letter = "";  //resulting letter from the string
String valString = ""; //value before its converted to an int
int incomingVal = 0;    //incoming value before its assigned to another variable

int valA = 0; //variables for motorspeed, pos etc
int valB = 0;
int valC = 0;
int runFlag = 0;    //Run the stepper

int startTime;    //for calc speed

#include <AccelStepper.h>   //add library
AccelStepper stepperH(1, 2, 3);    //attach stepperH to pins 2+3

void setup() {
  Serial.begin(9600); // Start serial communication at 9600 bps
  Serial.setTimeout(5);   //reading the serial takes 3ms - if this isnt hear it will take 1000ms

  stepperH.setMaxSpeed(900);    //set values - CHANGE TO LOOP
  //stepperH.setAcceleration(999);
}

void loop() {
  // Check if
  if (Serial.available()) {
    startTime = millis();
    incoming = Serial.readString();
    valString = incoming;   //copy incoming string so we can delete the number and letter seperately

    incoming.remove(1);   // remove everything but the first character to get the letter
    letter = incoming;

    valString.remove(0, 1);   //remove the first character from the string to get the number
    incomingVal = valString.toInt();    //convert string to int

    if (letter == "A") {      //assign the value to the correct variable
      valA = incomingVal;
      stepperH.moveTo(valA);
    }
    else if (letter == "B") {
      valB = incomingVal;
      stepperH.setSpeed(valB);
    }
    else if (letter == "C") {
      valC = incomingVal;
    }
    else if (letter == "R") {   //if recieved R then run
      runFlag = 1;
    }
    else if (letter == "S") {   //if recieved S then stop
      runFlag = 0;
      Serial.println("Stopped");
    }
    else if (letter == "I") {   //if recieved S then stop
      Serial.print("Current position = ");
      Serial.println(stepperH.currentPosition());
    }
    else if (letter == "X") {   //reset the board
      Serial.println("Resetting Board");
      delay(500);                         //delay so that the print can finish
      void software_Reset();      {
        asm volatile ("  jmp 0");
      }
    }
    else {
      Serial.println("Incorrect Code!");    //if its not one of the recognised letters
    }   //Print out the values after one is changed
    Serial.print(valA); Serial.print("--"); Serial.print(valB); Serial.print("--"); Serial.println(valC);
  }

  if (runFlag == 1) {   //if run command was sent
    stepperH.runSpeed();   //run stepper
    //Serial.println(stepperH.currentPosition());   //print current position
    if (stepperH.currentPosition() == valA) {   //if the stepper has reached destination
      runFlag = 0;                              //turn off run flag
      Serial.println("Move complete");
    }
  }

}

Thanks for your time,

Andrew

What about runSpeedToPosition()

...R

Hi, thanks for your reply, i actually found this on a website just before i read your post, it seems to work perfectly :slight_smile:

Thanks again

lavericklavericklaverick:
found this on a website

Why not look at THE website where everything is documented? :slight_smile:

...R

lavericklavericklaverick:
Im using the accelstepper library but I want a constant speed - I see that I need to use runSpeed() - however this appears to run indefinitely.... is there a way to use moveTo at the same time?

Yes, runSpeed runs indefinitely, that's what its for.

If you want to use moveTo, use run, not runSpeed.