Problem with simple button press experiment

Hello,

I have a very simple experiment that I’ve managed not to make work… The experiment should play a beep sound each time the space button is pressed for duration of 1 second and pressing + should increase volume by 10% and pressing - should decrease volume by 10%. However, my code only play the beep sound only once :confused:

Here is example of my code

#Begin experiment

import serial
from psychopy import sound
import serial


beep = sound.Sound('test_100_200Hz.wav', name='$beep', secs = 1)
volume_level = 0.1

beep.setVolume(volume_level)
timer = core.Clock()


port_trigger_eeg = serial.Serial('COM5')
port_trigger_eeg.write([0x00])

#begin routine

beepOnsets=[]
beepISIs=[]

#Each frame

if event.getKeys(keyList = ['space']):
  beep.play()
  timer = core.Clock()
  port_trigger_eeg.write([0x01])
elif timer.getTime() > 1 and not event.getKeys():
  beep.stop()
  port_trigger_eeg.write([0x00])
  


if event.getKeys(keyList = ['+']):
    beep.setVolume(volume_level+0.1)
    
if event.getKeys(keyList = ['-']):
    beep.setVolume(volume_level-0.1)   

#End routine

thisExp.addData('beepISIs', beepISIs)
thisExp.addData('beepOnsets', beepOnsets)

Any help would be greatly appreciated.

Try

#begin routine 
beeping = False

#Each frame

if timer.getTime() > 1 and beeping == True:
  beep.stop()
  beeping = False
  port_trigger_eeg.write([0x00])
  
keys = event.getKeys()
if 'space' in keys:
    beep.play()
    beeping = True
    timer.reset()
    port_trigger_eeg.write([0x01])
elif 'plus' in keys:
    volume_level += .1
    beep.setVolume(volume_level)
elif 'minus' in keys:    
    volume_level -= .1
    beep.setVolume(volume_level)

I get error

if timer.GetTime() > 1 and beeping == True:

AttributeError: ‘Clock’ object has no attribute ‘GetTime’

I wrote timer.getTime()
You wrote timer.GetTime()

Thank you silly mistake on my part!

Now it runs but for some reason pressing the space button more than once makes the sound become quieter and kind buggy. Not sure why


if timer.getTime() > 1 and beeping == True:
    beep.stop()
    beeping = False
    port_trigger_eeg.write([0x00])
    
keys = event.getKeys()
if 'space' in keys and beeping ==False:
    beep.play()
    beeping = True
    timer.reset()
    port_trigger_eeg.write([0x01])
    
elif 'plus' in keys:
     volume_level+=.1
     beep.setVolume(volume_level)

elif 'minus' in keys:
      volume_level -=.1
      beep.setVolume(volume_level)

This is very bizarre behavior.

I’ve even commented out the plus and minus codes and the beep sound still decreases with each consecutive space bar press. Is this a psychopy bug?

@Michael @wakecarter I would greatly appreciate any input

I ‘fixed’ it by making a huge continuous beep sound (2 hours long) and by pressing space bar it now unmutes the sound for 1 second then mutes it back up. Its a temporary fix that works I guess. I still have no idea why it was behaving oddly when restarting beep sound