class Sensor: def __init__(self, trigger_pin, echo_pin, angle): self._trigger_pin = trigger_pin self._echo_pin = echo_pin self._angle = angle self._sr04 = Echo(self._trigger_pin, self._echo_pin) def getDistance(self, samples = 1): return self._sr04.read('cm', samples) def getAngle(self): return self._angle def getTriggerPin(self): return self._trigger_pin def getEchoPin(self): return self._echo_pin def stop(self): self._sr04.stop()
#DEVANT ; trig 27 echo 22 // gauche : echo 17 trigger 18 """File: echo_multi_sensor.py""" # Import necessary libraries. from time import sleep from Bluetin_Echo import Echo # Define pin constants TRIGGER_PIN_1 = 27 ECHO_PIN_1 = 22 TRIGGER_PIN_2 = 18 ECHO_PIN_2 = 17 speed_of_sound = 315 while True: capteurDevant = Echo(TRIGGER_PIN1, ECHO_PIN1, speed_of_sound) samples = 5 result = capteurDevant.read('cm', samples) print(result, 'cm') capteurDevant.stop() capteurGauche = Echo(TRIGGER_PIN2, ECHO_PIN2, speed_of_sound) samples = 5 result = capteurGauche.read('cm', samples) print(result, 'cm') capteurGauche.stop() time.sleep(1)
"""File: echo_simple_once.py""" # Import necessary libraries. from Bluetin_Echo import Echo # Define GPIO pin constants. TRIGGER_PIN = 16 ECHO_PIN = 12 # Initialise Sensor with pins, speed of sound. speed_of_sound = 315 echo = Echo(TRIGGER_PIN, ECHO_PIN, speed_of_sound) # Measure Distance 5 times, return average. samples = 5 result = echo.read('cm', samples) # Print result. print(result, 'cm') # Reset GPIO Pins. echo.stop()
# https://github.com/MarkAHeywood/Bluetin_Python_Echo from Bluetin_Echo import Echo # HCSR04 Module Uses BCM Pins! from time import sleep # BCM 23 & 24 are DPI 19 and 20 DistSense = Echo(23, 24) def report_dist(): samples = 4 try: while True: d = DistSense.read('cm', samples) print(d, 'cm') sleep(.5) except KeyboardInterrupt: pass report_dist() DistSense.stop() print('End Of File.')