Can't run Stepper motor using input from serial monitor

I'm using this code to get the number of steps from serial monitor to run the nema17. but there's no rotation at all.I'm able to run the motor by manuall entering step in the code.

#include <AccelStepper.h>

const int stepsPerRevolution = 200; // change this to match your motor
AccelStepper stepper(1, 33, 32);

void setup() {
  Serial.begin(9600);
  stepper.setCurrentPosition(0);
  stepper.setMaxSpeed(2000.0);
  stepper.setAcceleration(500.0);
  stepper.setSpeed(500);
  
}

void loop() {
  while (Serial.available()==0){
    }
  int steps = Serial.parseInt(); 
  stepper.moveTo(steps);
  stepper.run();
}

what am I not doing right?

  1. If your intention is to execute these instructions inside the while, they must be inside the brackets:
while (Serial.available()==0){
  int steps = Serial.parseInt(); 
  stepper.moveTo(steps);
  stepper.run();
}
  1. What do you think the expression below does?

checks if there's any input in serial monitor, I know the condition is wrong but after correction it still doesn't work:

#include <AccelStepper.h>

const int stepsPerRevolution = 200; // change this to match your motor
AccelStepper stepper(1, 33, 32);

void setup() {
  Serial.begin(9600);
  stepper.setCurrentPosition(0);
  stepper.setMaxSpeed(2000.0);
  stepper.setAcceleration(500.0);
  stepper.setSpeed(500);
  
}

void loop() {
  if (Serial.available() > 0) {
  int steps = Serial.parseInt();
  
  stepper.moveTo(steps);
  stepper.run();
  
}

}


Fix the while (Serial.available() == 0) pointed out by @Brazilino and

also turn off line endings in serial monitor.

image

Remember that the moveTo function is an absolute position. If you moveTo(200) and do another moveTo(200) nothing will happen cause you are already there. The line endings are just sending it 0 after every number that you enter.

You are only calling the run() method when you have serial input. You need to call the run() method every time loop() is called.

This works on my set up with line endings in serial monitor set to "No line ending".

#include <AccelStepper.h>

const int stepsPerRevolution = 200; // change this to match your motor
AccelStepper stepper(1, 2, 5); // my setup uses a CNC shield so pins different

void setup()
{
   Serial.begin(9600);
   stepper.setCurrentPosition(0);
   stepper.setMaxSpeed(2000.0);
   stepper.setAcceleration(500.0);
   stepper.setSpeed(500);

}

void loop()
{
   while (Serial.available() > 0)
   {
      int steps = Serial.parseInt();
      Serial.print("entered steps = ");
      Serial.println(steps);
      stepper.moveTo(steps);
   }
   stepper.run();
}

Thank you soo much.