user70622: **I wanted to add an updated code after I have finished it to help others in the future if they are ever looking for it.
EDIT AT END OF QUESTION AFTER ANSWER AND CODE UPDATE.**
I currently have an Uno with a CNCBoard Hat.
I am running a program on my RPi and sending a value through serial to the Arduino. I am receiving what I believe is a string, converting it to an int using toInt() (I found this solution somewhere). I then want to write that value to the stepper I have connected.
I know the stepper works, because if I run a simple test program such as this one:
const int stepX = 2;
const int dirX  = 5;

const int stepY = 3;
const int dirY  = 6;

const int stepZ = 4;
const int dirZ  = 7;

const int enPin = 8;

void setup() {
  // Sets the two pins as Outputs
  pinMode(stepX, OUTPUT);
  pinMode(dirX, OUTPUT);
  pinMode(enPin, OUTPUT);

  digitalWrite(enPin, LOW);
  digitalWrite(dirX, HIGH);
  digitalWrite(dirY, LOW);
  digitalWrite(dirZ, HIGH);
}

void loop() {
  // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for (int x = 0; x < 200; x++) {
    digitalWrite(stepX,HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepX, LOW);
    delayMicroseconds(1000);
  }
   delay(1000); // One second delay
}

The stepper rotates as the program describes. Where 200 is the pulse count.
The program I am currently trying to use is:
// defines pins numbers
//Front left wheel
const int stepX = 2;
const int dirX  = 5;

//Front right wheel
const int stepY = 3;
const int dirY  = 6;

//Back left wheel
const int stepZ = 4;
const int dirZ  = 7;

//Back right wheel
const int stepA = 12;
const int dirA  = 13;

const int enPin = 8;

void setup() {
  Serial.begin(9600);

  // Sets the two pins as Outputs
  pinMode(stepX, OUTPUT);
  pinMode(dirX, OUTPUT);

  pinMode(stepY, OUTPUT);
  pinMode(dirY, OUTPUT);

  pinMode(stepZ, OUTPUT);
  pinMode(dirZ, OUTPUT);

  pinMode(enPin, OUTPUT);

  digitalWrite(enPin, LOW);
  digitalWrite(dirX, HIGH);
  digitalWrite(dirY, LOW);
  digitalWrite(dirZ, HIGH);
  digitalWrite(dirA, HIGH);
}

void loop() {
  String YpulseEncode;
  if (Serial.available() > 0) {          
    YpulseEncode = Serial.readStringUntil('\n');
    int yMove1 = YpulseEncode.toInt();
    //XpulseCalc = Serial.read();

    //Serial.print("YpulseCalc: ");
    Serial.println(yMove1 * (-1));
    //Serial.print("YpulseCalc +: ");
    //Serial.println(YpulseCalc
    //Serial.print("XpulseCalc: ");
    //Serial.println(XpulseCalc);
    delay(5000);
    Serial.end();
    if (yMove1 != 0) {
      // Enables the motor to move in a particular direction
      // Makes 200 pulses for making one full cycle rotation
      for (int x = 0; x < (yMove1); x++) {
        digitalWrite(stepX, HIGH);
        delayMicroseconds(1000);
        digitalWrite(stepX, LOW);
      }
    }
  }
}

The number I am receiving from the RPi is -267. So when the serial monitor shows it, it is 267. I want to send the number 267 pulses to the stepper motor.
EDIT - UPDATED CODE

AccelStepper yStepper(AccelStepper::DRIVER, 2, 5);

// defines pins numbers
//Front left wheel
const int stepX = 2;
const int dirX  = 5;

//Front right wheel
const int stepY = 3;
const int dirY  = 6;

//Back left wheel
const int stepZ = 4;
const int dirZ  = 7;

//Back right wheel
const int stepA = 12;
const int dirA  = 13;

const int enPin = 8;

void setup() {
  yStepper.setMaxSpeed(800);
  yStepper.setAcceleration(400);
  
  
  Serial.begin(9600);

  // Sets the pins as Outputs
  pinMode(stepX, OUTPUT);
  pinMode(dirX, OUTPUT);

  pinMode(stepY, OUTPUT);
  pinMode(dirY, OUTPUT);

  pinMode(stepZ, OUTPUT);
  pinMode(dirZ, OUTPUT);

  pinMode(enPin, OUTPUT);

  digitalWrite(enPin, LOW);
  digitalWrite(dirX, HIGH);
  digitalWrite(dirY, LOW);
  digitalWrite(dirZ, HIGH);
  digitalWrite(dirA, HIGH);
}

void loop() {

  String YpulseEncode;
  if (Serial.available() > 0) {          
    YpulseEncode = Serial.readStringUntil('\n');
    int yMove1 = YpulseEncode.toInt();
    
if (yMove1 < 0) {
    int yMoveNew = yMove1 * (-1);

    //XpulseCalc = Serial.read();
    //Serial.print("YpulseCalc: ");
    Serial.println(yMove1 * (-1));
    //Serial.print("YpulseCalc +: ");
    //Serial.println(YpulseCalc
    //Serial.print("XpulseCalc: ");
    //Serial.println(XpulseCalc);
    delay(2000);

      // Enables the motor to move in a particular direction
      // Makes 200 pulses for making one full cycle rotation
      //for (int x = 0; x < yMoveNew; x++) {
      yStepper.move(yMoveNew);
      yStepper.runToPosition();
        //digitalWrite(stepX, HIGH);
        delayMicroseconds(1000);
        //digitalWrite(stepX, LOW);
      //}
    }
  }
}


user42015: You have 2 problems, which are already mentioned in the comments:

You call Serial.end() after you received one line. That will stop the Serial hardware. Thus the code will only receive one line and then ignore any data from Serial. I don't think, that you want this. So just remove the Serial.end().

yMove1 is negative, while your for loop has the condition int x = 0; x < (yMove1); x++. But zero is always higher than a negative number. Thus the for does not run at all (the condition x < yMove1 is false, when yMove1 is negative). I don't quite understand, why you want to provide a negative number. You don't have any code for direction reversing. In the current state I would suggest just not sending a negative number. I cannot say more, because it is unclear, what exactly you want here.



Note:

The parentheses around yMove1 inside the for loop condition is not needed.
The if statement if (yMove1 != 0) is not needed. If yMove1 is zero, the for loop  will just not run, because it starts with zero.


70622: <p>**I wanted to add an updated code after I have finished it to help others in the future if they are ever looking for it.</p>
<p>EDIT AT END OF QUESTION AFTER ANSWER AND CODE UPDATE.**</p>
<p>I currently have an Uno with a CNCBoard Hat.</p>
<p>I am running a program on my RPi and sending a value through serial to the Arduino. I am receiving what I believe is a string, converting it to an <code>int</code> using <code>toInt()</code> (I found this solution somewhere). I then want to write that value to the stepper I have connected.</p>
<p>I know the stepper works, because if I run a simple test program such as this one:</p>
<pre class="lang-cpp prettyprint-override"><code>const int stepX = 2;
const int dirX  = 5;

const int stepY = 3;
const int dirY  = 6;

const int stepZ = 4;
const int dirZ  = 7;

const int enPin = 8;

void setup() {
  // Sets the two pins as Outputs
  pinMode(stepX, OUTPUT);
  pinMode(dirX, OUTPUT);
  pinMode(enPin, OUTPUT);

  digitalWrite(enPin, LOW);
  digitalWrite(dirX, HIGH);
  digitalWrite(dirY, LOW);
  digitalWrite(dirZ, HIGH);
}

void loop() {
  // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for (int x = 0; x &lt; 200; x++) {
    digitalWrite(stepX,HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepX, LOW);
    delayMicroseconds(1000);
  }
   delay(1000); // One second delay
}
</code></pre>
<p>The stepper rotates as the program describes. Where 200 is the pulse count.</p>
<p>The program I am currently trying to use is:</p>
<pre class="lang-cpp prettyprint-override"><code>// defines pins numbers
//Front left wheel
const int stepX = 2;
const int dirX  = 5;

//Front right wheel
const int stepY = 3;
const int dirY  = 6;

//Back left wheel
const int stepZ = 4;
const int dirZ  = 7;

//Back right wheel
const int stepA = 12;
const int dirA  = 13;

const int enPin = 8;

void setup() {
  Serial.begin(9600);

  // Sets the two pins as Outputs
  pinMode(stepX, OUTPUT);
  pinMode(dirX, OUTPUT);

  pinMode(stepY, OUTPUT);
  pinMode(dirY, OUTPUT);

  pinMode(stepZ, OUTPUT);
  pinMode(dirZ, OUTPUT);

  pinMode(enPin, OUTPUT);

  digitalWrite(enPin, LOW);
  digitalWrite(dirX, HIGH);
  digitalWrite(dirY, LOW);
  digitalWrite(dirZ, HIGH);
  digitalWrite(dirA, HIGH);
}

void loop() {
  String YpulseEncode;
  if (Serial.available() &gt; 0) {          
    YpulseEncode = Serial.readStringUntil('\n');
    int yMove1 = YpulseEncode.toInt();
    //XpulseCalc = Serial.read();

    //Serial.print(&quot;YpulseCalc: &quot;);
    Serial.println(yMove1 * (-1));
    //Serial.print(&quot;YpulseCalc +: &quot;);
    //Serial.println(YpulseCalc
    //Serial.print(&quot;XpulseCalc: &quot;);
    //Serial.println(XpulseCalc);
    delay(5000);
    Serial.end();
    if (yMove1 != 0) {
      // Enables the motor to move in a particular direction
      // Makes 200 pulses for making one full cycle rotation
      for (int x = 0; x &lt; (yMove1); x++) {
        digitalWrite(stepX, HIGH);
        delayMicroseconds(1000);
        digitalWrite(stepX, LOW);
      }
    }
  }
}
</code></pre>
<p>The number I am receiving from the RPi is -267. So when the serial monitor shows it, it is 267. I want to send the number 267 pulses to the stepper motor.</p>
<p><strong>EDIT - UPDATED CODE</strong></p>
<pre><code>
AccelStepper yStepper(AccelStepper::DRIVER, 2, 5);

// defines pins numbers
//Front left wheel
const int stepX = 2;
const int dirX  = 5;

//Front right wheel
const int stepY = 3;
const int dirY  = 6;

//Back left wheel
const int stepZ = 4;
const int dirZ  = 7;

//Back right wheel
const int stepA = 12;
const int dirA  = 13;

const int enPin = 8;

void setup() {
  yStepper.setMaxSpeed(800);
  yStepper.setAcceleration(400);
  
  
  Serial.begin(9600);

  // Sets the pins as Outputs
  pinMode(stepX, OUTPUT);
  pinMode(dirX, OUTPUT);

  pinMode(stepY, OUTPUT);
  pinMode(dirY, OUTPUT);

  pinMode(stepZ, OUTPUT);
  pinMode(dirZ, OUTPUT);

  pinMode(enPin, OUTPUT);

  digitalWrite(enPin, LOW);
  digitalWrite(dirX, HIGH);
  digitalWrite(dirY, LOW);
  digitalWrite(dirZ, HIGH);
  digitalWrite(dirA, HIGH);
}

void loop() {

  String YpulseEncode;
  if (Serial.available() &gt; 0) {          
    YpulseEncode = Serial.readStringUntil('\n');
    int yMove1 = YpulseEncode.toInt();
    
if (yMove1 &lt; 0) {
    int yMoveNew = yMove1 * (-1);

    //XpulseCalc = Serial.read();
    //Serial.print(&quot;YpulseCalc: &quot;);
    Serial.println(yMove1 * (-1));
    //Serial.print(&quot;YpulseCalc +: &quot;);
    //Serial.println(YpulseCalc
    //Serial.print(&quot;XpulseCalc: &quot;);
    //Serial.println(XpulseCalc);
    delay(2000);

      // Enables the motor to move in a particular direction
      // Makes 200 pulses for making one full cycle rotation
      //for (int x = 0; x &lt; yMoveNew; x++) {
      yStepper.move(yMoveNew);
      yStepper.runToPosition();
        //digitalWrite(stepX, HIGH);
        delayMicroseconds(1000);
        //digitalWrite(stepX, LOW);
      //}
    }
  }
}
</code></pre>
 42015: <p>You have 2 problems, which are already mentioned in the comments:</p>
<ul>
<li><p>You call <code>Serial.end()</code> after you received one line. That will stop the Serial hardware. Thus the code will only receive one line and then ignore any data from Serial. I don't think, that you want this. So just remove the <code>Serial.end()</code>.</p>
</li>
<li><p><code>yMove1</code> is negative, while your <code>for</code> loop has the condition <code>int x = 0; x &lt; (yMove1); x++</code>. But zero is always higher than a negative number. Thus the <code>for</code> does not run at all (the condition <code>x &lt; yMove1</code> is <code>false</code>, when <code>yMove1</code> is negative). I don't quite understand, why you want to provide a negative number. You don't have any code for direction reversing. In the current state I would suggest just not sending a negative number. I cannot say more, because it is unclear, what exactly you want here.</p>
</li>
</ul>
<hr />
<p>Note:</p>
<ul>
<li>The parentheses around <code>yMove1</code> inside the <code>for</code> loop condition is not needed.</li>
<li>The <code>if</code> statement <code>if (yMove1 != 0)</code> is not needed. If <code>yMove1</code> is zero, the <code>for</code> loop  will just not run, because it starts with zero.</li>
</ul>
