I’m looking for some clarity on understanding the difference between using threads with while loops, and timers.
I haven’t tried my current program with threads yet but I am successfully using two timers, that I have tried to synchronize as I believe that a timer will be blocked if the previous timer hasn’t completed it’s task. Is this correct?
For simplicity’s sake lets say that the first timer polls some inputs and the second writes the results to the SD card. I know how long it takes to poll the inputs so I make sure that timer is longer and the second timer fires before the first one fires again. So timer 1 fires every 130ms, it takes 90ms max to poll the inputs so the second timer to write the results is offset by 115ms. It only takes a few ms to write the details to the SD card so I know that is done before the timer to poll the inputs is fired again.
So am I understanding timers and using them correctly? The main reason that I ask is because I’ve been recording the times that the first timer fires and I found a weird pattern where periodically the first timer doesn’t fire. It can be seen in the attached chart of the results. It also appears to be unreliable in it’s timing, only hitting 130ms around 70% of the time.
So what about using threads? Is it possible that I can poll the inputs in a while loop in one thread, and write the results to the SD card in another? If so, will the two threads actually execute simultaneously?
Many thanks in advance for any help.