Python Forum
Controller Buzzer as Distance Decreases
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Controller Buzzer as Distance Decreases
#6
Thanks, yeah I just took code from some some example and worked with it and being a crappy programmer I didn't even think about the int & string thing. I will give that a try.

(Oct-29-2021, 04:05 PM)deanhystad Wrote: Stopping turning the buzzer on does not turn it off. You need to stop the buzzer.

Why are dist and dist2 strings? You never use them as a string, so why convert a nice float to a string only to have to convert it back to a float whenever you use it? And why convert the float to an int? You never need the value as an int.

Use function arguments instead of duplicate code. distance and distance2 are the same function.
from time import sleep, time
import RPi.GPIO as GPIO
from gpiozero import PWMLED, Buzzer
 
GPIO.setmode(GPIO.BCM)
GPIO_BEEP = Buzzer(18)
GPIO_TRIGGER = 23 #rx
GPIO_ECHO = 24 #tx
GPIO2_TRIGGER = 25 #rx
GPIO2_ECHO = 8 #tx
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
GPIO.setup(GPIO2_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO2_ECHO, GPIO.IN)
 
led = PWMLED(21) 
led2 = PWMLED(27) 

def distance(trigger, echo):
    GPIO.output(trigger, True)
    sleep(0.00001)
    GPIO.output(trigger, False)
  
    while GPIO.input(echo) == 0:
        pass
    start_time = time()
  
    while GPIO.input(echo) == 1:
        pass
    return (time() - start_time) * 17150

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 = distance(GPIO_TRIGGER, GPIO_ECHO)
            led.value = ledbrightness(dist)
            
            if 50 < dist < 100: 
                speed = dist/1000
                print("Speed:", speed)
                GPIO_BEEP.beep(speed, speed)

            dist2 = distance(GPIO2_TRIGGER, GPIO2_ECHO)
            led2.value = ledbrightness(dist2)

            print(dist, " : ", dist2)
            time.sleep(0.5)
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        #GPIO.cleanup()
Maybe the buzzer logic should be:
if dist < 100: 
    speed = max(0.5, dist/1000)
    GPIO_BEEP.beep(speed, speed)
else:
    GPIO_BEEP.off()
Reply


Messages In This Thread
RE: Controller Buzzer as Distance Decreases - by barkster - Oct-29-2021, 04:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python and DMX (aka, light controller) ScottAF 4 2,782 Apr-06-2023, 07:09 PM
Last Post: ScottAF
  How can flask find the controller? 3lnyn0 3 1,415 Jul-08-2022, 10:05 AM
Last Post: Larz60+
  Auto re-pair / re-sync Controller via Script? User3000 2 2,398 Nov-30-2020, 11:42 AM
Last Post: User3000
  Tuning PID controller Sancho_Pansa 4 4,487 Nov-09-2020, 07:51 AM
Last Post: Sancho_Pansa
  Xbox Controller arki 0 1,744 Jun-30-2020, 10:32 AM
Last Post: arki
  Please help! Akai APC20 midi controller script versusliveact 0 2,177 Feb-14-2020, 05:37 AM
Last Post: versusliveact
  Visualize Geo Map/Calculate distance zarize 1 1,936 Dec-05-2019, 08:36 PM
Last Post: Larz60+
  managing command codes for external controller box Oolongtea 0 1,952 Sep-19-2019, 08:32 AM
Last Post: Oolongtea
  euclidean distance jenya56 3 2,901 Mar-29-2019, 02:56 AM
Last Post: scidam
  organizing by distance gonzo620 7 4,043 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