         }
       }
 
-      //Serial.write("limit_reached:");
-      //Serial.write(limit_reached);
-      //Serial.println();
-
-      if (false == limit_reached)
+      if (!limit_reached)
       {
         motors_control.steppers[m]->run();
         is_one_motor_running = true;
       }
-      else
-      {
+      else{
         motors_control.steppers[m]->setCurrentPosition(0);
-      }
-
+        motors_control.set_motor_running(m, 0);
+        Serial.write("M");
+        Serial.print(m);
+        Serial.write('#');
+      }
     }
     else{
       motors_control.steppers[m]->setCurrentPosition(0);
+// the motor has just finished the move, so we output that event
+      if (motors_control.is_motor_running(m)){
+        motors_control.set_motor_running(m, 0);
+        Serial.write("M");
+        Serial.print(m);
+        Serial.write('#');
+      }
     }
   }
-
-    
-  if (!is_one_motor_running)
-    is_command_running = 0;
 }
 

  Serial.println();

  current_buffer[0] = 0;
  is_command_running = 0;

  //motors_control.add_sensor(0, senTypePotentiometer, 0); // THIS IS ONLY FOR TEST PURPOSE
}

//--------------------------------------------------------------------------------------------

        int motor_index, num_steps;
        sscanf(tmp_str + i + 1, "%d%d", &motor_index, &num_steps);
        motors_control.move_motor(motor_index, num_steps);
        motors_control.set_motor_running(motor_index, 1);
        is_command_running = 1;
        i += 3;
      }
