Word by word sentence presentation with parallel port output

This is because you have blank cells in the last row of your conditions file. i.e. there is no valid content for the sentence on the last row of the file for the final iteration of the loop.

On the trigger front, your current code is this:

if currentWordIndex == trigger_w:
      p_port.setData(cond)
      core.wait(0.001)
      p_port.setData(0)

Note that this code runs on every frame (typically at 60 Hz), so your signal will actually be sent multiple times during the presentation of the word. Also, we should generally avoid using core.wait() in Builder scripts, it can muck up the timing (plus this is only waiting 1 ms, which is too short for your desired pulse duration of 20 ms). But also note that because we are stuck within Builder’s 60 Hz event loop, we can’t easily achieve a pulse width of exactly 20 ms, as this code runs only at a granularity of 16.7 ms intervals. So we will add a check to end the pulse if at least 20 ms have elapsed…

First, you need to have some code in the Begin routine tab so that we set things up so the pulse is only sent oncer per routine, and to catch any edge cases where perhaps the trial ended before we had the chance to terminate the pulse:

In the Begin Routine tab:

pulse_sent = False # keep track of whether a signal has been sent yet
# tidy any that may have not been terminated properly:
p_port.setData(0) 
pulse_terminated = True

In the Each Frame tab:

# send a signal at the beginning of the appropriate word:
if currentWordIndex == trigger_w:
    if not pulse_sent:
        p_port.setData(cond)
        pulse_start_time = t
        pulse_sent = True
        pulse_terminated = False

# terminate the signal at least 20 ms later:
if pulse_sent and not pulse_terminated:
    if t - pulse_start_time >= 0.020:
        p_port.setData(0)
        pulse_terminated = True

Hope that helps.

1 Like