Getting out of a pattern in Arduino IDE - Ultrasonic + Motors Using Arduino
You should control ultrasonic sensor continuous when program is running. Your 5 second delay is block this. You put this function instead of delay(5000)

void delay_5000()
{
 for(int i = 0; i&lt; 500; i++){
  if(cm1 &lt;= object){
   findroute();
  }
  delay(10);
 }
}