How to stop stepper motor through serial monitor command

Hello,
I’m looking for an idea of how to solve these problems.

  1. I want to stop the stepper motor when I command “s” but it doesn’t stop.
  2. In the code, I am including the steps and in the serial monitor, I command “f” for forward and “b” for backward but I want to give steps as an input in serial monitor with the backward and forward direction.
  3. Suppose motor is rotating (I give 4000 steps(say)) after some time I want to stop the motor before completing it’s 4000 steps through commanding on the serial monitor.

here is my code…

#include<Stepper.h>
const int stepsPerRevolution = 200; // change this to the number of steps on your motor
Stepper stepper(stepsPerRevolution, 9, 10, 11, 12); // input pin in the stepper motor (you change this pin also)
int stepCount = 0; // initial condition for stepper motor
void setup()
{
while (!Serial);
Serial.begin(9600); // for serial monitor & plotter(Data accumulation)
}
void loop()
{
stepper.setSpeed(30); // speed control of stepper motor
char n;
if (Serial.available() > 0)
{
int command = Serial.read();
{

if (command == ‘f’)
{
do
{
stepper.step(1000);
} while (Serial.available() == 0);
Serial.read();
}
else if (command == ‘b’)
{
stepper.step(-1000);
}
else if (command == ‘s’)
{
stepper.stop();
}
}
}

}

I modified the code. It is working now but the problems 2 and 3 remain the same. How can I solve this?
Here is the modified code.

#include<Stepper.h>
const int stepsPerRevolution = 200; // change this to the number of steps on your motor
Stepper stepper(stepsPerRevolution, 9, 10, 11, 12); // input pin in the stepper motor (you change this pin also)
int stepCount = 0; // initial condition for stepper motor
void setup()
{
while (!Serial);
Serial.begin(9600); // for serial monitor & plotter(Data accumulation)
}
void loop()
{
stepper.setSpeed(30); // speed control of stepper motor
char n;
if (Serial.available() > 0)
{
int command = Serial.read();
{

if (command == ‘f’)
{
do
{
stepper.step(1000);
} while (Serial.available() == 0);
Serial.read();
}
else if (command == ‘b’)
{
stepper.step(-1000);
}
else if (command == ‘s’)
{
Stop();
delay(1000);
}
}
}

}
void Stop()
{
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
}

amit2019:
I modified the code. It is working now but the problems 2 and 3 remain the same. How can I solve this?
Here is the modified code.

But 1 is meant to be related to 3, right? You mentioned that you want to stop the motor by entering 's'. And you mentioned that it's working now.

Maybe you can modify the code so that you have a variable that stores the desired number of counts .. eg .. N = 3000.

And then when you write code for a loop, you could use the 'step' function to move 1 step at a time. And your value of N will have a counter that counts from 1 to N .... stepping only 1 step for each count {eg. stepper.step(1);}. And have some code that keeps checking for incoming serial commands ..... like 's' for stop. And if an 's' is detected, then you could make your program exit the counting loop.

Eg. if N = 3000 (the desired number of counts), then have a loop with a counter called 'i', which starts to count 1, 2, 3, 4 etc ... up to N. But after each count.... include one of these commands --- stepper.step(1);

Also.... once the counter 'i' reaches 3000, the loop stops naturally.

But ...... if an 's' is detected through serial during the looping, then just set the counter 'i' to 3000, and your loop will stop. That's just one example. There are probably lots of ways to stop the loop.

Here I mean if I press "f" then the motor rotates forward direction and then press "s" and "b" then it holds 1-sec and rotates backward. But it stops after completing the full rotation (which I give in code 1000 steps). I want to stop it after certain rotation as I mention in problem 2 and 3.