Why does this program freeze for aprox 1 second when receiving serial commands?

Hi all.

I have a piece of code that works about as hoped.
The program reads a sensor and outputs the sensor data to the serial port, it also controls a servo from commands received from the serial port.

However, whenever I send a command for the server to move to a new position the whole program freezes for about a second before moving the servo and resuming reporting the sensor values. Why is this and is there anything I can do about it?
Please see relevant code and I am also happy for any suggestions for improvements.

Thank you!

void loop() {

  //// serial input and motor control below.
  if (Serial.available() > 0)
    target = Serial.parseInt();
    timeToGetThere = Serial.parseFloat();

    if (target == currentPosition) { // if no change in position do nothing
      difference = 0.;
    else { //Calculate difference between current position and target
      difference = target - currentPosition;
      startPoint = currentPosition;
    if (timeToGetThere < 1) { //force minimum time
      timeToGetThere = 1;
    startTime = millis(); //set startime for this trajecotry
    targetTime = startTime + timeToGetThere;

  currentTime = millis();
  if (currentTime <= targetTime) {
    long remain = targetTime - currentTime;
    float fraction = (timeToGetThere - remain) / timeToGetThere;
    Serial.println("fraction is;");
    currentPosition = startPoint + difference * fraction;


If you post ALL of the code we can see how variables and objects are declared, what is in setup() and copy to our IDE to see if the code will, if possible, compile and run.

target = Serial.parseInt();

A second, you say? And the time out period for parseInt is. . . ?


Well what do you know.


I will be less lazy and make a serial parser.


The code in serial input basics should be suitable. It receives without blocking.