Facebook
From del ray 4, 8 Months ago, written in Plain Text.
This paste is a reply to Re: Re: measure distance from del ray 3 - view diff
Embed
Download Paste or View Raw
Hits: 321
  1.  
  2. import RPi.GPIO as GPIO
  3. import time
  4. import pygame
  5.  
  6. # Configure GPIO pins for sensors
  7. ENTER_TRIG_PIN = 23
  8. ENTER_ECHO_PIN = 24
  9. EXIT_TRIG_PIN = 5
  10. EXIT_ECHO_PIN = 6
  11.  
  12. # Configure GPIO pin for relay (LED strip)
  13. RELAY_PIN = 17
  14.  
  15. # Initialize GPIO settings
  16. GPIO.setmode(GPIO.BCM)
  17. GPIO.setup(ENTER_TRIG_PIN, GPIO.OUT)
  18. GPIO.setup(ENTER_ECHO_PIN, GPIO.IN)
  19. GPIO.setup(EXIT_TRIG_PIN, GPIO.OUT)
  20. GPIO.setup(EXIT_ECHO_PIN, GPIO.IN)
  21. GPIO.setup(RELAY_PIN, GPIO.OUT)
  22.  
  23. # Initialize pygame for audio playback
  24. pygame.init()
  25. pygame.mixer.init()
  26.  
  27. # Set up audio file
  28. audio_file = "music.mp3"  # Replace with your audio file
  29.  
  30. # Function to play audio
  31. def play_audio():
  32.     pygame.mixer.music.load(audio_file)
  33.     pygame.mixer.music.play()
  34.  
  35. # Function to stop audio
  36. def stop_audio():
  37.     pygame.mixer.music.stop()
  38.  
  39. # Function to control LED strip
  40. def control_led_strip(state):
  41.     GPIO.output(RELAY_PIN, state)
  42.  
  43. def measure_distance(trig_pin, echo_pin, num_readings=5):
  44.     distances = []
  45.  
  46.     for _ in range(num_readings):
  47.         GPIO.output(trig_pin, True)
  48.         time.sleep(0.00001)
  49.         GPIO.output(trig_pin, False)
  50.  
  51.         pulse_start = time.time()
  52.         pulse_end = time.time()
  53.  
  54.         while GPIO.input(echo_pin) == 0:
  55.             pulse_start = time.time()
  56.  
  57.         while GPIO.input(echo_pin) == 1:
  58.             pulse_end = time.time()
  59.  
  60.         pulse_duration = pulse_end - pulse_start
  61.         distance = pulse_duration * 17150  # Speed of sound = 34300 cm/s
  62.         distances.append(distance)
  63.  
  64.     avg_distance = sum(distances) / num_readings
  65.     return avg_distance
  66.  
  67. try:
  68.     people_inside = 0  # Counter for people inside the room
  69.     entry_cooldown = False  # Flag to prevent rapid multiple entries
  70.     exit_cooldown = False  # Flag to prevent rapid multiple exits
  71.  
  72.     while True:
  73.         enter_distance = measure_distance(ENTER_TRIG_PIN, ENTER_ECHO_PIN, num_readings=5)
  74.         print("measuring enter distance", enter_distance)
  75.         exit_distance = measure_distance(EXIT_TRIG_PIN, EXIT_ECHO_PIN, num_readings=5)
  76.         print("measuring exit distance", exit_distance)
  77.        
  78.         # Detecting if someone is within 30 cm range for entry
  79.         if enter_distance < 30 and not entry_cooldown:
  80.             people_inside += 1
  81.             entry_cooldown = True
  82.             time.sleep(2)  # Entry cooldown period, adjust as needed
  83.  
  84.         # Detecting if someone is within 30 cm range for exit
  85.         if exit_distance < 30 and people_inside > 0 and not exit_cooldown:
  86.             people_inside -= 1
  87.             exit_cooldown = True
  88.             time.sleep(2)  # Exit cooldown period, adjust as needed
  89.        
  90.         print(people_inside)
  91.  
  92.         if people_inside > 0:
  93.             print("people present")
  94. #             play_audio()
  95. #             control_led_strip(GPIO.HIGH)
  96.         else:
  97.             print("no one present")
  98. #             stop_audio()
  99. #             control_led_strip(GPIO.LOW)
  100.  
  101.         if not (enter_distance < 30):
  102.             entry_cooldown = False
  103.  
  104.         if not (exit_distance < 30):
  105.             exit_cooldown = False
  106.  
  107. except KeyboardInterrupt:
  108.     GPIO.cleanup()
  109.     pygame.mixer.quit()
  110.  

Replies to Re: Re: Re: measure distance rss

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