Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # works with sonic sensor HC-SR04
- from gpiozero import DistanceSensor
- from time import sleep
- import sys
- sensor = DistanceSensor(echo=17, trigger=4, max_distance=2, threshold_distance=0.04) #defines min and max in meters
- sys.stdout.flush()
- try:
- while True:
- pingerValue = float(sensor.distance * 100) # convert snsor reading to cm
- if pingerValue <= 4.00 : # min distance to read is 4cm
- sys.stdout.write("\rDistance: >>>...") # using sys.stdout write in place of print as it give neater terminal output
- elif pingerValue >= 200.00 : # max distance to read is 200cm. NB: To test keep the distance at less than 100cm.
- sys.stdout.write("\rDistance: <<<...")
- elif sys.stdout.write("\rDistance: %6.2f" % pingerValue + " cm\r") : # \r for terminal; \n for IDLE window
- sys.stdout.flush() # clear screen
- sleep(0.01)
- except KeyboardInterrupt:
- print ("\nExit")
- sys.stdout.flush()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement