# Question about update\_stimulus\_display

**URL:** <https://mworks.discourse.group/t/question-about-update-stimulus-display/407>\
**Category:** Support\
**Created:** [July 24, 2017, 1:31pm UTC](https://mworks.discourse.group/t/question-about-update-stimulus-display/407 "2017-07-24T13:31:30Z")\
**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:** [July 24, 2017, 1:31pm UTC](https://mworks.discourse.group/t/question-about-update-stimulus-display/407/1 "2017-07-24T13:31:30Z")

</div>

Hi there,

I try to present rhythmic stimuli in a part of my experiment like"…1212121…" (start and end can both either be 1 or 2), so i write a transitional sentence that will go to the previous task\_system\_state to make a loop (for the related code see line 80-88 and 150-197).

The experiment generally works fine; however, approximately 1 in 5 trials there will be a display problem like “… 1(1 second) 2 (1 second) 1 (a blink, maybe 1 frame) 2(1 second) …” (given the period is 2 second). I check the code myself first. I find when this problem happens, after “update\_stimulus\_display” either "stimulus\_ending\_1” or “stimulus\_ending\_2” will change (e.g. 0 , 0 → 0, true; they should always be 0; in line 180 and 184), but I do not know why this happens.

Besides, I am a beginner in MWorks. I used to program on psychtoolbox and I was taught to always write a time slack: if the refresh rate of a monitor is 50Hz (20ms per frame), and the stimulus duration I want to present is 600ms, I should let the computer update the stimulus at 590ms. Otherwise, if I make it 600ms, due to the delay of the command or whatever, stimulus may present 31 (30+1) frames. I do not know if this works for the MWorks Timer.

Thanks,  
Xiang

---

<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 24, 2017, 1:31pm UTC](https://mworks.discourse.group/t/question-about-update-stimulus-display/407/2 "2017-07-24T13:31:30Z")

</div>

Attachment: [pingpong.xml](https://mworks.discourse.group/uploads/short-url/uUlOhQzhKusWxf7VVUhzl8pTa0b.xml) (25.1 KB)

---

<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 24, 2017, 1:48pm UTC](https://mworks.discourse.group/t/question-about-update-stimulus-display/407/3 "2017-07-24T13:48:04Z")

</div>

I forget to mention my MWorks version is 0.7 and my OS X version is 10.10.5 .

---

<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 24, 2017, 4:20pm UTC](https://mworks.discourse.group/t/question-about-update-stimulus-display/407/4 "2017-07-24T16:20:29Z")

</div>

Hi Xiang,

I think I see what’s going on.

Because you reset stimulus\_ending\_1 and stimulus\_ending\_2 after dequeueing the stimuli but _before_ invoking update\_stimulus\_display, the “old” stimulus is sometimes setting the “ending” variable a second time. (This happens due to some details of the implementation – not a bug, exactly, but certainly unhelpful in this case.)

The fix is to alter the transitions out of the stimulus\_execution\_3 state to require that the _current_ stimulus is ending and ignore the previous stimulus. For example, on line 195, you should change the transition condition from

```
(stimulus_ending_1 OR stimulus_ending_2) AND half_period_number_remain

```

to

```
((stimulus_current == 2 AND stimulus_ending_1) OR (stimulus_current == 1 AND stimulus_ending_2)) AND half_period_number_remain

```

> I was taught to always write a time slack: if the refresh rate of a monitor is 50Hz (20ms per frame), and the stimulus duration I want to present is 600ms, I should let the computer update the stimulus at 590ms.

That’s exactly how the [ending parameter](https://mworks.github.io/documentation/0.7/components/frame_list_stimulus.html#ending) works: It becomes true during the frame _immediately preceding_ the final frame. This lets your protocol use the time spent rendering the last frame to prepare the next stimulus, which will then be displayed during the frame immediately _after_ the last frame.

Cheers,  
Chris Stawarz

---

<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 24, 2017, 8:01pm UTC](https://mworks.discourse.group/t/question-about-update-stimulus-display/407/5 "2017-07-24T20:01:04Z")

</div>

Hi Chris,

I have revised the program and it works out!

Thanks,  
Xiang

---

<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:56pm UTC](https://mworks.discourse.group/t/question-about-update-stimulus-display/407/6 "2022-07-19T21:56:34Z")

</div>


