Trouble with Ultrasonic sensor


i am trying to print an error message to the serial port when my ultrasonic sensor senses something within 15cm. The only problem when it senses that something is in range it stops everything else and spams the serial port with the error message. Is there a way I can send a message to the serial port once when an item is in range and then return to the main loop.

Code please.

Add a boolean which you set to true if the object is in range. If that "flag" is true, you don't print anything on the serial port. If the object moves away, reset that flag to false.

Don't forget to add a threshold. This means, wait until the object is like 20cm away before you reset the flag.

See the info and example code HERE: