def process_direction(self, sensor: DistanceSensor, floor: float, db_field: str): ''' When a sensor is tripped, detremines if the person went all the way through, or turned back before tripping the second sensor. Note: There is an allotted time to trip the second sensor before it times out and "thinks" the person has turned back. ''' print('tripped') date = utils.iso_date( ) # iso 8601 format (yyyy-mm-dd) because we're not savages start_time = utils.unix_time() # used for timeout while utils.unix_time( ) < start_time + self.timeout: # poll the sensors until timeout if sensor.distance < floor: print('adding to db') self.db.increment(db_field, date) sensor.wait_for_out_of_range() return sleep(0.001)
from gpiozero import DistanceSensor from time import sleep ultrasonic = DistanceSensor(echo=19, trigger=6) while True: print("Y") ultrasonic.wait_for_in_range() print("In range") ultrasonic.wait_for_out_of_range() print("Out of range") #print("T") #ultrasonic.wait_for_in_range() #print("In range") #ultrasonic.wait_for_out_of_range() #print("Out of range") #sleep(1) #print(ultrasonic.distance)
from gpiozero import DistanceSensor us = DistanceSensor(echo = 17, trigger = 4) while True: us.wait_for_in_range() print(us.distance) us.wait_for_out_of_range() print('Out of range')
#!/usr/bin/env python3 import move import signal from gpiozero import DistanceSensor min_distance = 0.15 sonic_sensor = DistanceSensor( echo=18, trigger=17, threshold_distance=min_distance) speed = 0.5 def exit_gracefully(): exit() sonic_sensor.when_in_range = lambda: move.stop() sonic_sensor.when_out_of_range = lambda: move.forward() signal.signal(signal.SIGINT, exit_gracefully) move.forward(speed) while True: sonic_sensor.wait_for_in_range() move.backward(speed) sonic_sensor.wait_for_out_of_range() move.right(speed, 0.75) exit_gracefully()