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 Answer 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
.
-
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– danieljay2016年12月05日 23:15:45 +00:00Commented Dec 5, 2016 at 23:15
-
@danieljay, for that simple example, maybe, but for more complex programs, stick with
millis()
Dat Ha– Dat Ha2016年12月05日 23:20:46 +00:00Commented Dec 5, 2016 at 23:20
unsigned
issue, how would you manage to dofake_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 reimplementedmillis()
.