I apologize for my lack of knowledge, but I'm tying to have an ultrasonic sensor measure distance but I want it to measure distance every time I press the button. the script I have works, but only once and then I have to re run (F5) the program to get another reading. what am I missing? am I going about this the wrong way?
from gpiozero import Button
button=Button(21)
button.wait_for_press()
import RPi.GPIO as GPIO
import time
GPIO.setmode (GPIO.BCM)
TRIG = 23
ECHO = 24
print ("Distance Measurement In Progress")
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG, False)
print ("waiting For Sensor To Settle")
time.sleep(2)
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 * 6756
distance = round(distance, 2)
print ("Distance:",distance,"in.")
GPIO.cleanup()
So after I press the button, and it runs through the script giving me a distance measurement, I want to be able to press the button again for a new measurement. Thank you in advance.