2

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

Greenonline
3,1527 gold badges36 silver badges48 bronze badges
asked Dec 5, 2016 at 22:46
2
  • 1
    millis() returns an unsigned long, not a long. It is never negative. Commented Dec 6, 2016 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(). Commented Dec 6, 2016 at 8:25

1 Answer 1

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.

answered Dec 5, 2016 at 23:09
2
  • 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 ? Commented Dec 5, 2016 at 23:15
  • @danieljay, for that simple example, maybe, but for more complex programs, stick with millis() Commented Dec 5, 2016 at 23:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.