Jun-30-2017, 02:23 PM
(This post was last modified: Jun-30-2017, 02:46 PM by sparkz_alot.)
I made a sound board and I wanted to improve it, so I want to change libraries with the amount of button presses(library A with one press and library B with two). I also have an LED to show that the button has been pressed. How do I count the button presses and have it within a time frame? Here's my code:
import pygame.mixer from time import sleep import RPi.GPIO as GPIO from sys import exit GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(9, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(6, GPIO.OUT) GPIO.setup(5, GPIO.OUT) GPIO.setup(24, GPIO.OUT) GPIO.setup(23, GPIO.OUT) GPIO.setup(27, GPIO.OUT) GPIO.setup(17, GPIO.OUT) GPIO.setup(22, GPIO.OUT) PAUSE = 0.5 #var = 1 presses = 0 GPIO.output(6, False) GPIO.output(5, False) GPIO.output(24, False) GPIO.output(23, False) GPIO.output(27, False) GPIO.output(17, False) GPIO.output(22, False) print ('Soundboard Ready') def lib_a(): global presses presses1 = presses + 1 def lib_b(): global presses presses2 = presses + 2 while True: ## def lib_b(): ## global presses ## presses2 = presses + 2 if (GPIO.input(20) == False): if (lib_b()): GPIO.output(22, True) time.sleep(PAUSE) GPIO.output(22, GPIO.LOW) time.sleep(PAUSE) pygame.mixer.init(48000, -16, 1, 1024) sound_1=pygame.mixer.Sound('/home/pi/Documents/PiSounds/applause.wav') sound_2=pygame.mixer.Sound('/home/pi/Documents/PiSounds/buzzer.wav') sound_3=pygame.mixer.Sound('/home/pi/Documents/PiSounds/castlethunder.wav') sound_4=pygame.mixer.Sound('/home/pi/Documents/PiSounds/clap.wav') sound_5=pygame.mixer.Sound('/home/pi/Documents/PiSounds/scream.wav') sound_6=pygame.mixer.Sound('/home/pi/Documents/PiSounds/scream2.wav') soundChannel1=pygame.mixer.Channel(1) soundChannel2=pygame.mixer.Channel(2) soundChannel3=pygame.mixer.Channel(3) soundChannel4=pygame.mixer.Channel(4) soundChannel5=pygame.mixer.Channel(5) soundChannel6=pygame.mixer.Channel(6) sound_7=pygame.mixer.Sound('/home/pi/Documents/PiSounds/buzzer.wav') sound_8=pygame.mixer.Sound('/home/pi/Documents/PiSounds/clap.wav') sound_9=pygame.mixer.Sound('/home/pi/Documents/PiSounds/scream.wav') sound_10=pygame.mixer.Sound('/home/pi/Documents/PiSounds/scream2.wav') sound_11=pygame.mixer.Sound('/home/pi/Documents/PiSounds/laugh.wav') sound_12=pygame.mixer.Sound('/home/pi/Documents/PiSounds/applause.wav') soundChannel7=pygame.mixer.Channel(1) soundChannel8=pygame.mixer.Channel(2) soundChannel9=pygame.mixer.Channel(3) soundChannel10=pygame.mixer.Channel(4) soundChannel11=pygame.mixer.Channel(5) soundChannel12=pygame.mixer.Channel(6) def button_1(): if (GPIO.input(21) == False and lib_a()): soundChannel1.play(sound_1) while pygame.mixer.get_busy() == True: GPIO.output(6, True) else: GPIO.output(6, GPIO.LOW) if (GPIO.input(21) == False and lib_b()): soundChannel7.play(sound_7) while pygame.mixer.get_busy() == True: GPIO.output(6, True) else: GPIO.output(6, GPIO.LOW) def button_2(): if (GPIO.input(19) == False and lib_a()): soundChannel2.play(sound_2) while pygame.mixer.get_busy() == True: GPIO.output(5, True) else: GPIO.output(5, GPIO.LOW) if (GPIO.input(19) == False and lib_b()): soundChannel8.play(sound_8) while pygame.mixer.get_busy() == True: GPIO.output(5, True) else: GPIO.output(5, GPIO.LOW) def button_3(): if (GPIO.input(7) == False and lib_a()): soundChannel3.play(sound_3) while pygame.mixer.get_busy() == True: GPIO.output(24, True) else: GPIO.output(24, GPIO.LOW) if (GPIO.input(7) == False and lib_b()): soundChannel9.play(sound_9) while pygame.mixer.get_busy() == True: GPIO.output(24, True) else: GPIO.output(24, GPIO.LOW) def button_4(): if (GPIO.input(8) == False and lib_a()): soundChannel4.play(sound_4) while pygame.mixer.get_busy() == True: GPIO.output(23, True) else: GPIO.output(23, GPIO.LOW) if (GPIO.input(8) == False and lib_b()): soundChannel10.play(sound_10) while pygame.mixer.get_busy() == True: GPIO.output(23, True) else: GPIO.output(23, GPIO.LOW) def button_5(): if (GPIO.input(9) == False and lib_a()): soundChannel5.play(sound_5) while pygame.mixer.get_busy() == True: GPIO.output(27, True) else: GPIO.output(27, GPIO.LOW) if (GPIO.input(9) == Flase and lib_b()): soundChannel11.play(sound_11) while pygame.mixer.get_busy() == True: GPIO.output(27, True) else: GPIO.output(27, GPIO.LOW) def button_6(): if (GPIO.input(10) == False and lib_a()): soundChannel6.play(sound_6) while pygame.mixer.get_busy() == True: GPIO.output(17, True) else: GPIO.output(17, GPIO.LOW) if (GPIO.input(10) == False and lib_b()): soundChannel12.play(sound_12) while pygame.mixer.get_busy() == True: GPIO.ouput(17, True) else: GPIO.output(17, GPIO.LOW) while True: try: if (GPIO.input(21) == False): button_1() if (GPIO.input(19) == False): button_2() if (GPIO.input(7) == False): button_3() if (GPIO.input(8) == False): button_4() if (GPIO.input(9) == False): button_5() if (GPIO.input(10) == False): button_6() sleep(1) except KeyboardInterrupt: exit() GPIO.cleanup()I hope someone can help me and thank you so much :)