Starter robot kit UltraSonic sensor delay


#1

I’ve wrote this code for my starter robot kit but I have a problem with the ultrasonic sensor
I’ve made the robot stop and turn back if it ever gets near an object like a wall,but the problem is that for the ultrasonic sensor to function properly, there should be a measure interval of at least (100 ms)
but when I write that down in “void loop” the delay affects the functions of the robot like:
when I press a button to move forward or release the button, there is a 100ms lag.
so how do I make the ultrasonic sensor function properly without any lag ?
the problem is in this part

and this is the complete code:


#2

Is there a function to get the time in with a resolution of milliseconds? If yes then just run the loop as quickly as possible, but update the ultrasonic sensor only when the last readout is >100ms in the past.


#3

thanks for the help,based on your idea, I included “Timer.h” library which has a function that calls another function every amount of time(milliseconds) every(100,UpdateDistance);
this is the new code for anyone who is interested


#4