Stepper motor issues

Hello, fresh newbie here with an issue that puzzles:

  1. I'm using a Nema 17 motor set to 6400 microsteps, TB6600 stepper Driver, Elegoo Nano clone.
    I want 5.4 revolutions of the motor after pushing a button, at 6400 microsteps, that's 34560 steps, and the motor runs continuously after I push the button. I kept reducing the number of steps and 32767 is the highest nember of steps it'll run and stop before the motor runs nonstop at 32768 and above. I switched to 3200 microsteps (17280 steps for 5.4 turns) and the motor stops as desired.
    I think I'm fine at 3200, getting the desired number of turns, but am curious what the issue is when trying 6400 microsteps. Is it the Nano, the Driver? Anybody? I've got a lot to learn.

Post your code using the <CODE> button and your wiring.

Sounds like the type definition is wrong.

/* Attempt to run a stepper motor with TB6600 stepper motor driver 
   and Arduino without a library: push button, run x revolutions */

// Define stepper motor connections, button, and steps per revolution:
#define buttonPin 7
#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 3200

int buttonState = 0;  // variable for reading the pushbutton status

void setup() {
  // Declare pins as output:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(buttonPin, INPUT);
 
}

void loop() {
// read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // run motor 5.4 revs:
 // Set the spinning direction clockwise:
  digitalWrite(dirPin, HIGH);

  // Spin the stepper motor 5.4  revolutions:
  for (int i=0; i < 17280; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(125);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(125);
  }


  } else {
    // turn motor off:
    digitalWrite(stepPin, LOW);
    digitalWrite(dirPin, LOW);
  }
 

}

  

Common cathode wiring on the Stepper driver, 5V from Nano to one leg of the button, other leg to pin 7 on the Nano with a 10K pulldown resistor, 24V power supply on the motor, Nano running on power from USB. Everything sharing ground.

Show you code in code brackets and draw a circuit diagram and include here showing all terminal numbers and connections and power supplies.

Just a stab in the dark here but does the number you quote sound familiar at all??

I think we were typing at the same time. I've written up the wiring which as I've said, works fine with the steps defined below the threshold.
It just seemed odd to me that any number below that threshold worked fine, but nothing above it, only changing the number of steps. The code I posted is the current iteration with steps per rev redefined as 3200 instead of 6400 and the number of steps to run, divided by two.

A simulation of your code stops on 17280, and multiples on button presses. I do not see a use of stepsperrevolution

As shown, it is working fine, but with the driver set to 6400, and defined steps per rev of 6400, 34560 runs non stop.

The code is a patchwork of different sketches found online, I'm sure it could be cleaner and more direct. I was just curious if there's something about extreme microstepping or "large" numbers that are causing the issue?

I tried the same numbers, 32767 / 32768 at the 3200 microstep setting, and the issue repeats. Runs and stops as designed at 32767, but runs without stopping at 32768.

Use unsigned int here:

for (unsigned int i=0; i < 34560; i++)

4000 steps per second with no acceleration?

Will look at this when I'm back in my shop tomorrow.

I reduced the delay after switching from 6400 to 3200, at 6400, I had delay of 250. No library, no acceleration or deceleration.

on 8 bit arduino-microcontrollers a signed integer-variable can hold values up to 32767

As soon as you want to use 32768 this no longer the value +32768 but a negative value
because the highest bit is used for the sign.

If you want to count up to numbers higher than 32767 you have to use either
unsigned int which can hold number up to 2^16 - 1 = 65.535

or variable type long which can hold values up to 2^31 - 1 = 2.1474.83.647

Thank you Stefan, I THOUGHT it might be something like this, and a friend I spoke with also confirmed it, and offered a slight code change that worked. I will study the other respondents replies and keep learning.