jremington:
Also, for the approach to work well, the time interval between calculations must be constant. I see nothing in the incomplete code that you posted which suggests attention to this important detail.
Why would the loop function not be constant?