I thought the subject of this thread was how to address overflow in the argument to the arduino delay function.
My main request when I started the thread to have the documentation improved, to explain more clearly how to use the delay() function correctly. The docs
still don't indicate what the type of the argument is. A warning there about the perils of 16-bit arithmetic would also be pretty helpful.
It seems to me that we help users with overflows in the arguments of functions like delay if the cast is implicitly done by calling the function through a macro. I wonder if you think that would be worthwhile.
I agree it would probably help most people avoid the mistake I made, and to that extent it might be worthwhile. On the other hand, it doesn't help people understand what they're doing.
My main objection to the macro, however, is that the
way it works is a bit unclean. The cast doesn't act on the argument as a whole, but depends on it being an arithmetic expression that can be modified before it's evaluated. Usually, you try to avoid writing macros that work that way because they lead to code that fails mysteriously. In this case, for example, the following cases do and don't work...
#define delay(_val) original_delay( (unsigned long) _val )
delay(60000); //works
delay(60*1000); //works
delay((60*1000)); // fails
unsigned long interval = 60*1000;
delay(interval); // fails
So, the simplest and most common usages of the function would now work, but the reason why the last two examples fail would be hard to explain. Just as C code, all four of my examples would ordinarily be expected to produce the same result, since all four delay() arguments evaluate to the same value when taken by themselves. Of course, without the macro, all four examples would fail, but once you understood why, you'd have learned something useful and (hopefully) become a better C programmer.