# How to send triggers using the parPulse function?

**URL:** <https://psychtoolbox.discourse.group/t/how-to-send-triggers-using-the-parpulse-function/5387>\
**Category:** Programming Help\
**Tags:** windows\
**Created:** [June 13, 2024, 3:50am UTC](https://psychtoolbox.discourse.group/t/how-to-send-triggers-using-the-parpulse-function/5387 "2024-06-13T03:50:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HuizhongWu](https://avatars.discourse-cdn.com/v4/letter/h/8edcca/32.png) [@HuizhongWu](https://psychtoolbox.discourse.group/u/HuizhongWu)\
**Post date:** [June 13, 2024, 3:50am UTC](https://psychtoolbox.discourse.group/t/how-to-send-triggers-using-the-parpulse-function/5387/1 "2024-06-13T03:50:56Z")

</div>

Hi,

I tried my best to understand how to use the parPulse function sending triggers as the official website explained [https://display-corner.epfl.ch/index.php?title=ParPulse&oldid=845](https://display-corner.epfl.ch/index.php?title=ParPulse&oldid=845), but failed. The main issue result from the different Syntax between the io64 function and the parPulse function.

This is a sample code of io64.

```auto
%trialstart marker using the io64 function
if measureEEG == 1
    data_out = startmark;
    io64(ioObj,address,data_out);
    WaitSecs(0.01);
    io64(ioObj,address,0); %reset to zero
end

```

_Should the afterVal of parPulse be reset to the zero value after the trigger sending, as the io64 function?_

The main syntax of parPulse is

```auto
parPulse([portAddr,] pulseVal, afterVal, valMask, pulseTi);

```

The sample code of parPulse is

```auto
  parPulse(hex2dec('d030')); % Initialize port
  parPulse(0,0,255,0); % Set all data lines to 0
  parPulse(0,1,255,0); % Set all data lines to 0, except for line 0 which is set to 1
  for i=1:50
      parPulse(2,1,3,1e-3); % 1ms pulse on data line 0 (negative pulse)
                               % and line 1 (positive pulse)
      pause(100e-3); % Pause for roughly 100ms
  end

```

In my understanding, the afterVal is the EEG mark in experiments (_If not, please correct me_). But I cannot get the meaning of pulseVal and valMask.

For the _pulseVal_  
I cannot understand:

1. why there needs a output value during pulse, what is the function of this value?
2. In an EEG experiment, what value should be set to the pulseVal, is it varied as different stimuli presented?

For the _valMask_  
The official web said that the _valMask_ indicats which output lines of the parallel port will actually be affected by pulseVal and afterVal.  
My question is:

1. Which output lines of the parallel port should be set in normal EEG experiments? Is the valMask a constant value during the whole experiment?

2. What is the meaning when the valMask was set as “255” as the line 2 of sample code “parPulse(0,0,255,0)” illustrated? Does it mean that there are 255 lines in the parallel port? Or, does “255” mean all data lines? (I guess this is the case.) If so, again, whilch line should be programmed by pulseVal and afterVal in a normal EEG experiment.

Other specific questions for the sample code of parPulse:

```auto
parPulse(0,1,255,0); % Set all data lines to 0, except for line 0 which is set to 1

```

The first one is which position of the four value in the aboved code indicating the line 0 is set to 1, which value indicates all the other lines is set to 0.  
In my understanding, the code means all lines (the third value: 255) is set to 1 (the second value which means afterVal: 1). But this is not matched with the comment.

```auto
  for i=1:50
      parPulse(2,1,3,1e-3); % 1ms pulse on data line 0 (negative pulse)
                               % and line 1 (positive pulse)
      pause(100e-3); % Pause for roughly 100ms
  end

```

The last question: which value means the line 0 and which means the line 1 in the above code “parPulse(2,1,3,1e-3)” ? How could I figure out whether the pulse is a negative pulse or a positive pulse as the comment said.

That’s all.  
This is the last part of my first EEG experiment, and very appreciate to anyone who can explain these questions clearly.

Thanks again,  
Huizhong

---

<div class="post-metadata">

**Author:** ![\_qx1147](https://avatars.discourse-cdn.com/v4/letter/_/a8b319/32.png) [@\_qx1147](https://psychtoolbox.discourse.group/u/_qx1147)\
**Post date:** [June 13, 2024, 7:37am UTC](https://psychtoolbox.discourse.group/t/how-to-send-triggers-using-the-parpulse-function/5387/2 "2024-06-13T07:37:51Z")

</div>

Well, I cannot tell which kind of trigger signal your EEG recording system and/or analysis software expects. But for mimicking your `io64()` code snipped, you would use [EDIT: swapped the first 2 parameters - ouch]:

```matlab
parPulse(data_out, 0, 255, 10e-3);

```

Or using the `isTrigger` version, which makes the call non-blocking - at the expense of a less accurate pulse time:

```matlab
parPulse(portAddr, data_out, 0, 255, 10e-3, 1);

```

When using the `isTrigger` version, the `portAddress` parameter has to be provided just for making the parameter interpretation unambiguous to other use cases of `parPulse()`. It does not mean that the I/O port is initialized from scratch in each call. It is ugly, but, originally, `parPulse()` was implemented for getting accurate pulse times in the sub-millisecond range; `isTrigger` was implemented later.

Note that your `io64()` code uses a 10ms pulse width, whereas your `parPulse()` code in the loop uses 1ms, which - depending on the EEG system -, might be too short to be captured reliably.

Regarding the logic behind `pulseVal`, `afterVal`, and `valMask`, maybe these formulas make it clearer:

```matlab
outVal_DuringPulse = (outVal_BeforeTheCall & ~valMask) | (pulseVal & valMask);
outVal_AfterPulse = (outVal_BeforeTheCall & ~valMask) | (afterVal & valMask);
outVal_BeforeTheCall = outVal_AfterPulse;

```

---

<div class="post-metadata">

**Author:** ![HuizhongWu](https://avatars.discourse-cdn.com/v4/letter/h/8edcca/32.png) [@HuizhongWu](https://psychtoolbox.discourse.group/u/HuizhongWu)\
**Post date:** [June 13, 2024, 8:18am UTC](https://psychtoolbox.discourse.group/t/how-to-send-triggers-using-the-parpulse-function/5387/3 "2024-06-13T08:18:42Z")

</div>

Words cannot express my appreciation for your help for many times.

> [@\_qx1147](#):
>
> `parPulse(data_out, 0, 255, 10e-3);`

Now, the synax is clear for me. The pulseVal is actually the trigger mark, not the afterVal.

> [@\_qx1147](#):
>
> ```auto
> outVal_DuringPulse = (outVal_BeforeTheCall & ~valMask) | (pulseVal & valMask);
> outVal_AfterPulse = (outVal_BeforeTheCall & ~valMask) | (afterVal & valMask);
> outVal_BeforeTheCall = outVal_AfterPulse;
> 
> ```

The first two lines make sense why the pulseVal and afterVal only worked to valMask.  
For the third formula, does it mean that the outVal have been resetted to afterVal, and that’s the reason why afterVal should be set to zero.  
If it’s ture, it is so beatiful and clever to complish the two steps in io64 into one step in parPulse.

Hope my understanding is correct this time.

Thanks again.

---

<div class="post-metadata">

**Author:** ![\_qx1147](https://avatars.discourse-cdn.com/v4/letter/_/a8b319/32.png) [@\_qx1147](https://psychtoolbox.discourse.group/u/_qx1147)\
**Post date:** [June 13, 2024, 8:39am UTC](https://psychtoolbox.discourse.group/t/how-to-send-triggers-using-the-parpulse-function/5387/4 "2024-06-13T08:39:25Z")

</div>

```matlab
outVal_BeforeTheCall = outVal_AfterPulse;

```

was just supposed to mean that `parPulse()` will leave the output state at `outVal_AfterPulse`, which will become the `outVal_BeforeTheCall` for the next call to `parPulse()`.

---

<div class="post-metadata">

**Author:** ![HuizhongWu](https://avatars.discourse-cdn.com/v4/letter/h/8edcca/32.png) [@HuizhongWu](https://psychtoolbox.discourse.group/u/HuizhongWu)\
**Post date:** [June 13, 2024, 9:17am UTC](https://psychtoolbox.discourse.group/t/how-to-send-triggers-using-the-parpulse-function/5387/5 "2024-06-13T09:17:29Z")

</div>

Yes, I know 😉

I try to express the same meaning

> [@HuizhongWu](#):
>
> the outVal have been resetted to afterVal, and that’s the reason why afterVal should be set to zero.

as yours.
