# Timing between NI card and visual stim

**URL:** <https://mworks.discourse.group/t/timing-between-ni-card-and-visual-stim/302>\
**Category:** Support\
**Created:** [September 15, 2018, 3:37pm UTC](https://mworks.discourse.group/t/timing-between-ni-card-and-visual-stim/302 "2018-09-15T15:37:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [September 15, 2018, 3:37pm UTC](https://mworks.discourse.group/t/timing-between-ni-card-and-visual-stim/302/1 "2018-09-15T15:37:47Z")

</div>

Hello Chris,  
I need help with ensuring consistent timing between two events; the first would be to pulse a digital output on a NI card and the second would be to produce a visual stimulus on the screen. I tried scheduling them with the time difference i want, but there is a jitter of around +/- 10 ms around the desired delay. Any idea how i can improve this?  
Cheers,  
Beshoy

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [September 17, 2018, 1:43pm UTC](https://mworks.discourse.group/t/timing-between-ni-card-and-visual-stim/302/2 "2018-09-17T13:43:53Z")

</div>

Hi Beshoy,

This is tricky. What is the desired interval between the pulse and the stimulus?

Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [September 17, 2018, 1:54pm UTC](https://mworks.discourse.group/t/timing-between-ni-card-and-visual-stim/302/3 "2018-09-17T13:54:37Z")

</div>

Hi Chris,

I want the visual stimulus to be followed by a pulse after 30ms. Initially, I scheduled the two events so that they are 30ms apart. So far, I found that the time required to execute the NI command is pretty consistent (1-3 ms) while the time for updating the display can be anything between 10-24 ms.

Here is what I did to improve this; I got the time when the last display update occurred and try to ensure that the next display update is scheduled at a multiple of a frame time. This reduced the variability a bit to around +/-5 ms from the desired delay. The rationale was to have the display update command to happen at the beginning of a monitor refresh.

Cheers,  
Beshoy

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [September 17, 2018, 2:14pm UTC](https://mworks.discourse.group/t/timing-between-ni-card-and-visual-stim/302/4 "2018-09-17T14:14:42Z")

</div>

Hi Beshoy,

> I want the visual stimulus to be followed by a pulse after 30ms.

Ok, great, that’s much easier than making the stimulus follow the pulse.

The best approach would be to get the predicted output time from [update\_stimulus\_display](https://mworks.github.io/documentation/latest/components/update_stimulus_display.html) and schedule the pulse to occur at that time plus 30ms. Assuming you have a stimulus named `stim` and a variable called `stim_onset_time`, here’s how this would look in [MWEL](https://mworks.github.io/documentation/latest/mwel/index.html):

```
queue_stimulus (stim)
update_stimulus_display (predicted_output_time = stim_onset_time)
wait (stim_onset_time - now() + 30ms)
// Send the pulse now

```

Of course, you could use a timer and timer-expired transition instead of a wait, or you could create a [scheduled action](https://mworks.github.io/documentation/latest/components/schedule_actions.html), if that fits better with your experiment design.

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [September 19, 2018, 8:28pm UTC](https://mworks.discourse.group/t/timing-between-ni-card-and-visual-stim/302/5 "2018-09-19T20:28:37Z")

</div>

Hello Chris,

I tried this and it works much better now. Whether i use wait or schedule, i get most of my delays within 4 ms of what i want; that works for me.  
Thanks,  
Cheers,  
Beshoy

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 19, 2022, 9:40pm UTC](https://mworks.discourse.group/t/timing-between-ni-card-and-visual-stim/302/6 "2022-07-19T21:40:13Z")

</div>


