arduino stepper motor

hi Iam using accelstepper motor and easy driver v4 and serial port that when I send a position in absulot coordinate to the arduino it will move the the stepper motor according to this position that i send to the arduino my sketch work very good but I want to add new feature that when stepper motor complete the point that I send and stop the arduino will write message using ( Serial.println(stepper stop)) the question is how can I write this to the arduino and this is my code below

#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper(1, 9, 8);
void setup() {
  Serial.begin(115200);
   stepper.setMaxSpeed(100000);
  stepper.setAcceleration(10000);
}
String command;
void loop() {
if(Serial.available())
{
char c=Serial.read();
if (c=='

)
{
  parseCommand(command); 
  command = “”;
}
else
{
command += c;
}
}
stepper.run(); // run the position
}
void parseCommand(String com)


    String partx;
partx=com.substring(0,com.indexOf(“x”));// split the string and take only the (x) value
  stepper.moveTo(partx.toInt()); //make the stepper motor move to the (x) value that take from the string
if (stepper.distanceToGo() == 0)       
  {
  stepper.moveTo(stepper.currentPosition());
}
                 
  }

I am not familiar with the AccelStepper, library but it looks like when you do this   stepper.moveTo(partx.toInt()); //make the stepper motor move to the (x) value that take from the stringthe stepper moves unconditionally to the position indicated. If that is so then you could do the move in a series of small steps so that you know when you have commanded the stepper to go to its final target position.

There is an AccelStepper function called distanceToGo() which can be used to check when the motor has reached its destination.

It is always a good idea to read about all the functions that are available in a library.

...R

There is an AccelStepper function called distanceToGo() which can be used to check when the motor has reached its destination.

Which, ironically is already being used in the program.

From the way it is used I took it to mean that it returned the difference between the current and target positions [u]before[/u] the stepper.run() function was invoked. As I was not sure, and I'm still not sure, I did not mention it.

UKHeliBob:
Which, ironically is already being used in the program.

I had not spotted that - perhaps because the OP’s question implied that he was not using it :slight_smile:
One often only sees what one expects to see.

…R

From the way it is used I took it to mean that it returned the difference between the current and target positions before the stepper.run() function was invoked.

True. But, the run() method does NOT make the stepper run until it gets to the commanded position. It simply causes the library to determine if another step is needed now, and, if so, to make the stepper step once.

PaulS: True. But, the run() method does NOT make the stepper run until it gets to the commanded position. It simply causes the library to determine if another step is needed now, and, if so, to make the stepper step once.

Thanks Paul. It sounds like it is time for me to buy a stepper motor and experiment with it.