How to stop detected after 5 seconds using ultrasonic sensor

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

You can RE-edit your first post by clicking on the pencil-icon below your post.

best regards Stefan