Solution
The base concept you have to keep in mind is that both functions use the operating system's millisecond timer as a reference for counting the time to wait. With that said,
Wait Until Next ms Multiple cares about the value in the OS timer being a multiple of its input value at every iteration. Thus,
Wait Until Next ms Multiple may not wait the specified time on the first iterations as it is adjusting the loop timing to be "in phase" (synched) with the OS timer. Use this function to synchronize activities.
The
Wait (ms) function waits the time in ms regardless of the current time value in the OS timer. It doesn't care about getting in phase with the OS timer. On the other hand, it complies with the time to wait you defined in its input in every iteration.
The function icons also illustrate that difference.
Wait (ms) has a watch as its icon whereas
Wait Until Next ms Multiple has a metronome, which is used by musicians to force them to adjust and keep the same pace.
The figure below demonstrates the timeline for 2 loop executions. The black horizontal axis represents the OS timer count; the red line means the timeline of loop 1, which uses
Wait Until Next ms Multiple; the blue line represents the timeline of loop 2, which uses
Wait (ms). The circles represent a transition between iterations.
Considering that both functions receive an input of
100, and that the OS timer has a value of 50 at the beginning of both loop executions:
- Wait Until Next ms Multiple will not comply with the 100 ms input and will time the loop iteration with only 50 ms, matching the end of iteration/beginning of the next iteration, with the OS timer value equal to 100. The following iterations take 100ms and the OS timer value are multiples of 100 (200, 300, 400, etc.). If occasionally the loop takes more than 100ms, on the very next iteration, Wait Until Next ms Multiple will force a shorter iteration period in order to get in phase with the OS counter again.
- Wait (ms) will comply with the 100 ms since the very first iteration, regardless of the OS timer value. Notice that the iterations in the figure below are transitioning with 150, 250, etc... but those values have no meaning to Wait (ms). If occasionally the loop takes more than 100ms, on the very next iteration, Wait (ms) will time the loop again with 100 ms, regardless of the OS timer value.
You can also observe the difference in execution using the following code examples.
Wait (ms)
The Wait (ms) function will block execution until the time specified at
milliseconds to wait has
elapsed.
- Example 1: Consider a loop with code that takes 5 ms to execute. The loop also includes a Wait (ms) function with 10 ms wired to its milliseconds to wait input. The While Loop takes 10 ms total to execute because the code finishes after 5 ms and then the Wait (ms) function finishes 5 ms later. In this case, the Wait (ms) function executes in parallel with the code.
In this example, the loop timing is set by the outer Wait (ms) function.
- Example 2: The loop is the same as in the previous example, but the code now takes 15 ms to execute. The outer Wait (ms) function still has 10 ms wired to its milliseconds to wait input. Because the time wired to the Wait (ms) function is less than the time it takes the code to execute, there is no delay after the code finishes, and the loop moves to the next iteration immediately after 15 ms elapses.
In this example, the loop timing is not set by the Wait (ms) function. It is set by the overall execution time of the code within the loop.
Wait Until Next ms Multiple
You can use the Wait Until Next ms Multiple function to synchronize separate loops to your system's millisecond clock. As the name suggests, it will wait until the next multiple of the number of milliseconds specified at the
milliseconds multiple input before becoming unblocking.
- Example 3: There is a loop with code that takes 100 ms to execute and a Wait Until Next ms Multiple function running in parallel with the code. The Wait Until Next ms Multiple function has 200 ms wired to its millisecond multiple input. The loop executes at every 200 ms multiple of the system's millisecond clock. There is second loop in the same VI that has a Wait Until Next ms Multiple function with 200 ms wired to its millisecond multiple input. The code in the second loop takes 150 ms to execute running in parallel. The two loops will be synchronized to move to their next iterations every time a multiple of 200 ms on the system clock elapses. Using this method, it will be insured that each loop begins subsequent iterations at the same time.