Serial Monitor freezing... Ultrasonic Motion Tracker

Hello all,

I have created an ultrasonic motion detector which points and tracks an object using a servo motor. It displays the angle at which it is pointing via a 2 digit 7-segment display.

I am having trouble with the Serial monitor. It appears to be freezing and locking up my controller. I've attached the problematic code below. Here is a short video of the project and the freezing I'm experiencing: - YouTube

Any help would be much appreciated!

FinalCode.ino (9.27 KB)

What makes you think Serial Monitor is the problem? Have you tried it with the Serial output disabled to see whether the problem goes away? An easy way to do this is by adding the following lines to the top of your sketch:

#define DEBUG false  //(false == serial debug output off, true == serial debug output on)
#define DEBUG_SERIAL if(DEBUG)Serial

One thing you should know is that the default timeout for pulseIn() is 1000 ms. That means if the ultrasonic sensor doesn't receive the echo, it will block your code for 1000 ms. You can set the timeout to a more reasonable value via the 3rd parameter:

pulseIn(pin, value, timeout)
1 Like

That means if the ultrasonic sensor doesn't receive the echo, it will block your code for 1000 ms. You can set the timeout to a more reasonable value via the 3rd parameter:

Interesting, I will change this value and report back what happens. Is there a reason why my sensor is not receiving the echo? What do you suggest adding as my timeout?

Thanks

The time-out depends on the furthest distance you want to measure. If you set it to e.g. 10 ms (or 10,000 us, not sure which one to specify, consult the reference page), you can fill that number in in the distance formula and get the maximum distance that the sensor will detect.

I wrote code for a rc car and adjusted it like that; from memory, 10 ms (might have been 100), the max distance was around 170 cm which was sufficient to stop the car in time in front of the obstacle.

1 Like

The third parameter is in microseconds, so it would have been better if I'd said the default timeout is 1000000 us rather than 1000 ms.