Accelerating my Nema 17 motor

Hi,

In my current program, I control the movement of my motor using the following code:

if (millis() %2 <1)
        digitalWrite(X_STEP_PIN, HIGH);
      else
        digitalWrite(X_STEP_PIN, LOW);

I control my motor via pyserial with a Python program. Values below 1ms (e.g. if (millis() %2 <1)) don't work. When I use the given code without controling it via Python, values below 1 ms do work and thus a higher rpm results. Why is this the case? Is there a way to accelerate my motor or is it because I control it via pyserial with a Python program?

Thank you very much in advance!

millis() % 2 can only yield 0 or 1 as a value so every millisecond you are toggling your X_STEP_PIN.

If you want finer resolution, you need to use micros().

You can also just use the AccelStepper library to do all the work for you.