Modifying the code for sending triggers


I am using the following code for sending the triggers to my EEG computer in my experiment (the code is suggested by PsychoPy)

if stimulus.status == STARTED and not stimulus_pulse_started: #Change ‘stimulus’ to match the name of the component that you want to send the trigger for
win.callOnFlip(port.write, str.encode(‘1’))
stimulus_pulse_start_time = globalClock.getTime()
stimulus_pulse_started = True

if stimulus_pulse_started and not stimulus_pulse_ended:
if globalClock.getTime() - stimulus_pulse_start_time >= 0.005:
win.callOnFlip(port.write, str.encode(‘0’))
stimulus_pulse_ended = True

This sends a ‘1’ as a trigger marker with my stimuli. However, it is said in the information that you can change the ‘1’ to whatever value is in your conditions file (xlxs). I have a condition called ‘trigger’ in my conditions file which I am using to pull stimuli from. How can I change the ‘1’ to the value I defined as ‘trigger’ in my conditions file in this code? The PsychoPy website says you can do it, but does not clarify how. My suspicion is that I should change this part:

win.callOnFlip(port.write, str.encode(‘1’))

Can anyone help please? Thank you all in advance.

Hi @melisvuslat,

Thanks for pointing this out!

To change the code to use the trigger variable, you just need to change the '1' to trigger so that line will read:

win.callOnFlip(port.write, str.encode(trigger))

We’ll update the documentation to give this example too :slight_smile:

Just FYI too, if you’re using a recent version of PsychoPy you can also use the Serial Port component to remove the need for a code component at all.