Stepper motor with two inputs

Greetings for all,

First let me wish you a good health, for you and your families. I'm Dokleat, from Kosovo, I'm in the beginning of programming. I need your help, so I decided to address you through this post.
I have a Nema 17 stepper motor, and an Arduino Nano plate. I want to do a program that first accepts the commander's order to move in front or back, and then again receives from the user the number of rotations that the motor stepper should do.

I have created a script but I have a problem with its execution. Specifically, during the execution of the second order, Arduino does not accept the number of rotations provided by the user.

This is the source code of the program:

const int stepPin = 3;
const int dirPin = 4;
int x = 0;
long i = 0;
long p = 0;
char serialData = 0;
void setup()
{
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available() > 0)
  {
    serialData = Serial.read();
    Serial.print(serialData);
    
    if(serialData == '1')
      {   
        i = Serial.parseInt();
        digitalWrite(dirPin, HIGH);
        for(x = 0; x <=i*200; x++)
        {
          digitalWrite(stepPin, HIGH);
          delayMicroseconds(1500);
          digitalWrite(stepPin, LOW);
          delayMicroseconds(1500);
        }
      }else if(serialData == '0')
      {
        p = Serial.parseInt();
        digitalWrite(dirPin, LOW);
        for(x=0; x <= p*200; x++)
        {
          digitalWrite(stepPin, HIGH);
          delayMicroseconds(1500);
          digitalWrite(stepPin, LOW);
          delayMicroseconds(1500);  
        }
      }
  }

}

Can you help me, how to solve this problem?

Best regards,
Dokleat

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

Your user could send all the info in one message - for example <F, 123> meaning move 123 steps in the forward direction.

And if you want a more user-friendly style of program have a look at the the simple user input example in Planning and Implementing a Program

...R

The code looks OK. If you send '11' it should go 200 steps in one direction and if you send '01' it should go 200 steps in the other direction. What is it doing instead?

Healthy. To do this you need a motor controller. Just 4988. Then take the program down.

Healthy. The program is good. That driver needs to be done. I'm sending a link. https://www.banggood.com/CNC-Shield-Board-4Pcs-A4988-Stepper-Motor-Driver-For-Arduino-3D-Printer-p-1105262.html?rmmds=myorder&cur_warehouse=CN