Mem,
heh, It's not much, just something on the order of,
If (Alarm.count == dtNBR_ALARMS){
don't set the alarm.
}
I then do something else. In one case, where I really need the particular alarm, I set a flag to try again later (without using an alarm). I also use the count to tell me if I need to up the max number of alarms the next time I compile. An example of this is a controller I have for various functions around the house. Things like turn off the A/C recirculators, guarantee a pump is shut off before going to bed, that kind of thing. I get the count of alarms active, save the max over time and display it. This way when I get close to the compiled max, I bump the max, recompile and put the device back in service. It's not an elegant, sophisticated solution, but it keeps me from running out of timers. On this device I have dtNBR_ALARMS set to 20 and at run time I max out around 14 over a couple of days operation.
See, when you dynamically create alarms to handle something like retrying a failed communication
(such as Pachube) or time a response from a remote device that may be slow responding
(my neighbors chicken feeder that may fail because a chicken is asleep on it), it's easy to loose track of how many are possible at a given instant. Additionally, it may not be possible to actually know the max used because seemingly random hardware events happen. Yes, I could possibly count up all the instances of creation and set the max for that number, but that is wasteful of the limited memory on the Arduino. The twenty I already have eat up 240 bytes and I'm not close to being done with the eventual capabilities of the device and I must have 25 or thirty timerOnce calls scattered around in the code doing various things.
Obviously, it would be nice to be able to allocate a new timer whenever the number hit the max, but that would likely require malloc and I'm reasonably convinced that would introduce unexpected problems over the long term on a little device. If you want to see the controller where I display the timer count it's at
this page. Scroll down a little and there is a short video that illustrates what happens.
And, reviewing the thread you pointed out, I disagree with Dave Mellis, returning the alarm id is such a tiny complication that it wouldn't be worth mentioning. At least IMHO.
viknet,
Yes, I could use the ID like you illustrate, but suppose the ID isn't just an index into the array? In some libraries the token is a pointer, offset into a buffer, structure or something even more exotic. Relying on what it appears to be (index) seems a little scary.