Facebook
From del ray 2, 8 Months ago, written in Plain Text.
This paste is a reply to measure distance from del ray - view diff
Embed
Download Paste or View Raw
Hits: 291
  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.  
  25.  
  26. import RPi.GPIO as GPIO
  27. import time
  28. import pygame
  29.  
  30. # ... (rest of the code remains the same)
  31.  
  32. try:
  33.     people_inside = 0  # Counter for people inside the room
  34.     entry_cooldown = False  # Flag to prevent rapid multiple entries
  35.  
  36.     while True:
  37.         enter_distance = measure_distance(ENTER_TRIG_PIN, ENTER_ECHO_PIN, num_readings=5)
  38.         exit_distance = measure_distance(EXIT_TRIG_PIN, EXIT_ECHO_PIN, num_readings=5)
  39.  
  40.         # Detecting if someone is within 30 cm range
  41.         if enter_distance < 30 and not entry_cooldown:
  42.             people_inside += 1
  43.             entry_cooldown = True
  44.             time.sleep(2)  # Cooldown period, adjust as needed
  45.  
  46.         if exit_distance < 30 and people_inside > 0:
  47.             people_inside -= 1
  48.  
  49.         if people_inside > 0:
  50.             play_audio()
  51.             control_led_strip(GPIO.HIGH)
  52.         else:
  53.             stop_audio()
  54.             control_led_strip(GPIO.LOW)
  55.  
  56.         if not (enter_distance < 30):
  57.             entry_cooldown = False
  58.  
  59. except KeyboardInterrupt:
  60.     GPIO.cleanup()
  61.     pygame.mixer.quit()
  62.  

Replies to Re: measure distance rss

Title Name Language When
Re: Re: measure distance del ray 3 text 8 Months ago.