Biffboff
Posts: 57
Joined: Sat Mar 15, 2014 6:38 pm

Re: Pi Powerd Audio for EM Rifle Game - Rodeo Rebooted

Sun Sep 11, 2022 6:15 pm

Okay, so MVO1 finished - yeyyyyy thanks for the help Gordon and ame: -

All FX sounds working via simple inputs no logic (basic input to file output sound) and a defined code structure.

Seems like we have a ghost a in the machine too - watch till the end... :-)

Will make a start on MVO2 next week.

Link to the to the MVO1 Video: -

11/09/2022 - MVO1 or minimum Viable Offering 1 will comprise: All FX sounds working via inputs but no logic (basic input to file output sound) and a defined code structure.

https://youtu.be/oJdbk1oTY1Y 19

JB

Biffboff
Posts: 57
Joined: Sat Mar 15, 2014 6:38 pm

Re: Pi Powerd Audio for EM Rifle Game - Rodeo Rebooted

Fri Sep 23, 2022 6:03 pm

MVO2 is taking its time, i am reading up on Python (Python for dummies) etc. One issue i would like to get rid of is when i press a button, sometimes i get two operations and two sound files a split second apart, makes it sound liek an echo... I think this will only get worse with the use of relays. Hysteresis the on and off state of the contact while i press the button.

I have looked at a few artciules and the BPIOZERO info site: -

viewtopic.php?f=29&t=133740

https://gpiozero.readthedocs.io/en/stab ... Pin.bounce

And i see i can use bouncetime = 5 where 5 = 5 ms and the button will activate for the first operation but will ignore further 'bounces' for 5ms.

Just not sure where i use it in my code? In this stripped down version below, would this be correct use?

Code: Select all

import os
from gpiozero import Button
import subprocess
import time
import random

button1 = Button(26) # pin 37 Start Relay - MM

start_music    = '/home/pi/rodeo/audio/start.wav'

while True:
    time.sleep(0.1)
    if button1.is_pressed:
        # play start_music
        os.system("mplayer " + start_music, bouncetime=5)
Thnaks

JB

gordon77
Posts: 6883
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pi Powerd Audio for EM Rifle Game - Rodeo Rebooted

Fri Sep 23, 2022 6:32 pm

I think you need

button_1 = Button(26, bounce_time = 0.2)

Not the bounce_time in the mplayer statement

https://gpiozero.readthedocs.io/en/stab ... input.html

Biffboff
Posts: 57
Joined: Sat Mar 15, 2014 6:38 pm

Re: Pi Powerd Audio for EM Rifle Game - Rodeo Rebooted

Mon Sep 26, 2022 6:24 pm

Hmm, that just made it worse. Reading more into this and i think i need to use Pull up or down resistors as well, so the switch is not in a floating open state

Will try again in a bit, sigh :-)

Thanks

JB

gordon77
Posts: 6883
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pi Powerd Audio for EM Rifle Game - Rodeo Rebooted

Mon Sep 26, 2022 6:30 pm

Have you tried pullup in the config ?

gpiozero.Button(pin, *, pull_up=True, active_state=None, bounce_time=None, hold_time=1, hold_repeat=False, pin_factory=None)

Biffboff
Posts: 57
Joined: Sat Mar 15, 2014 6:38 pm

Re: Pi Powerd Audio for EM Rifle Game - Rodeo Rebooted

Mon Sep 26, 2022 8:03 pm

I went back and had another check through the code. Sorted it :-).

A friend and i were playing around with the code to make a start n MV02. He had some ideas about functions, i think...

Anyway he remarked out a lot of the code and when he started to try creating threads, daemons and the like he started to get debounce issues.

Longer story a bit shorter, he still he #out the following: -
time.sleep(0.1)

Without this, i get at least three triggers per button press.
When i put it in wit no new debounce code, the duplication occurs sometimes, if I set the debounce to .3 or .5 and e time.sleep, i have no issues :-)

So the lack of sleep function, making it call several times when I press the button? I thought all it did was setup a little pause when running the program? I assumed start to end, wait 0.1, start to end, wait 0.1 etc etc? Or have I just answered my own question? The pi cycles through my program about three times during a normal button press, hence why it fires three times. The 0.1 is enough to slow the programme so it only runs once during a button press maybe?

Code: Select all

# Rodeo Rebooted POC V 0.02 Some Rights almost reserved...

import os
from gpiozero import Button
import subprocess
import time
import random


# set volume
volume = 100
os.system("amixer -D pulse sset Master " + str(volume) + "%")


Start = Button(26, bounce_time = 0.5) # pin 37 Start Relay - MM
Trigger = Button(19, bounce_time = 0.5) # pin 35 Trigger Relay - MM
MoveingTarget = Button(13, bounce_time = 0.3) # pin 33 Moving Target Hit - MM
FlashBullHedCtrl = Button(6, bounce_time = 0.3)  # pin 31 Flashing Bull Head Ctrl - ON
FlashBullHedHit = Button(21, bounce_time = 0.3) # pin 40 Flashing Bull Head Hit - MM
DropTrgt1 = Button(20, bounce_time = 0.3) # pin 38 Drop Target 1 Relay - MM
DropTrgt2 = Button(16, bounce_time = 0.3) # pin 36 Drop Traget 2 Relay - MM
DropTrgt3 = Button(12, bounce_time = 0.3) # pin 32 Drop Target 3 Relay - MM
DropTrgt4 = Button(7, bounce_time = 0.3)  # pin 26 Drop Target 4 Relay - MM
BullseyeFlash = Button(8, bounce_time = 0.3) # pin 24 Bullseye Flashing Relay - ON
BullseyeHit = Button(25, bounce_time = 0.3) # pin 22 Bullseye Hit Relay - MM
XtendedPlay = Button(24, bounce_time = 0.3) # pin 18 Extended Play Relay - MM
Cheat = Button(23, bounce_time = 0.3) # pin 16 Anti Cheat Relay - ON
GameOver = Button(18, bounce_time = 0.5) # pin 12 Game Over Relay - ON
AdultOnOff = Button(15, bounce_time = 0.3) # pin 10 Adult Mode - MM
#ShutDown = Button(14, bounce_time = 0.3) # pin 8 Shutdown Pi - MM


Start_music    = '/home/pi/rodeo/audio/intro.ogg'
Bgrnd_music     = ['/home/pi/rodeo/audio/background1.ogg','/home/pi/rodeo/audio/background2.ogg','/home/pi/rodeo/audio/background3.ogg']
Trigger_sounds = '/home/pi/rodeo/audio/trigger.wav'
MoveingTarget_sounds = ['/home/pi/rodeo/audio/ricochet1.wav','/home/pi/rodeo/audio/ricochet2.wav','/home/pi/rodeo/audio/ricochet3.wav','/home/pi/rodeo/audio/ricochet4.wav','/home/pi/rodeo/audio/ricochet5.wav','/home/pi/rodeo/audio/ricochet6.wav','/home/pi/rodeo/audio/ricochet7.wav']
FlashBullHedCtrl_sounds = '/home/pi/rodeo/audio/flashbullhd2.wav'
FlashBullHedHit_sounds = '/home/pi/rodeo/audio/bullhit.wav'
DropTrgt1_sounds = '/home/pi/rodeo/audio/droptrgtcowboy1.wav'
DropTrgt2_sounds = '/home/pi/rodeo/audio/droptrgtcowboy2.wav'
DropTrgt3_sounds = '/home/pi/rodeo/audio/droptrgtcowboy3.wav'
DropTrgt4_sounds = '/home/pi/rodeo/audio/droptrgtcowboy4.wav'
BullseyeFlash_sounds = '/home/pi/rodeo/audio/bullseyehurryup.wav'
BullseyeHit_sounds = '/home/pi/rodeo/audio/bulseyehit.wav'
XtendedPlay_sounds = '/home/pi/rodeo/audio/extendedplay.wav'
Cheat_sounds = ['/home/pi/rodeo/audio/cheat1.wav','/home/pi/rodeo/audio/cheat2.wav']
GameOver_sounds = ['/home/pi/rodeo/audio/gameover1.wav','/home/pi/rodeo/audio/gameover2.wav','/home/pi/rodeo/audio/gameover3.ogg']
AdultOnOff_sounds = '/home/pi/rodeo/audio/adultmodeon.wav'
ShutDown_sounds = '/home/pi/rodeo/audio/shutdown.wav'


    
while True:
    time.sleep(0.1)
    
    
    if Start.is_pressed:
        S1 = subprocess.Popen(["mplayer",Start_music], shell=False)
                
        
    if Trigger.is_pressed:
        S2 = subprocess.Popen(["mplayer",Trigger_sounds], shell=False)
                    # play game and check inputs
        
    if MoveingTarget.is_pressed:
        S3 = subprocess.Popen(["mplayer",MoveingTarget_sounds[random.randint(0,6)]], shell=False)
        
    if FlashBullHedCtrl.is_pressed:
        S4 = subprocess.Popen(["mplayer",FlashBullHedCtrl_sounds], shell=False)
        
    if FlashBullHedHit.is_pressed:
        S5 = subprocess.Popen(["mplayer",FlashBullHedHit_sounds], shell=False)

    if DropTrgt1.is_pressed:
        S6 = subprocess.Popen(["mplayer",DropTrgt1_sounds], shell=False)
        
    if DropTrgt2.is_pressed:
        S7 = subprocess.Popen(["mplayer",DropTrgt2_sounds], shell=False)
        
    if DropTrgt3.is_pressed:
        S8 = subprocess.Popen(["mplayer",DropTrgt3_sounds], shell=False)
        
    if DropTrgt4.is_pressed:
        S9 = subprocess.Popen(["mplayer",DropTrgt4_sounds], shell=False)       
    
    if BullseyeFlash.is_pressed:
        S10 = subprocess.Popen(["mplayer",BullseyeFlash_sounds], shell=False)           
        
    if BullseyeHit.is_pressed:
        S11 = subprocess.Popen(["mplayer",BullseyeHit_sounds], shell=False)        
        
    if XtendedPlay.is_pressed:
        S12 = subprocess.Popen(["mplayer",XtendedPlay_sounds], shell=False)
         
    if Cheat.is_pressed:
        S13 = subprocess.Popen(["mplayer",Cheat_sounds[random.randint(0,1)]], shell=False)      
            
    if GameOver.is_pressed:
        S14 = subprocess.Popen(["mplayer",GameOver_sounds[random.randint(0,2)]], shell=False)





        


    
                #os.system("mplayer " + attract_sounds[random.randint(0,4)])
                #time.sleep(6) =

gordon77
Posts: 6883
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pi Powerd Audio for EM Rifle Game - Rodeo Rebooted

Mon Sep 26, 2022 8:21 pm

The 0.1 is just a short delay , it keeps the cpu load lower, it may go to 100% without it when just going around the loop with nothing happening.
You could try removing it.

ame
Posts: 6059
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Pi Powerd Audio for EM Rifle Game - Rodeo Rebooted

Mon Sep 26, 2022 8:36 pm

Look at your code. Every time around the loop it looks at the current state of each button.

Imagine you held down a button. Every time the loop goes round it will perform the action for that button (try it!).

The problem is that you are assuming no-one will hold down the button. You are also assuming that any button press will be short. So you've created a bit of a problem for yourself. Your loop has to execute fast enough to catch any button press, no matter how brief, and slow enough not to cause any repeats.

I can think of two solutions. One might be to use callback functions on button-press events. The other might be to have a "lock-out" flag for each button. When a button is pressed, if the flag is clear then the action is performed and the flag is set. The flag is only cleared when the button is released (after some debounce time).
Hmm. What can I put here?

gordon77
Posts: 6883
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pi Powerd Audio for EM Rifle Game - Rodeo Rebooted

Mon Sep 26, 2022 8:49 pm

The OP isn't using buttons, it's relays in a game. So l don't imagine them held down.

ame
Posts: 6059
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Pi Powerd Audio for EM Rifle Game - Rodeo Rebooted

Mon Sep 26, 2022 8:56 pm

gordon77 wrote:
Mon Sep 26, 2022 8:49 pm
The OP isn't using buttons, it's relays in a game. So l don't imagine them held down.
I've heard that relays can be turned on for several seconds sometimes. Maybe longer as technology improves.
Hmm. What can I put here?

Biffboff
Posts: 57
Joined: Sat Mar 15, 2014 6:38 pm

Re: Pi Powerd Audio for EM Rifle Game - Rodeo Rebooted

Tue Sep 27, 2022 12:19 pm

Thanks for confirming my thoughts and the explanation.

You are both right :-). But this version of the code is just MVO1. Later MVOs will take account of duration or MM or Momentary Make verses being helped on for periods longer than say a button press. Hence the MM at the end of each commented button in code, as a reminder for me to sort that out later :-)

Just trying not to boil the ocean and instead breaking the development down, will worry about those issues later when i have created the prototype PCB to house the relays, ribbon connection and Jones connections (Jones is name for the Electro Mechanical plug type) used buy the games of that period.

Thanks

JB

Return to “Gaming”