1

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();

1
  • 1
    the difference will not become negative ... this has been covered here many times
    – jsotola
    Jul 8 at 6:19
1

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.

3
  • 1
    I would flag this as not an answer. it is a comment
    – Juraj
    Jul 8 at 8:52
  • Well, it perfectly answers will millis handle the rollover if we use unsigned variables for all variables for millis(); Jul 8 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
    Jul 13 at 7:28
1

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/

Not the answer you're looking for? Browse other questions tagged or ask your own question.