Problem with Serial communication in arduino

My project is to make a motor move at a certain speed and to a certain position. I am using the accelstepper library to control the motor. I know how to make it work if I put the speed and the position in the code, but what I want is to make them variables and the user enters the value for the speed , distance, and acceleration they want, using the serial monitor. I know how to do this in C++ using cin, but here when I use Serial.read(), it just doesnt work and one value entered by the user goes to all the variables. The motor moves when i put the values of the speed, distance, acceleration in the code, but when i ask the user to put it, it doesnt move.
can you please help?
Also if there is a good book or online source about serial communication or accelstepper, Ill be grateful if you can point me to it.

Thank you so much,
here is my code:

#include <AccelStepper.h>

AccelStepper motor(1,2,3); // step pin = 2 dir pin = 3
char mode;                // 1 for forward, 2 for backward
double velocity;
double distance;
double acceleration;
void setup() 
{    
        Serial.begin(9600); 
        Serial.println("Please choose a number for the desired mode:");
        Serial.println("1) forward");
        Serial.println("2) backward");
}

void loop() 
{
  if(Serial.available())
  {
    mode=Serial.read();
    Serial.println(mode);
  if (mode=='1')  //also tried if mode ==49
  {
    Serial.println("you have chosen forward"); //prints "you have chosen forward" but motor doesnt move,
    Serial.println("enter speed");
    velocity=Serial.read();
     Serial.println("enter acceleration");
     acceleration=Serial.read();
     Serial.println("enter distance");
     distance=Serial.read();
     motor.setCurrentPosition(0);  
     moveup();
  }
  if (mode=='2')
  {
    Serial.println("you have chosen the backward mode");
      Serial.println("enter speed");
    velocity=Serial.read();
     Serial.println("enter acceleration");
     acceleration=Serial.read();
     Serial.println("enter distance");
     distance=Serial.read();
     motor.setCurrentPosition(0);  
     movedown();
  }
}
}

void moveup()
{
  while(mode==1)
  {
    motor.setMaxSpeed(velocity);
    motor.setAcceleration(acceleration);
    motor.moveTo(-distance);
    motor.run();
  }    
}


void movedown()  
{
  while(mode==2)
  {
    motor.setMaxSpeed(velocity);
    motor.setAcceleration(acceleration);
    motor.moveTo(distance);
    motor.run();
  }
}
  if (mode=='1')  //also tried if mode ==49
  {
    Serial.println("you have chosen forward"); //prints "you have chosen forward" but motor doesnt move,
    Serial.println("enter speed");
    velocity=Serial.read();

Exactly how long does the Arduino have to send the message, for the receiving system to read and display the message, for the user to read it, for the user to enter a value, and for the receiving system to send the data back to the Arduino?

About 65 nanoseconds is the time between Serial.orintln() returning, and the read() method being called. If you think that the message will even have been shifted out by then, I've got news for you.

so should i put delay(100); before every Serial.read();
sorry, I am new with the serial communication.
thank you for your reply

so should i put delay(100); before every Serial.read();

Absolutely. Try about two weeks.

Seriously, no. What you need to do is implement a state machine. At any given time, you are in some state - waiting for a complete velocity, waiting for a mode, waiting for a complete acceleration value, etc.

On any given pass through loop, you deal with the next character read depending on what state you are in.

You must ensure that your code is layed out correct (by using the auto format (look in the IDE's tools menu) this makes the structure of your code clear to you (about whom I do not care) and to us.

Mark

These demos - communicating between a PC and an Arduino and several things at a time may be useful.

The second one illustrates the use of millis() for timing, the use of variables to record the state of things and the organization of code into small functions.

...R

kyrollos:
so should i put delay(100); before every Serial.read();
sorry, I am new with the serial communication.
thank you for your reply

or look at Nick Gammon's excellent example of a State Machine