Entering a desired motor position in a switch case

Hi All ,
I have a stepper motor program here . I would like to know in case 2 of my program , how do I enter the position for the motor to move to .
I would like the motor to move to for example 1500 position.
Presently I am not able to enter any value other than 1 and 2 , which is the switch case .
Please let me know what changes to make .

#include <AccelStepper.h>
#define SWITCH 7   //input for REED SWITCH
#define LED 13     //pin for the LED
int incomingByte = 0;
int val = 0;       //used to store input value
char incomingPositionValue = 0 ;
char *I , x ;
int i =0;
char X_buffer[5]= {0};
// stepper motor pins: STEP, DIRECTION, STEPPER ON/OFF
AccelStepper Stepper1(1,2,3);



void setup() {
  
  for(int i = 0;i <54;i++) {
    pinMode(i,OUTPUT);
    digitalWrite(i,LOW);
  }
  
  pinMode(SWITCH, INPUT);   //SWITCH is input
  pinMode(LED, OUTPUT);   //tell arduino LED is an output
  
   //Start Serial for debuging purposes	
  Serial.begin(115200); // USB comm with PC

  
  // switch stepper chips off 
  digitalWrite(4,HIGH);
 
  Stepper1.setMaxSpeed(500);
  Stepper1.setAcceleration(500);
  spam();
 
}

void spam() {
  Serial.println("--------------------");
  
  Serial.println("1: Move Stepper1 To Reference position (To Reed Switch)");

  Serial.println("2: Move Stepper1 To desired Forward position");
}



// main loop
void loop() {
  
      if (Serial.available() > 0) {
      incomingByte = Serial.read()-48;
      Serial.print("  Selection:");Serial.println(incomingByte);
      switch(incomingByte) {
          case 1: 
                  Stepper1.setCurrentPosition(0);
                  
                  val=digitalRead(SWITCH);   //read input value from reed switch and store it
                  
                  if(val == 0)     //switch is open
                    { 
                     digitalWrite(4,LOW); digitalWrite(3,HIGH); digitalWrite(2,HIGH); //Serial.println("Stepper1: forward"); 
                     digitalWrite(LED, LOW);
                     Stepper1.moveTo(30000);
                    }
                  
                  if(val == HIGH)
                    {
                     digitalWrite(4,HIGH);
                     Serial.println("Current Position :");
                     Serial.println(Stepper1.currentPosition());
                    }
                  break;
                
          case 2 : Serial.println("Enter Position to move the motor to :");
                   
                   
                   if (Serial.available() > 0) {
                   Serial.flush();
                   delay(5);
                   i=0;
                   while(i<5)
                   {
                    X_buffer[i] = Serial.read();
                   
                    i++;
                    }
                    Serial.flush();
                    
                  
                   incomingPositionValue = atoi(X_buffer);
                   Serial.print("  Position selected :");Serial.println(incomingPositionValue);
                   digitalWrite(4,LOW); digitalWrite(3,HIGH); digitalWrite(2,HIGH); //Serial.println("Stepper1: forward");
                   Stepper1.setCurrentPosition(0);
                   Stepper1.moveTo(- incomingPositionValue);
                   digitalWrite(4,HIGH);
                   Serial.println(Stepper1.currentPosition());
                   }
                   break;
                  
          
          default :spam();
                   break;
          
        }
      }  
                    
                    
     while(Stepper1.currentPosition() !=Stepper1.targetPosition())
          {
           Stepper1.run();
           val=digitalRead(SWITCH); 
           if(val == HIGH)   //switch is closed
             {
              digitalWrite(4,HIGH);
              digitalWrite(LED, HIGH);
              Stepper1.moveTo(Stepper1.currentPosition());
              Serial.println("Current Position :");
              Serial.println(Stepper1.currentPosition());
             } 
            
           } 
    
    if(Stepper1.run() ==0)
    digitalWrite(4,HIGH);
    
    
} // main loop
[/code ]
                   if (Serial.available() > 0) {
                   Serial.flush();

If there is any serial data, dump it without reading it (prior to 1.0). Is this really smart?

After 1.0, the Serial.flush() method only affects the outgoing serial buffer, so why you would be trying to use it here is a mystery.

You are not actually waiting for any serial data to arrive.

Hi Paul ,
I thought it would dump the inital values which it read . I have now removed Serial.flush() from the code . But the result is still the same .
What would you advise me to change .

What would you advise me to change .

The fact that you don't actually wait for serial data to arrive before you try to read it.