Oct-29-2021, 01:25 PM
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()
