Make system with ultrasonic sensor and servo
Look at your code sequence. An ultrasonic measurement is only taken, when you use pulseIn(). At the end of the loop() function you call your servo() function. In it you tell the Arduino to first go from 0° to 180° and then back to 0°. AFTER THAT (!) the function exits and the loop() function continues. This means, that nothing else happens during the servo motion. The measurement only takes place between the motions, meaning about every 10s.

To solve this problem you need to make the servo code non-blocking. It is mostly a bad thing to use delay(), since you cannot do anything else during waiting. Think about how you are backing a pizza. You are not sitting in front of the oven the whole time, until the pizza is ready. Instead you are looking at your clock regularily and check, if it is time to take the pizza out of the oven.

You can do the same thing on an Arduino with the millis() function. It will return the milliseconds since the last reset/powerup of the Arduino. This has to be used as a clock. To do something after a specific time you first save the current time in a timestamp:

unsigned long timestamp=0;
...
timestamp = millis();


Then in the loop() function you are regularly (on every loop iteration) checking, if the difference between the timestamp and the current time is above your set interval:

if(millis() - timestamp &gt; 30){
    // do something here, like moving a servo
    timestamp = millis(); //update timestamp for next event
}


The Arduino will not do the things simultaneously, but will switch between the different code parts so fast, that it will seem simultaneous. Also refer to the BlinkWithoutDelay example of the Arduino library.

Note: The current delay between two consecutive servo values is ok. If you want to go way faster with the servo, you have to watch out, that the rest of the code in loop() doesn't take to long. If this happens and you cannot make the other code parts non-blocking too, you can also do the servo part in an interrupt routine from a timer.