diff --git a/chapter8/test_distance_sensors.py b/chapter8/test_distance_sensors.py index 201f743..7ac8e30 100644 --- a/chapter8/test_distance_sensors.py +++ b/chapter8/test_distance_sensors.py @@ -1,12 +1,15 @@ import time from gpiozero import DistanceSensor +from gpiozero.pins.pigpio import PiGPIOFactory +factory = PiGPIOFactory() print("Prepare GPIO Pins") -sensor_l = DistanceSensor(echo=17, trigger=27, queue_len=2) -sensor_r = DistanceSensor(echo=5, trigger=6, queue_len=2) +# register the sensors and map to Raspberry Pi pins +sensor_l = DistanceSensor(echo=17, trigger=27, queue_len=2, pin_factory=factory) +sensor_r = DistanceSensor(echo=5, trigger=6, queue_len=2, pin_factory=factory) +# print out distance to sensor while True: - print("Left: {l:.2f}, Right: {r:.2f}".format( - l=sensor_l.distance * 100, - r=sensor_r.distance * 100)) + print(f'Left: {sensor_l.distance * 100:.2f}, Right: {sensor_r.distance * 100:.2f}') + # force pause to avoid flooding output time.sleep(0.1)