Facebook
From del ray, 9 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 269
  1. def measure_distance(trig_pin, echo_pin, num_readings=5):
  2.     distances = []
  3.  
  4.     for _ in range(num_readings):
  5.         GPIO.output(trig_pin, True)
  6.         time.sleep(0.00001)
  7.         GPIO.output(trig_pin, False)
  8.  
  9.         pulse_start = time.time()
  10.         pulse_end = time.time()
  11.  
  12.         while GPIO.input(echo_pin) == 0:
  13.             pulse_start = time.time()
  14.  
  15.         while GPIO.input(echo_pin) == 1:
  16.             pulse_end = time.time()
  17.  
  18.         pulse_duration = pulse_end - pulse_start
  19.         distance = pulse_duration * 17150  # Speed of sound = 34300 cm/s
  20.         distances.append(distance)
  21.  
  22.     avg_distance = sum(distances) / num_readings
  23.     return avg_distance
  24.  

Replies to measure distance rss

Title Name Language When
Re: measure distance del ray 2 text 9 Months ago.