Hi
I'm in the process of developing a home automation project that is as event-driven as possible. So interrupt-handling wins out over polling, testing millis() regularly is equivalent to polling, and delay() is definitely not a preferred approach.
However, many devices need time to stabilise between initiating a read and the value being available. The Dallas temperature sensors are the obvious example, but I'm sure there are others.
So what I needed was a way to initiate a read and then return a certain time later to capture the value.
The result is a library I've called WAKEUP that allows multiple 'sleeper' functions to be woken after a specified interval, allowing the main program to get on with other things in the meantime. This enables a crude form of co-operative processing or 'protothreading'.
I'm a newbie when it comes to libraries and have doubtless made some elementary mistakes, although I have tested it pretty thoroughly (on a Mega). However, I'd appreciate some more experienced eyes on the code. After several false starts I've managed to create a project on google, so you can find the code here:
http://code.google.com/p/arduino-ha/ Haven't managed to master the creation of a nicely structured download (sorry!), but if you go to source > browse and download everything under trunk > libs that should get you started. It includes a modified version of Timer1, already discussed with Lex at
http://code.google.com/p/arduino-timerone/ and the following examples:
- Multi_timer - series of examples making use of the library
- Dallas_temperature - an example showing how to return to pick up reading 750ms after initiating it
- Calibration - done as an exercise to figure out the right value to compensate for code lag
All comments appreciated. Many thanks in advance
- Andrew