Send trigger (biosemi actiview) for every key press

What is the custom code to send a trigger for key press ? I need a trigger code to be sent for every left and right key pressed for 10s.

So far I have this in each frame tab:

if len(key_resp_2.keys)>=0 and not trigger:
   port.write(?)
   trigger=True 

Are you sending a trigger via the parallel port or the serial port?

Also do you want the trigger to last 10 seconds
a trigger to be sent once the key has been pressed for 10 seconds
or a trigger to be sent for every keypress made within a 10 second time period (one per keypress)

Hi @Becca ,

It is a serial port (windows).
I just need the trigger to be sent every time the key is pressed. It does not have to last for 10s. Just need the trigger to be noted on actiview when the subject pushes down on the button.

Currently, I need the subject to push down the left and right key alternatively as fast as they can for 10s duration. So every time they push these keys , I need the trigger to be sent to actiview within that 10s period.

OK, you want to follow roughly the guidance in this post But some tweaks.

Add a code component.

In the Begin Experiment tab:

import serial
port = serial.Serial('COM1')# where COM1 is the address of your port

In the Begin Routine tab

CountedKeys = 0

In the Each frame tab

if len(key_resp.keys) > CountedKeys:
    CountedKeys += 1 # increase the keys that were counted
    port.write([0x01]) # sent the trigger

Make sure you have a keyboard component that is called “key_resp” where Data > Store data is set to “All Keys”

Please do feed back if that works.

Hope this helps,
Becca

Actually another simpler way might be that if the keypress ends the routine to just use

port.write([0x01]) in the End Routine tab (rather than using the code suggested for each frame)

That might also have better timing, because the check for keypress responses wouldn’t be tied to window refresh

An alternate approach is to use an RB-740 response with an “m-pod for Biosemi”. Output from the response pad goes to PsychoPy via USB and to BioSemi via TTL simultaneously. This offers a couple of advantages:

  • It’s more precise because it’s all done in hardware. With a keyboard, you are looking at about ±10ms jitter, and additional jitter if you are using a USB-serial port adapter.

  • You can set the duration of the output pulse if you like. By default, the duration of the pulse is how long the key was pressed. This gives you both onset and offset of key press.

  • No need to code for producing output.

With this setup, you can also add a light sensor to detect the precise onset of a visual stimulus.

See this diagram:
http://cedrus.com/m-pod/pics/m-pod-diagram.png

Thanks @Becca !! That worked when port.write([0x01] was in each frame and not in the end routine.

To follow up on this, I am also trying to send triggers of sound stimulus at the same as keys pressed. I have designed a task where I am playing certain sounds. While the sounds are played intermittently, the subject has to continuously press the left and right key alternatively, as fast they can. The sound is independent of the key press but they are occuring in the same task. This is the code I have, but the key press trigger only appears when the sound is played. I do want the triggers of the key press to appear even during the little gaps or pauses between the sounds.

Begin experimental tab:

import serial
port = serial.Serial('COM4')

Begin routine:

trigger=False
CountedKeys=0

In each frame tab:

if t>=0.1 and not trigger:
    port.write(str.encode(chr(stim)))
    trigger=True
if len(key_resp.keys) > CountedKeys:
    CountedKeys += 1 # increase the keys that were counted
    port.write([0x01])

Hi Apoorva,

Marvelous, so pleased that fixed the first issue!

If I understand this is a dual task where task 1 is pressing the left and right keys alternately and task 2 involves the presentation of sounds? Is that right? and you want triggers for both the key presses and the sounds?

If so I think you would want something a little like this
serial_port_trigs_dual.psyexp (14.1 KB)

So, there are two code components. One called “key_triggers” (which is the code you already have for your key presses) and one is “stim_triggers” ( a new one for your sound onset triggers). In the new one you want something like this in the Begin Routine:

sounds = [sound_1, sound_2, sound_3] # if you have 3 sound components called "sound_1" "sound_2" and "sound_3"
for sound in sound:
    sound.trigger_sent = False

Then in the Each frame something like:

for sound in sound:
    if sound.status == STARTED and not sound.trigger_sent :
        win.callOnFlip(port.write([0x01]))
        sound.trigger_sent  = True # Mark that this sound has been marked with a trigger, we don't need another for it...

Please do give that a go and let me know if it works.

As with anything EEG related, once you have your triggers set up we really recommend you check the triggers are as you expect with some hardware your side if you can! But let’s focus on getting this sorted first,

Becca

Hi @Becca ,

I am little confused. The code I sent earlier was for the sound stimulus (where stim is the variable I have assigned in the excel sheet containing the list of sounds).

if t>=0.1 and not trigger:
    port.write(str.encode(chr(stim)))
    trigger=True

I have a list of sounds to play which I have on the excel sheet. I have looped those to play for a certain number of trials. During the trials, I also need the subject to press the left and right keys. With the new code you have sent, would that not be redundant to what I already have ?

Also, if I were to use the new code and discard the code I have mentioned above , I get the error that it is not iterable .

I have attached a file if that helps.

dual_task.psyexp (12.6 KB)
soundlist_1s.xlsx (8.1 KB)

UPDATE:

The code worked fine and I got the triggers to be working they I wanted it. I just used the code I sent in the file and that seemed to work.

Thanks a bunch!!!