Python Forum
Button Presses in Certain Time Frame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Button Presses in Certain Time Frame
#1
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  :)
Reply
#2
thank you, sorry I'm new to all this
Reply
#3
(Jun-30-2017, 02:23 PM)MsCheeseyPuff Wrote:
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)
I'm confused, does any sound come out at all right now?  lib_a() and lib_b() don't return anything, so your if blocks should never run.

This is unrelated, but instead of having a different function for each button with a bunch of hardcoded values for which pin it is and which sound file, your life would be much better if all of those were really just a single function.  Also, don't use exit(), since the rest of your code will never run (you'll never cleanup the pins at the end).  Something like:
# first, declare the pins
sounds = {
   21: {
       "sound": pygame.mixer.Sound('/home/pi/Documents/PiSounds/applause.wav'),
       "alt-sound": pygame.mixer.Sound('/home/pi/Documents/PiSounds/buzzer.wav'),
       "channel": pygame.mixer.Channel(1),
       "alt-channel": pygame.mixer.Channel(1),
       "out-pin": 6
   }
# etc
}

def button_pressed(in_pin):
    sound = sounds[in_pin]

    if (GPIO.input(in_pin) == False and lib_a()):
        sound["channel"].play(sound["sound"])
        while pygame.mixer.get_busy() == True:
            GPIO.output(sound["out-pin"], True)
        else:
            GPIO.output(sound["out-pin"], GPIO.LOW)
    if (GPIO.input(in_pin) == False and lib_b()):
        sound["alt-channel"].play(sound["alt-sound"])
        while pygame.mixer.get_busy() == True:
            GPIO.output(sound["out-pin"], True)
        else:
            GPIO.output(sound["out-pin"], GPIO.LOW)

# now to listen for presses...
running = True
while running:
    try:
        for pin in sounds:
            if not GPIO.input(pin):
                button_pressed(pin)
        sleep(1)
    except KeyboardInterrupt:
        running = False
GPIO.cleanup()
Reply
#4
Thank you so much!! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Give visit number ( or counter) within the same time frame balthordu 1 954 Jun-27-2022, 09:51 AM
Last Post: Larz60+
  run different functions each time the same button is pressed? User3000 6 3,309 Jul-31-2020, 11:11 PM
Last Post: User3000
  How to detect key presses BlinkyGamer99 3 2,090 Mar-15-2020, 06:11 AM
Last Post: michael1789
  Count to movement according to the time pressed button noartist 1 2,516 Feb-27-2019, 01:33 PM
Last Post: noartist

Forum Jump:

User Panel Messages

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