Sending and saving 7T fMRI Triggers via USB connected NNL SyncBox on every TR and Response Keypress

Hi all,

I would like to get synchronised timings and number of triggers for each single pulse sent by the 7T MRI scanner together with each single SPACEBAR keypress as a participant’s response when particular letters (A, B, C, X, Y) are presented on the screen, also the stimulus start time with each corresponding trigger.
So far I have added a “Waiting for scanner” routine where I get the first pulse via USB connected NNL Synbox saved in the .csv data file. Triggers are automatically emulated as letter ‘s’ sent by the NNL Synbox.

# Code component: Creat routine "Wait for Scanner"

text_wait = visual.TextStim(win=win, name='text_wait',
    text='Waiting for scanner...',
    font='Arial',
    pos=(0, 0), height=0.8, wrapWidth=None, ori=0,
    color='white', colorSpace='rgb', opacity=1, 
    languageStyle='LTR',
    depth=0.0);

#Set first MR Trigger
mr_trigger_number = []                                                       # list for triggers
mr_trigger_time = []                                                            # list for time of each trigger 
ScannerKeyboard = keyboard.Keyboard()                                           # set keyboard 
count_mr_trigger = 0                                                            # counts amount of MR scanner trigger, starts with zero
start_trigger = ScannerKeyboard.getKeys(keyList=['s'], waitRelease = False)     # experiment starts with MR Scanner "s" 

# Draw waiting Screen until "s" is sent from NNL Syncbox
while len(start_trigger) == 0:                                                  # if no trigger was sent show:
    text_wait.draw()                                                            # Text "Waiting for scanner" 
    win.flip()
    start_trigger = ScannerKeyboard.getKeys(keyList=['s'], waitRelease = False) # show text_wait until one "s" was received

# Get timing information and store trigger number
onset = core.getTime()                                                          # get Onset time of trigger
count_mr_trigger = count_mr_trigger + 1                                         # count MR Scanner trigger, add "1!
# Save time and number of MR Trigger
thisExp.addData('MR_trigger_time',onset)                          # save trigger time of onset
thisExp.addData('MR_trigger_number',count_mr_trigger)

And the following code component in > Each Frame tab of 2 trials conditions looking at a previous post to save the stimuli onset timings. This works fine but stimulus start time > stimulus_start_time = globalClock.getTime() - mr_trigger_time is saved based on first pulse only as I am unable to save each pulse timings.

#save stimulus onset time for two types of trial conditions 
if trials.thisN == 0 and frameN == 0: 
    loop_start_time = globalClock.getTime() - mr_trigger_time 
elif frameN == 1: 
    stimulus_start_time = globalClock.getTime() - mr_trigger_time 
    # store the data:
    thisExp.addData('stimulus_start_time', stimulus_start_time)

if trials_2.thisN == 0 and frameN == 0: 
    loop_start_time = globalClock.getTime() - mr_trigger_time 
elif frameN == 1: 
    stimulus_start_time = globalClock.getTime() - mr_trigger_time 
    # store the data:
    thisExp.addData('stimulus_start_time', stimulus_start_time)

However, I want to record and save timings of each trigger pulse when a SPACEBAR response is pressed on a target letter (stimuli) by a participant, to compare the timings with accuracy and reaction times of the task with each corresponding trigger timings.

I would really appreciate your valuable suggestions to get the desired output as I am quite new to the PsychoPy.

Thanks in advance!

Best,
Ryz