EEG Response code via serial port

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10):
PsychoPy version (e.g. 2024.2.4 Py 3.10):
Standard Standalone Installation? (y/n) y
Do you want it to also run online? (y/n) n
What are you trying to achieve?:

I want to send different triggers via the serial port depending on the key the participant has pressed.

I following code in a Each frame tab

if key_respFrage.keys == 'j':
    code = 74
elif key_respFrage.keys == 'f':
    code = 70

I checked the if-clause with a print command and it prints out the proper values.

The routine is set up as follows

and the serial port has a conditional start.

What did you try to make it work?:

I tried different start conditions/times.

What specifically went wrong when you tried that?:

I was never able to send a port code. Notice, the triggers work in the rest of the experiment but just the response encoding does not work.

Ideas and suggestions are welcome.

Best wishes Jens

Have you tried adding a text component with the same start condition to confirm that aspect?

Does the serialPortResp send a trigger if you give it a fixed time (e.g. 0.7s) for it’s start?

Is the issue with $chr(code)? Try adding that to the text component test.

I would also suspect $chr(code) to be the problem because in your case, it will result in the characters J and F as Start data, respectively, while numbers are expected. Have you tried simply using $code instead?

Alternatively, perhaps you could simplify the routine by removing your code in the “Each frame” tab and use the following Start data, which would directly convert the response keys to your trigger codes: $ord(key_respFrage.keys)

Hello @TorgeMS

Thanks a lot four your advice using $code directly. At a different place in the experiment I send trigger code using $chr(code) and that works. But I will happily give it a try.

Best wishes Jens

Hello

I think I now know what the problem. It results from the combination of key_respFrage.status == FINISHED and the duration of key_respFrage.

I made a little toy experiment at home. When I do not specify a duration for key_respFrage and make the appearance of a text component conditional from key_respFrage.status == PRESSED, the toy program works as intended. However, I can not set the keyboard component to end the routine. Any suggestions how can I end the routine after a response has given and a trigger has been send?

Begin Experiment

keyPressed = False
keyCode = 0

Each frame

if key_resp.keys:
    keyPressed = True
    if key_resp.keys == 'j':
        keyCode = ord(key_resp.keys.upper())
    else:
        keyCode = ord(key_resp.keys.upper())

End routine

keyPressed = False
keyCode = 0

So, how do I end a routine when the keyboard component can not be used and I need to send a trigger?

Best wishes Jens

Begin Experiment

keyPressed = False
keyCode = 0

Each frame

if key_resp.keys:
    if not keyPressed:
         keyPressed = t
         if key_resp.keys == 'j':
             keyCode = ord(key_resp.keys.upper())
         else:
             keyCode = ord(key_resp.keys.upper())
    elif t > keyPressed + .5:
          continueRoutine = False

End routine

keyPressed = False
keyCcode = 0
1 Like

Hello @wakecarter

Thanks a lot. That works in the toy experiment. I have to check it when sending a trigger. I took out some redundant code.

Each frame

if key_resp.keys:
    if not keyPressed:
        keyPressed = True
        keyCode = ord(key_resp.keys.upper())
    elif t > keyPressed + .5:
        continueRoutine = False

I also set the value of keyCodeto None instead of setting it to 0.

keyCode = None

Best wishes Jens