Accurate Delay

Hello,

Is there any way to create a reliable accurate delay that is not computer dependent? I.e. if I want a 5 ms delay, the function will delay any other Igor functions for 5ms +- 2-5%.

I've tried doing this with the MStimer (code to find the number of us for each iteration of a loop) and it varies by a large amount (0.095us/iteration to 0.326us/iteration). This would mean a 5 ms delay could be 5ms or 17ms.


Thanks,
It depends on how accurate you want "accurate" to be. The Sleep operation will generate a delay, but it's not clear that it can produce an accurate 5 mS delay.

Using a loop to delay will, of course, occupy the processor completely as it runs. That may or may not bother you. But different computers with differing instruction sets and clock speeds will, indeed, vary a lot in how fast an empty loop runs. In fact, such a loop will vary somewhat even on the same computer as the processor may be interrupted sometimes by the system to service the network adapter, disk drives, etc.

But why not use StopMSTimer inside the loop:
Function delay(ms)
    Variable ms
   
    Variable delay = ms*1000
   
    Variable start = StopMSTimer(-2)
    do
    while(StopMSTimer(-2) - start < delay)
end

Even this will depend on the resolution of the system's timer, and the availability of the processor to run the loop. And busy loops like this are frowned on these days, as they take computing power from other processes, and they burn battery on a laptop.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com