AccelStepper with Nema34 skips with stepper.run

I have a Mega 2560 connected to TB6600 connected to Nema 34 (2.8a). I have the TB6600 set at 2.8a, 6400 microsteps. When the sketch tries to go 6400 steps in the following, there are times when the motor bucks, skips. The result is the motor does not rotate completely. There is a wheel on the shaft of the motor, but is not very heavy.

 stepper.setAcceleration(300);
  stepper.setSpeed(200);
  static int toMove = 6400;
  homeFound = false;

  long position = stepper.currentPosition();
  Serial.println(position);

  sensorFound =  false;
  // Wait for the motor to reach its target position

  currentPosition = stepper.currentPosition();
  Serial.print(" Forward-1: "); Serial.print(toMove);
  Serial.print(" : "); Serial.println(currentPosition);
  stepper.moveTo(toMove + currentPosition);
  while (stepper.distanceToGo() != 0) {
    value_D0 = digitalRead(SENSOR5);// reads the digital input from the sensor2
    if (value_D0 == LOW)
    {
      homeFound = true;
      break;
    }
    stepper.run();
  }

The main purpose is to find out if the wheel has passed over a sensor during its rotation.

I have used various acceleration and speed values.

Any guidance how to stop the skipping and always come back to the starting point.

Please post the entire code.

Tells nothing useful. Schematics are requested.

A, I am not allowed to

B, what does the rest have to do with this loop?

I have reduced the code to this.

/*
*/

const char compile_date[] = __DATE__;
const char compile_time[] = __TIME__;

#include <AccelStepper.h>

#define motorInterfaceType 1
#define ENABLE 7
#define DIR 6
#define PUL 8

const int SENSOR5 = 11; // digital input

int lastPosition = 0;

// Define a stepper and the pins it will use
AccelStepper stepper = AccelStepper(motorInterfaceType, PUL, DIR);
#define stepsPerRevolution 6400

void setup()
{
  Serial.begin(115200);
  while (!Serial); //Wait for the serial port to come online
  Serial.println("\n\n\n\n\n\n\n\n\n");

  Serial.print("Compiled on: ");
  Serial.print(compile_date);
  Serial.print(" at ");
  Serial.println(compile_time);


  Serial.println("Starting... ");

  // Motor controller
  pinMode(PUL, OUTPUT);    // sets the digital pin 13 as output
  pinMode(DIR, OUTPUT);    // sets the digital pin 13 as output
  pinMode(ENABLE, OUTPUT);    // sets the digital pin 13 as output
  digitalWrite(ENABLE, LOW);

  // Set the maximum speed and acceleration:
  stepper.setMaxSpeed(5000);
  stepper.setAcceleration(1000);
  stepper.setSpeed(800);

  digitalWrite(ENABLE, HIGH);

  Serial.println("End init");
}

static bool homeFound = false;

void findHome()
{
  int count = 0;
  int loops = 0;
  int value_D0 = 0;
  long currentPosition = 0;
  bool sensorFound = false;

  Serial.print("Find Home ");

  digitalWrite(ENABLE, LOW);
  stepper.setAcceleration(300);
  stepper.setSpeed(200);
  static int toMove = 6400;
  homeFound = false;

  long position = stepper.currentPosition();
  Serial.println(position);

  sensorFound =  false;
  // Wait for the motor to reach its target position

  currentPosition = stepper.currentPosition();
  Serial.print(" Forward-1: "); Serial.print(toMove);
  Serial.print(" : "); Serial.println(currentPosition);
  stepper.moveTo(toMove + currentPosition);
  while (stepper.distanceToGo() != 0) {
    value_D0 = digitalRead(SENSOR5);// reads the digital input from the sensor2
    if (value_D0 == LOW)
    {
      homeFound = true;
      break;
    }
    stepper.run();
  }
  //  delay(1000);


  if (homeFound)
    Serial.println("stop");
  else
    Serial.println("\t NO SENSOR5");
}

/****************************************************************************/

void loop()
{
  if (Serial.available())
  {
    String line = Serial.readStringUntil('\n');

    int blower = line.substring(1).toInt();
    char command = line.charAt(0);

    if (command == 'h')
    {
      Serial.println("Find Home");
      findHome();
    }
  }
}

I am not able to provide a schematic as it all goes into a pcb that is proprietary. 12v 3a power supply to TB6000.

Here is a link to a video. Notice the end. Also notice the sound. https://youtu.be/_Nu_L05lknA

Bad for You. Is this a company work?

Try and reduce acceleration and max speed by a factor of 10. If that works, increase the numbers step by step.

Because "whole sketch" is standard. Do you bring just your car air filter to your mechanic when your car throws an error code?

Given you can not identify the problem or problems, what makes you belive you selected the random piece of code with "the problem or problems?"

Thanks for a helpful suggestion.

I reduced speed by half and make it move a full circle with 6400. Better 3 out of 4 tries. 4th try missed the mark by about 100 steps. Not acceptable.

Even slower missed more.

One thing appears to be true. The setSpeed does not set a constant speed. It might be what the speed is to start with (not sure), but it appears that setMaxSpeed is the real speed control. From what I can tell, the motor starts at setSpeed, increases by setAcceleration until it reaches setMaxSpeed and that is what continues.

If a stepper skips steps, either the electromechanical design or code is incorrect, or both.

You have not posted enough information on either of those topics to do anything but guess.

What is it that you need?

If this is for a commercial product or for a company, consider hiring or have the company provide an engineer to go over the details.

The Arduino forum is about learning, teaching and open source projects.