Using ultrasonic sensor to detect wall when robot is driving

You are spending time in pulseIn(), too.

Look at the NewPing library. It uses interrupts to handle the reading of the echo, without the need for any blocking code.