1

I think my issue is not fully understanding how the millis() function works;

I am wondering the difference between simply creating a variable such as

long fake_millis  ++ ;  

vs using millis()?

  • 1
    millis() returns an unsigned long, not a long. It is never negative. – sa_leinad Dec 6 '16 at 4:21
  • 1
    Besides the unsigned issue, how would you manage to do fake_millis++; exactly once per millisecond? Think about it: it's not so simple, especially if the program is busy doing other stuff. Best solution would be to do it from a timer interrupt. Once you do that, you have essentially reimplemented millis(). – Edgar Bonet Dec 6 '16 at 8:25
1

millis() returns the ACTUAL time milliseconds since the start of the program. Wheter you put a delay, you do multiple tasks, millis() will still return the right time.

The other method that you mentionned will not work if, for example, you add a delay in the program. Then, for more complex tasks that might take more time, your "custom" counter will be very unacurate.

I short, just use millis(). It is easy to use and works great.

P.S. Use an unsigned long instead of a long.

| improve this answer | |
  • appreciate the response. I am wondering though for this basic formula where i just need to have a "second" counter, pastebin.com/raw/hxcVukR7 could i just replace Millis() with my method ? – danieljay Dec 5 '16 at 23:15
  • @danieljay, for that simple example, maybe, but for more complex programs, stick with millis() – Dat Ha Dec 5 '16 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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