Sending trigger based on key_resp.corr not working

I have a psychopy builder task and I would like to send a trigger for correct and incorrect responses based on the key_resp.corr variable.

What I’ve done:
I have added a code component in the specific routine and put the code to send the trigger. Because there is another trigger straight after the key press i have introduced a short pause after the trigger is sent so that both the key press trigger sends and the fixation cross following it sends. The code below works using key_resp.keys (either left or right) meaning the code works and sends the trigger but not with the key_resp.corr variable.

Please let me know if you know what might be the issue.

I have used key_resp.corr in previous code to skip routines therefore Its strange it doesnt work here.

Begin experiment:
from psychopy import event, core

Begin routine:

stimulus_pulse_corr_started = False
stimulus_pulse_corr_ended = False
 
pause_corr_ended = False

Each frame:

if key_resp_10.corr == 1 and not stimulus_pulse_corr_started:
    win.callOnFlip(port.write,[22])#Send the trigger, synced to the screen refresh
    stimulus_pulse_corr_start_time = globalClock.getTime()
    stimulus_pulse_corr_started  = True 
 
if stimulus_pulse_corr_started and not stimulus_pulse_corr_ended:
    if globalClock.getTime() - stimulus_pulse_corr_start_time >= 0.005:
        win.callOnFlip(send_triggers, [0])
        stimulus_pulse_corr_ended = True

if stimulus_pulse_corr_started and stimulus_pulse_corr_ended and not pause_corr_ended:
    core.wait(0.01)
    pause_corr_ended = True
if pause_corr_ended:
    continueRoutine = False

Hello @radi

That is strange indeed. Add some print-commands, e.g.

print(key_resp_10.corr)

aso. to determine why your if-construction is not evaluated.

Best wishers Jens

Hello,

We are doing this at the moment and it does return the response within each frame. But only after we exit the task so not in real time.

Hello

When you run the experiment, make sure you delete the print command. From your post, I gathered that the if-statement involving key_resp10.corr is not being evaluated. I therefore suggested checking the values of the relevant variables. Have I understood you correctly?

I guess that you could simplify your code to

if key_resp_10.corr:
    win.callOnFlip(port.write,[22])#Send the trigger, synced to the screen refresh
    core.wait(0.005) # this is really short
    win.callOnFlip(send_triggers, [0])
    core.wait(0.01)
    continueRoutine = False

I am not sure how “well” the Builder copes with the addition core.wait and win.callOnFlip.

Best wishes Jens

in your if statement do we not want to specify if key_resp_10.corr = 1 or 0 and then send the trigger?

Hello @radi

Commands following the ‘if’ construction are only executed when the ‘if’ construction evaluates as true. This only occurs when the pressed key corresponds with the correct one. Otherwise, the if-construction evaluates as false and the commands are not executed.

The code you posted does not deal with incorrect answers.

if key_resp10.corr:

is the same as

if key_resp10.corr == 1:

Best wishes Jens

So if we want to it to send a trigger when they press an incorrect response we wouldn’t be able to because key_resp_10.corr is always 0 until its 1 so it will send it before the key press?

The reason we haven’t done key_resp_10.keys is because the location of the words on screen (old and new) swap depending on the trial and therefore correct answer depends on the screen location not on old always being ‘left’ and new always being ‘right’

Hello @radi

What is send before the key press? The trigger? No, currently the trigger is only send when the answer is correct. In addition, currently the trial only ends when the correct key is pressed!

I do not understand this. Do you use a column in your condition file to define the correct key? If so, I do not see the problem.

Best wishes Jens

we had a separate code element with if key_resp_10.corr == 0 send a different trigger, and that trigger would send at the beginning of the routine before the key press, which is weird because it has not been defined yet

we do have a column in the condition file, which defines if the correct position is either left or right, but if the correct answer is old and the word old is located on the right side, the correct position is right, but if its located on the left, the correct position is left. Therefore, if we just send a trigger for key press left or right, it doesnt tell us if they got it correct or incorrect. This is why we wanted to use the key_press_10.corr variable instead but it doesn’t seem to behave the same way.

Hello @radi

Ok, I begin to understand your experiment. Here is some updated code

if key_resp_10.keys:                #a response was given 
    if key_resp_10.corr:                    #correct response given
        win.callOnFlip(port.write,[22])   #Send the trigger, synced to the screen refresh
        core.wait(0.005)                       # this is really short, half a millisecond
        win.callOnFlip(send_triggers, [0])
        core.wait(0.01)
        continueRoutine = False
    else:                                               # incorrect response given
        win.callOnFlip(port.write,[??])   #Send the trigger, synced to the screen refresh
        core.wait(0.005)                        # this is really short, half a millisecond
        win.callOnFlip(send_triggers, [0])
        core.wait(0.01)
        continueRoutine = False

So, participants have to indicate the position of the old world, which can either be left or right? Do you use the cursor keys, left and right?

You want to continue to the next trial when a response was given, correct?

Best wishes Jens

Hello

I tried this code and a trigger doesn’t send.

Hello @radi

OK, instead of sending a trigger, just present different words or symbols for when the response is correct and when it is incorrect. I want to eliminate the possibility of a problem with the trigger.

Best wishes Jens


i tried this but did 1 second to make sure i could see the image and it was pausing but the image didnt come up.

i did the same image just to see if any response was being shown