i'm working on a project where i need to run a function for 20 minutes and the function writes to memory using EEPROM every minute. How can i make the function run for 20 minutes, i was thinking of using millis to measure the time between starting time and current but i'm afraid that when the millis rolls over and the the difference in time becomes negative my program will break and my function will run forever . Is there any way to make a function run for a certain amount of time, or will millis handle the rollover if we use unsigned variables for all variables for millis();
2 Answers
See this answer for an explanation. Using unsigned variables, and subtracting will definitely work.
Not only will unsigned variables not become negative (by their very nature) but the rollover will be handled smoothly and accurately.
-
1
-
Well, it perfectly answers
will millis handle the rollover if we use unsigned variables for all variables for millis();
Commented Jul 8, 2021 at 11:42 -
it is a comment - it answers the question. It looks "short enough" to be a comment, and I note there is such a comment under the question. However comments under the question are for clarifying the question and not answering it. If I am wrong, then someone can explain why, and perhaps vote it down. Which I note hasn't happened.– Nick Gammon ♦Commented Jul 13, 2021 at 7:28
Another way to solve this problem is using cronAlarm. It can provide precise control on timings and you can schedule your job also. https://www.arduino.cc/reference/en/libraries/cronalarms/