-1

I am using attiny microcontroller and atmel studio. And I am using the millis function in my project.

Code related to the millis function:

Image is here

And I am using this classic code:


starttimex = millis();
endtimex = starttimex;
    
while ((endtimex - starttimex)<=60000)
{
               
    endtimex = millis();
                        
    // Action
                         
}

I don't want the millis() function to reset after 50 days. I have to use uint64_t instead of unsigned long.

My questions:

  1. Does this cause any trouble? Does this situation have a disadvantage? One of the disadvantages is memory size. I know this. But, I don't know other disadvantage.

  2. I don't understand variable of timer0_overflow_count in the image. Is there a need for this for millis?

  3. Should I do uint64_t for all of the unsigned long variables in the image?

Thanks

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • You are using the `millis()` function from the Arduino Core? Why? What exactly is your end goal? – elliptic_hyperboloid Aug 13 '20 at 15:13
  • 3
    Please do not post pictures of code, copy the code into your question. Read the guide on creating a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – elliptic_hyperboloid Aug 13 '20 at 15:34
  • 3
    You don’t need to worry about the rollover if you do your math right. It’s a non-issue. – Delta_G Aug 13 '20 at 16:36
  • As pointed out by Delta\_G and datafiddler, you are worrying about a non-issue. See [How can I handle the millis() rollover?](https://arduino.stackexchange.com/q/12587/7508). – Edgar Bonet Aug 14 '20 at 20:15
  • Thanks for replies, no need to do uint64_t formy code.Because, if millis is reset in loop,endtimex will be 0,1000,2000.Example,starttimex=4294947296.So 0-4294947296=20000 1000-4294947296=21000 2000-4294947296=22000 work is not interrupted,of course my limiter 60000 under my limiter condition. – harun caliskanoglu Aug 15 '20 at 13:40

2 Answers2

2

If your interval (60000 in your sample code) does not exceed the range of unsigned long, you're fine.

The millis() function does not reset, it simply rolls over :)

(endtimex - starttimex) in unsigned arithmetics calculates fine, even during a rollover.

datafiddler
  • 1,755
  • 3
  • 17
  • 30
0

I don't understand variable of "timer0_overflow_count" in image. Is there a need for this for millis?

The overflow count is used to keep track of the number of millis even when the period of the timer is not an exact multiple of milliseconds. For example, imagine if the timer went off every 1.5ms, see how that would work?

Does this cause any trouble?Does this situation have a disadvantage?

You can use any type you want for the millis counter, but keep in mind that interrupts are off whenever that variable is updated or read, so going from 32 to 64 bits means that interrupts will be off for significantly longer, which could impact other interrupts running on the system.

bigjosh
  • 1,273
  • 13
  • 19