Serial Input Control for Stepper Motors

Hello,
I have this code that successfully steps the motor but won’t step the motor quickly enough, and won’t step the motor far enough. The distance problem is whenever I input a value, if I input 5 there should be 5 steps taken, but instead it is 1. Any insight would be appreciated.
Thanks

#include "AccelStepper.h"
AccelStepper stepperX(8, 10, 9, 11);
// Define pin for mechanical switch input
#define home_switch 5 //this is pin five
//Travel/Target Values
long Xtarget;
long initial_homing = -1;
long Xcurrent = 0;


void setup() {
  Serial.begin(9600); //baud rate is adjustable later on for the response of the system
  pinMode(home_switch, INPUT_PULLUP);
  delay(5);
  stepperX.setMaxSpeed(1000);
  stepperX.setAcceleration(1000);
  //homing sequence
  while (digitalRead(home_switch)) {
    stepperX.moveTo(initial_homing);
    initial_homing--;
    stepperX.run();
  }
  stepperX.setCurrentPosition(0);
  stepperX.setMaxSpeed(1000);
  stepperX.setAcceleration(1000);
  initial_homing = 1;
  Xcurrent = stepperX.currentPosition();
  Xtarget = 100;
}
// put your setup code here, to run once:



void loop() {

  if (Serial.available() > 0)  { // Check if values are available in the Serial Buffer
    long new_Xtarget = (long)Serial.read();

    if (new_Xtarget < 0 || new_Xtarget > 1350) {  // Make sure the position entered is not beyond the HOME or MAX position
      Serial.println("");
      Serial.println("Please enter a value greater than zero and smaller or equal to 1350.....");
      Serial.println("");
    }
    else {
      Xtarget = Xtarget + 10;
    }
  }
  Xcurrent = stepperX.currentPosition();
  if (Xcurrent != Xtarget) {
    stepperX.moveTo(Xtarget);
    stepperX.run();
  }
  char buf[128];
  sprintf(buf, "T: %1d\tC: %1d\n", Xtarget, Xcurrent);
  Serial.print(buf);
}

This is not doing what you think

long new_Xtarget = (long)Serial.read();

If you enter the value 3456 at the serial monitor all that Serial.read() will see is the first character '3' and that has the ASCII value of 51 (decimal). Serial.read() only takes a single byte from the Serial Input Buffer.

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

...R

Hi,
Thanks for the help. Didn't think that (long)Serial.read(); wouldn't work