Sending multiple triggers in one routine using serial port

Hi @Kimberley_Dundas ,
I replied to your post right after but for some reason it wasn’t sent! I have been working on it and it is not working yet!!

The reason is that I don’t know how to analyze my data then with the combined triggers. The output file in MATLAB (S matrix) would have nine columns for the nine triggers that have been sent but I’m not sure how I can know which column is for which: Is column 1, for 234 or 253 for example? as I play the videos randomly. Please let me know if I wasn’t clear!

I wonder if there is a way that I can send the triggers with some delays between them?
I tried adding a text and define the third trigger with that. but now the it repeats the first two triggers for millions of times till the text starts (e.g., 2424242424242424242424…7 instead of 247). Then I adjust the timing of the text to start quickly after the video inset (.0000000001), and now it repeats less and sends 24247.



Many thanks,
Zohreh

Hi @zsoleimani,

To add a delay between the triggers you could add if t > 0.001: (for example) at the start of your trigger code. Then indent everything else so that the triggers are only sent when the time is greater than 0.01s.

The reason you’re getting the ‘242424…’ is because you need to copy the code on lines 19-21 and repeat them after line 9 (so that they’re contained within your first if statement). What’s happening is that your video trigger is never being told to end so it’s just repeatedly sending the triggers on every frame until the end of the routine.

Hope that helps,

Kim

Hi Kim,

Thanks for your help!
If I add line 19-21 after line 9, it quits there and doesn’t run the next if (i.e.; if trigA.status == STARTED and not pulse_started). because of this I had to remove those lines. How can I get it to continue running the rest of the codes?

Re your suggestion to add t, do I need to define t in the begin experiment tab? like: timer = core.Clock() then in each frame tab:
if timer.getTime() >= 0
if SetAVideo.status == STARTED and not pulse_started:
if Actor_Trigger==2:
port.write(str.encode(‘2’))
elif Actor_Trigger==3:
port.write(str.encode(‘3’))
if Velance_Trigger==4:
port.write(str.encode(‘4’))
elif Velance_Trigger==5:
port.write(str.encode(‘5’))
if timer.getTime() > 0.001
if trigA.status == STARTED and not pulse_started:
if View_Ind_Trigger==6:
port.write(str.encode(‘6’))
elif View_Ind_Trigger==7:
port.write(str.encode(‘7’))
elif View_Ind_Trigger==8:
port.write(str.encode(‘8’))
elif View_Ind_Trigger==9:
port.write(str.encode(‘9’))
pulse_start_time = globalClock.getTime()
pulse_started = True
pulse_ended = False
if pulse_started and not pulse_ended:
if globalClock.getTime() - pulse_start_time >= 0.005:
port.write(str.encode(‘0’))
pulse_ended = True

Is this what you meant?
Does this stop from repeating as well?

Thanks so much,
Zohreh

Hi @zsoleimani,

Apologies for the late response here - could you please paste your Each Frame code in a code block so that I can edit it and you can paste it back in to your experiment? You can do this using the </> icon or surrounding the code with 3 backticks (```).

As for defining t, no you don’t need to. t is a variable that indicates the time since the start of the current routine.

Thanks,

Kim

Hi Kim,

No problem!
I finally figured how to send multiple triggers with some delays in between. but there is still an issue: the order that Psychopy send triggers does not match with the order of triggers in the output s matrix in MATLAB.
here is my code:
begin routine tab:

pulse_started = False
pulse_ended = False
trigger_sent = False
trigger_clock=core.Clock()

Each frame tab:

if not trigger_sent and trigger_clock.getTime() >= 1:
    if Actor_Trigger==2:
       port.write(str.encode('2'))
    else:
    #Actor_Trigger==3:
         port.write(str.encode('3'))
    trigger_sent =True

if trigBB.status == STARTED and not pulse_started:
    if Velance_Trigger==4:
       port.write(str.encode('4'))
    else:
        #Velance_Trigger==5:
         port.write(str.encode('5'))    
    trigger_sent =True

if trigB.status == STARTED and not pulse_started:
    if View_Ind_Trigger==6:
       port.write(str.encode('6'))
    elif View_Ind_Trigger==7:
         port.write(str.encode('7'))
    elif View_Ind_Trigger==8:
         port.write(str.encode('8'))
    elif View_Ind_Trigger==9:
         port.write(str.encode('9'))
    pulse_start_time = globalClock.getTime()
    pulse_started = True 
    pulse_ended = False
if pulse_started and not pulse_ended:
        if globalClock.getTime() - pulse_start_time >= 0.005:
            #port.write(str.encode(chr(0)))
            pulse_ended = True

so the order of sending triggers is : 2,3, 4, 5, 6, 7, 8, 9. but in the s matrix of MATLAB, the order of columns is changing by each participants (as the videos are picked up randomly for each participants):
so it can be 2,5,6,3,4,7,8,9 for participant A and 3,4,7,2,5,8,7,9 for participant B depending which videos pulled on. I wonder if there is a way to define the trigger matrix at the very beginning to make the columns of triggers’ matrix constant.If that make sense! I probably didn’t explain it well, please let me know if you have any questions.

Thanks so much,
Zohreh