What am actually doing is to make the ultrasonci sensor to check the distance once the progame is excuted to allow the motors to run if the condtion i made is true. I was able to do that using the attahced codes. What am trying to do i to keep checking the distance while the robot is navigating without the need of stoping the robot for a while. Meaning that the ultrasonci should stop the robot once the distance is less than 50cm. So my question is , is it possible to run two two functions at once which are driving the motors and keep checking the distance? Thanks in advnace.
No. It IS possible for Forward() to measure distance, and take appropriate action if the distance goes below some threshold. Be aware, if you do this, that Forward() is no longer an appropriate name for the function.
It is better, however, to separate the "get moving" functionality from the "pick a direction" functionality.
Once you have decided that Forward() is the correct way to go, let the robot keep going that way until some other part of the code decides that a new direction is in order.