2 Inserted loop to keep it running and demonstrated the use of two buttons
source | link

Ideally, you should use interrupts. Define the button.when_pressed value to call a function instead of looping and waiting for input. This way you can have multiple buttons each waiting for a press. Here's an example which uses your code:

from gpiozero import Button
import RPi.GPIO as GPIO
import time

GPIO.setmode (GPIO.BCM) 

continue_running=True

def measure_distance():
    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.")

def exit_cleanly():
    global continue_running
    print ("Goodbye, World!")
    continue_running=False

def main():
    button=Buttonglobal continue_running
    buttonA=Button(21)
    buttonbuttonB=Button(GPIO_PIN_BUTTON_B) # Insert the number of the other pin
    buttonA.when_pressed = measure_distance
    buttonB.when_pressed = exit_cleanly

    # Now loop until you no longer need to
    while(continue_running):
        time.sleep(0.1)

if __name__ == "__main__": main()

Try it out, and let me know how that works!

Ideally, you should use interrupts. Define the button.when_pressed value to call a function instead of looping and waiting for input. Here's an example which uses your code:

from gpiozero import Button
import RPi.GPIO as GPIO
import time

GPIO.setmode (GPIO.BCM)

def measure_distance():
    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.")

def main():
    button=Button(21)
    button.when_pressed = measure_distance

if __name__ == "__main__": main()

Try it out, and let me know how that works!

Ideally, you should use interrupts. Define the button.when_pressed value to call a function instead of looping and waiting for input. This way you can have multiple buttons each waiting for a press. Here's an example which uses your code:

from gpiozero import Button
import RPi.GPIO as GPIO
import time

GPIO.setmode (GPIO.BCM) 

continue_running=True

def measure_distance():
    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.")

def exit_cleanly():
    global continue_running
    print ("Goodbye, World!")
    continue_running=False

def main():
    global continue_running
    buttonA=Button(21)
    buttonB=Button(GPIO_PIN_BUTTON_B) # Insert the number of the other pin
    buttonA.when_pressed = measure_distance
    buttonB.when_pressed = exit_cleanly

    # Now loop until you no longer need to
    while(continue_running):
        time.sleep(0.1)

if __name__ == "__main__": main()

Try it out, and let me know how that works!

1
source | link

Ideally, you should use interrupts. Define the button.when_pressed value to call a function instead of looping and waiting for input. Here's an example which uses your code:

from gpiozero import Button
import RPi.GPIO as GPIO
import time

GPIO.setmode (GPIO.BCM)

def measure_distance():
    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.")

def main():
    button=Button(21)
    button.when_pressed = measure_distance

if __name__ == "__main__": main()

Try it out, and let me know how that works!