in the Arduino world you read the system time, add the desired time period, call the function, and loop until the system time matches the saved time.
Well, that's one way to do it, but IMHO not the most efficient. The generic way to do this with a microcontroller would be to use one of the uC's timers, typically using a 32.768 kHz crystal as the clock source, set up one of the timer's compare units to trigger an interrupt when the timer value matches the user-configured interval (in other words, have the hardware do what you're describing doing in software), and put the uC to sleep or do other things until the interrupt fires. For example, on the Arduino Uno, one could use the ATmega328P's Timer2.
Minor edits.
| [reply] |
Since I came out of the real-time software world, I would use a processor timer interrupt as you pointed out. However, most of the Arduino world wouldn't understand event driven code if they tripped over it. They use delay() which is blocking - a bad idea.
I participated in the Open Source Ventilator Project in connection with the University of Florida. The Delay() loop people won out and ran off a contributor that had developed an event driven design because they didn't understand it and didn't want to learn. The end result was a working $200 ventilator, out for manufacture, with some really nasty delay() loops. Good luck with maintenance...
James
There's never enough time to do it right, but always enough time to do it over...
| [reply] |
However, most of the Arduino world wouldn't understand event driven code if they tripped over it. They use delay() which is blocking - a bad idea.
Heh, true. Arduino does have the advantage of having made microcontrollers more accessible, but at the cost of people not learning as much about the hardware at a lower level.
I've been working with one lately and I've already run into several issues: that the SD library needs a 512 byte buffer that it apparently allocates dynamically (or at least the complier doesn't detect the need for it, so I manually have to keep an eye on the RAM usage and use globals instead of the stack as much as I can, ugh), that constant strings are stored in RAM, that delay() itself uses interrupts, the low-power library I was using uses the watchdog timer which conflicted with my use of the watchdog... I've learned quite a few things about the libraries by tripping over them.
| [reply] [d/l] |