r/raspberry • u/Randoom15 • Aug 20 '18
[HELP] Ghosting and Bouncing with buttons
Hey guys,
I am trying to use my Pi 3 as a sort of "jukebox" : I wanna make a sound when I press a button. So I connected a speaker through the 3.5mm jack and a momentary switch, I programmed everything in Python (code bellow) but sometime it triggers itself alone or twice when I pushed once !
I tried adding resistiors and messing with the pulldown resistors but so far no luck ...
Am I missing out on something ?
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO libraryimport timeimport pygametime_stamp = time.time()GPIO.setwarnings(False)GPIO.setmode(GPIO.BOARD)pygame.mixer.init()alerte = pygame.mixer.Sound("son/alerteRouge.wav")
def alerte1():alerte.play()GPIO.output(3, GPIO.HIGH)def button_callback(channel):global time_stamp # put in to debouncetime_now = time.time()if (time_now - time_stamp) >= 0.3:if channel == 10:alerte1()time_stamp = time_now
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback, bouncetime=200) # Setup event on pin 10 rising edgeGPIO.setup(3, GPIO.OUT)message = input("Press enter to quit\n\n") # Run until someone presses enterGPIO.cleanup() # Clean up