Hi all,
Let me describe an issue that I’m having and can’t figure out:
Every now and then (in the course of less than a minute) a comparison that is NOT supposed to resolve to true, does.
I have this code as part of the firmware that is running on a Boron:
Global scope:
unsigned long receivePeriod = 0;
unsigned long receiveTimeout = 120000;
on a function that runs from loop():
receivePeriod = millis();
on another function that runs from loop() too (I’ve added a few new lines for easier reading):
if ((millis() - receivePeriod) > receiveTimeout)
{
Log.error("%s() - timeout waiting for messages after %lu msec %lu %lu %lu %d",
__FUNCTION__, millis() - receivePeriod, millis(), receivePeriod, receiveTimeout,
((millis() - receivePeriod) > receiveTimeout));
timeouts++;
}
(original code without newlines):
if ((millis() - receivePeriod) > receiveTimeout)
{
Log.error("%s() - timeout waiting for messages after %lu msec %lu %lu %lu %d", __FUNCTION__, millis() - receivePeriod, millis(), receivePeriod, receiveTimeout, ((millis() - receivePeriod) > receiveTimeout));
timeouts++;
}
Often times this code works fine, guestimate 8 out of 10 times.
But a couple of times out of ten, I observe this log (I’ve added a new line for easier reading):
0000129170 [app] ERROR: receiveUpdateFunction() - timeout waiting for messages
after 3 msec 129170 129167 120000 0
How could this if condition resolve as 1 (or true) at 3 msec when the comparison calls for 120000 msec?
I think I see this happening in the first log:
→ ((millis() - receivePeriod) > receiveTimeout)
→ (129170 - 129167) > 120000
3 > 120000 is true
But even then, when the log inside the if prints ((millis() - receivePeriod) > receiveTimeout) it resolves to 0 (zero or false), as the last value printed shows.
Another instance of the error:
0000216593 [app] ERROR: receiveUpdateFunction() - timeout waiting for messages
after 1 msec 216593 216592 120000 0
What kind of bit manipulation, internals, or something I can’t figure out yet is hitting me here?
Thanks!