0

I'm trying to write a program that displays the output of a ultrasonic sensor attached to a Pi 3B+. The code 'hangs' by which I mean that the display of output suddenly stops and the program doesnt respond anymore. Can someone help me point out the mistake in my code?

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BOARD)

TRIG = 16
ECHO = 18

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)

try:
    while True:
       GPIO.output(TRIG, True)
       time.sleep(0.00001)
       GPIO.output(TRIG, False)

       while GPIO.input(ECHO)==0:
          pulse_start = time.time()

       while GPIO.input(ECHO)==1:
          pulse_end = time.time()

       pulse_duration = pulse_end - pulse_start

       distance = pulse_duration * 17150

       distance = round(distance+1.15, 2)

       if distance<=400 and distance>=5:
          print "distance:",distance,"cm"

except KeyboardInterrupt:
     GPIO.cleanup()

Edit: Solved the issue, the Try and except block might have been the culprit, I removed the try block and now I can output indefinitely. Thank you for you help.

LeafTeaNeko
  • 113
  • 1
  • 13
  • Yea, I think. because otherwise I wouldn't get any output with the "distance:", distance line. The output is continuous for a couple of seconds to a min then it stops. – LeafTeaNeko Dec 04 '19 at 00:10
  • Could you post a screenshot of where it hangs so we know when exactly it hangs? – MegaEmailman Dec 04 '19 at 00:34
  • Thank you MegaEmailman, but I think its working now. I cant post the screenshot because it stops randomly, there was no set pattern to the code hanging, it simply stops displaying any output when it hangs. – LeafTeaNeko Dec 04 '19 at 00:36
  • 1
    I think you should either delete the question or post your own answer. – Kolban Dec 04 '19 at 00:50
  • Thank you Kolban, I attached the answer below. – LeafTeaNeko Dec 04 '19 at 01:44

1 Answers1

1

The error was with the Try and Except block of the code, if the receiver on the board doesnt receiver any echo output, it fails the try block and enters the except block where I clear all the pins attached to the Pi. This is why the code stops working after a while, the pins get cleared as soon as an Echo is not detected. removing the try block allows for uninterrupted ranging. Code should be modified as below:

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BOARD)

TRIG = 16
ECHO = 18

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)
while True:
           GPIO.output(TRIG, True)
           time.sleep(0.00001)
           GPIO.output(TRIG, False)

           while GPIO.input(ECHO)==0:
              pulse_start = time.time()

           while GPIO.input(ECHO)==1:
              pulse_end = time.time()

           pulse_duration = pulse_end - pulse_start

           distance = pulse_duration * 17150

           distance = round(distance+1.15, 2)

           if distance<=400 and distance>=5:
              print "distance:",distance,"cm"
LeafTeaNeko
  • 113
  • 1
  • 13