Python Forum
Controller Buzzer as Distance Decreases
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Controller Buzzer as Distance Decreases
#1
I have a raspberry pi and am trying to simulate something similar to backup sensors on a car. I have some ultrasonic sensors that turn on buzzer when value is between 100-50. I would like to make the buzzer beep faster as the distance value gets closer to 50. My problem is that since I'm trying to use multiple sensors and the code is running every .5 seconds that when it is in between my range the buzzer comes on as expected but it fires so many times that when your out of range it continues to fire because it is called so many time. Or at least that is what I think is happening. Pretty new to python and not a very good coder. Any help would be appreciated. Here is what I have so far.

#Libraries
import RPi.GPIO as GPIO
import time
from gpiozero import PWMLED
from time import sleep
from gpiozero import Buzzer

#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)

#Buzzer
GPIO_BEEP = Buzzer(18)


#set GPIO Pins
GPIO_TRIGGER = 23 #rx
GPIO_ECHO    = 24 #tx

#second sensor
GPIO2_TRIGGER = 25 #rx
GPIO2_ECHO = 8 #tx
 
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
#second sensor
GPIO.setup(GPIO2_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO2_ECHO, GPIO.IN)

led = PWMLED(21) 
#led.value = 0    # off
#led.value = 0.2  # 20% brightness 
#led.value = 0.4  # 40% brightness 
#led.value = 0.6  # 60% brightness 
#led.value = 1    # full brightness


led2 = PWMLED(27) 
#led2.value = 0    # off
#led2.value = 0.2  # 20% brightness 
#led2.value = 0.4  # 40% brightness 
#led2.value = 0.6  # 60% brightness 
#led2.value = 1    # full brightness
#led.pulse()
#led2.pulse()
# led.blink(.1,.1)
# led2.blink(.1,.1)
# sleep(3)
# led.off()
# led2.off()
def distance():
    # set Trigger to HIGH
    GPIO.output(GPIO_TRIGGER, True)
 
    # set Trigger after 0.01ms to LOW
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
 
    StartTime = time.time()
    StopTime = time.time()
 
    # save StartTime
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
 
    # save time of arrival
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()
 
    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    distance = (TimeElapsed * 17150)
 
    return distance

def distance2():
    # set Trigger to HIGH
    GPIO.output(GPIO2_TRIGGER, True)
 
    # set Trigger after 0.01ms to LOW
    time.sleep(0.00001)
    GPIO.output(GPIO2_TRIGGER, False)
 
    StartTime = time.time()
    StopTime = time.time()
 
    # save StartTime
    while GPIO.input(GPIO2_ECHO) == 0:
        StartTime = time.time()
 
    # save time of arrival
    while GPIO.input(GPIO2_ECHO) == 1:
        StopTime = time.time()
 
    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    distance2 = (TimeElapsed * 17150)
 
    return distance2

def ledbrightness(num):
    if num <= 60:
        brightness = 1
    elif num <= 200:
        brightness = .1
    else:
        brightness = 0
    return brightness

if __name__ == '__main__':
    try:
        while True:
            dist = "%.1f" % distance()
            led.value = ledbrightness(float(dist))
            if int(float(dist)) <= 100 and int(float(dist)) >= 50:
                speed = int(float(dist))/1000
                print("Speed:" + str(speed))
                GPIO_BEEP.beep(speed,speed)
            #print ("Measured Distance = %.1f cm" % dist)
            dist2 = "%.1f" % distance2()
            led2.value = ledbrightness(float(dist2))
            print(dist + " : " + dist2)
            time.sleep(.5)
 
        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        #GPIO.cleanup()
Reply


Messages In This Thread
Controller Buzzer as Distance Decreases - by barkster - Oct-29-2021, 01:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python and DMX (aka, light controller) ScottAF 4 2,773 Apr-06-2023, 07:09 PM
Last Post: ScottAF
  How can flask find the controller? 3lnyn0 3 1,411 Jul-08-2022, 10:05 AM
Last Post: Larz60+
  Auto re-pair / re-sync Controller via Script? User3000 2 2,386 Nov-30-2020, 11:42 AM
Last Post: User3000
  Tuning PID controller Sancho_Pansa 4 4,473 Nov-09-2020, 07:51 AM
Last Post: Sancho_Pansa
  Xbox Controller arki 0 1,742 Jun-30-2020, 10:32 AM
Last Post: arki
  Please help! Akai APC20 midi controller script versusliveact 0 2,169 Feb-14-2020, 05:37 AM
Last Post: versusliveact
  Visualize Geo Map/Calculate distance zarize 1 1,928 Dec-05-2019, 08:36 PM
Last Post: Larz60+
  managing command codes for external controller box Oolongtea 0 1,948 Sep-19-2019, 08:32 AM
Last Post: Oolongtea
  euclidean distance jenya56 3 2,888 Mar-29-2019, 02:56 AM
Last Post: scidam
  organizing by distance gonzo620 7 4,034 Oct-16-2018, 01:41 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020