Time based events

So, I’ve got:

a FEZ Cobra
network up
NTP up with Microsoft.SPOT.TimeService
clock timer getting datetime.now, updating OLED, and thread.sleep(1000)[/ul]

unfortunately, my OLED skips a second every few seconds due to not firing right on the second change.

I’d like to make things more multi threaded and don’t want to have to have a main loop that updates the OLED and sleeps. I’d like to eventually add a webserver and other things.

Is there a way to configure C# (or something lower level) to fire a C# event on the second and minute changes? I’d like to use this to update the OLED and other things


Does this help?

That’s the right idea. I’m generally aware of how to create timers, and that’s definitely an improvement over doing my proof of concept Thread Sleep 1000, but ultimately i’m going to run into the same issue which is that the timer doesn’t necessarily start right on a second transition. My hope was that there was a way to fire an event based on when the system time is at the top of a second- this way whenever it fires I can be sure that I have nearly a second to get my display updated - it doesn’t take nearly that, but then I know I’ll be showing the time for that second period instead of updating my display at 11:47:50.9 and going 1 second later + overhead and displaying 11:47:52.1 and thus having the clock appear to skip a second.

I realize this is probably impossible- at least strictly speaking. I can probably create a timer for 990ms and loop until the second transitions or something, but I hate tying up the CPU like that.

My solution to this problem is to check the current time more often than once a second. Check the time every 100ms, and only update the display if the time changes. Then you will be off by no more that ~100 ms.

Another solution would be to have a separate thread for reading the time. The thread would first read the current time and set it into a variable. It would then calculate the milliseconds until the next second change and go to sleep for that interval. It would then get the current time and repeat. With this solution, the CPU time would be conserved, and the time would be off by no more than 25 milliseconds. The changing of the time could fire off an event, where the event handler would update the display.

1 Like

I had a similar idea with the microseconds at one point but started to abandon it because I wanted my loops to be at an even interval, but your idea fixes that by putting it in a separate thread. I’d like to leave the processor as free as possible so I can make the web interface work- I noticed my display update has noticeable wait when the NTP update occurs so I know I’m sort of constrained. I’m going to try to optimize my existing RLP stuff and put all my LCD bits into RLP.

In any case, I like your idea, and I think that basically solves my problem!

Thanks to you both!