pfeerick / elapsedMillis

Arduino 'port' of the elapsedMillis library
MIT License
84 stars 26 forks source link

Long delays are not working #11

Closed b2krp closed 5 years ago

b2krp commented 5 years ago

Good Morning,

I am using an Arduous Mega 2560 board to create a heat staging time delay. The code works fine for short time periods, but delays longer than about 30 seconds make the delay stop working.

I am guessing is that the data type is an int, which only goes up to 32,767 on the Mega. If this is correct, it may be helpful to add this caution the wiki document, or update the data type to unsigned long.

Thank you!

pfeerick commented 5 years ago

Do you mean 30 minutes? I have code running on both Arduino Uno and ESP8266 with delays of a couple of minutes. The library already uses unsigned longs, so theoretically should work for up to delays of up to 49/50 days - when a millis rollover occurs. It may even survive rollover if you use >= instead of > when checking if the interval has elapsed - I'll check for that later as elapsedMicros allows for accelerated testing ;)

I will test this on a Mega board just to make sure nothing strange is going on, but I would suggest you might want to check your code further, as even if a signed int were used, elapsedMillis should handle delays of around 9 hours (16 bit signed int is 32,767, and 32,767 / 60 / 60 is 9.1 hours)

pfeerick commented 5 years ago

I just had a head-desk moment... what variable type are you using for your interval variable? If you haven't changed it, the examples use a regular signed int, so yes, anything over 32.7 seconds will go negative, hence it won't work. I might just change the examples to longs, and add a reminder to the readme...

b2krp commented 5 years ago

It sounds like that was likely my issue. Since I was having trouble with the elapsedMillis() library, I changed the code to use a millis() math function to effect the delay. I'll try to recreate the original code to confirm next time the system is shut down.

Thank you!