I have built a simple program to make an ultrasonic sensor detect whether it is too close to an object or not. If it is too close, it will light up an LED, else it will turn off the LED. The code I have does work as intended, but if I gradually change the distance it is detecting the program will freeze/seem to stop looping. If I change the distance abruptly (such as bringing my hand down in front of the sensor quickly or jerking it away) it continues to behave as expected… most of the time. It will freeze occasionally even if I do it quickly, but a slow change guarantees that it will freeze.
The problem seems to be crossing the “too close” threshold slowly, but I don’t understand why that would be the case. I have tried different delays, and that makes it a bit more reliable, but even at ~1s delays a detection near the threshold will freeze it.
Code is below. Any tips or insight would be greatly appreciated, as I’m very new to XOD. (I only just discovered it this morning but it’s perfect for my needs!).