How to reset millis() in Arduino, why not to do it, and what to do instead? - Page 1
Forum section: electronics / general

author: Chumanista
author_rank: Contributor
timestamp: August 18, 2016, 04:45:40 pm
content: PLEASE READ THE FOLLOWING BEFORE COMMENTING!Hi!I didn't find any videos about that popular topic so I recorded one.I first show the incorrect way (resetting the variable that stores the value), then explain why it is wrong and finally show the correct way (a wrapper with an offset) and a library that achieves it internally.And all that in 2 minutes!All feedback welcomeAnd the usual: If this is not cool just tell me.

author: jancumps
author_rank: Unknown
timestamp: August 18, 2016, 04:56:08 pm
content: It's not readable on an iPad with video maximised.There's the views on the left and right of the editor, and the console at the bottom that take most of the real estate. And then the editor with small font in the middle of that.

author: Chumanista
author_rank: Contributor
timestamp: August 18, 2016, 04:57:58 pm
content: Fair point. I'll try to fix this in the next video.

author: macboy
author_rank: Super Contributor
timestamp: August 18, 2016, 05:53:29 pm
content: Why would you ever want to reset the millis counter? Seriously, give one good justifiable reason.Consider what will happen to all the libraries you are using (including those that Arduino IDE includes implicitly) that might rely on the millis counter. Do you know what they will do when it is reset without their knowledge?

author: Chumanista
author_rank: Contributor
timestamp: August 18, 2016, 05:59:39 pm
content: Why would you comment on a video without watching? Or reading my post, actually?I show how to really reset it out of hatred for tutorials that answer a question with "just do something else" and then explain how to do it correctly, either with a simple struct or a ready library.Also only one of the "default" libraries use millis(), it's Stream.cpp, for timeouts in two functions, inside of a while().

author: Kilrah
author_rank: Unknown
timestamp: August 18, 2016, 06:14:39 pm
content: Because your title is inappropriate.It's the first thing people see, and when it sounds as stupid as it does there they won't even click."Why you don't want to reset the millis() counter and what to do instead?" might have more success.

author: Chumanista
author_rank: Contributor
timestamp: August 18, 2016, 06:18:41 pm
content: The Youtube title is based on what people would look for, if somebody knows you should't reset it he most likely doesn't need a tutorial.Fixed the title here though, thank you for the idea.

author: dannyf
author_rank: Super Contributor
timestamp: August 18, 2016, 08:48:18 pm
content: Not sure why you want to reset it.Looking at the source code will help.

author: Chumanista
author_rank: Contributor
timestamp: August 19, 2016, 10:26:26 am
content: Many people think that resetting it would be a good way to measure time.Thus I made a video where I explain why it's not and what is the real solution.

author: zapta
author_rank: Super Contributor
timestamp: August 19, 2016, 01:31:05 pm
content: Quote from: Chumanista on August 19, 2016, 10:26:26 amIt's the first time I hear the idea of resetting the millis .Anyway, having a passive timer class that provides elapsed time is very useful and is standard in mbed.Another trick I used is reading millis once in the beginning of loop() and storing in a global variable.

author: Chumanista
author_rank: Contributor
timestamp: August 19, 2016, 01:35:11 pm
content: Just one thread about it on the Arduino Forums has 22k views so many people thought about it.The ElapsedMillis library i show in the video provides the desired functionality quite well.

author: dannyf
author_rank: Super Contributor
timestamp: August 19, 2016, 02:04:33 pm
content: Quotemaybe I missed that but I don't recall seeing it.

author: Chumanista
author_rank: Contributor
timestamp: August 19, 2016, 02:10:21 pm
content: The video is linked in my first post in this thread, the explanation starts around 0:50.

author: zapta
author_rank: Super Contributor
timestamp: August 19, 2016, 05:02:53 pm
content: Quote from: Chumanista on August 19, 2016, 01:35:11 pmMost likely most people here are more experienced and didn't even consider reseting the central millis(). It's all about the context.Anyway, that ElapsedMillis looks like a good thing. One tutorial that will be really useful is how to avoid using delay() and running multiple Arduino tasks, each with it's own setup() and loop().

author: dannyf
author_rank: Super Contributor
timestamp: August 19, 2016, 07:26:30 pm
content: QuoteYour video talked about two approaches to reset the millie counter. It had no discussion on why resetting the millie counter isn't necessary or is a bad practice.

author: Chumanista
author_rank: Contributor
timestamp: August 19, 2016, 07:30:08 pm
content: Not really, no. I have said it breaks libraries and the second approach doesn't alter the contents of millis() in any way.Here is the struct i used in it:Code:struct myMillis {unsigned long offset = 0;void set(unsigned long current) {offset = millis() - current;}void reset() {offset = millis();}unsigned long get() {return millis() - offset;}};

author: dannyf
author_rank: Super Contributor
timestamp: August 19, 2016, 08:50:19 pm
content: 1. set() and reset() is basically the same thing;2. you can declare it through a typedef and then you can use multiple copies of it concurrently.3. in my view, it is much simpler to just use millis directly.my 2 cents.

author: Chumanista
author_rank: Contributor
timestamp: August 19, 2016, 08:53:01 pm
content: This was just an example of how it can be done. The library does this all in a much nicer way automatically with operator overloading.It is easier to use some code to handle it than to subtract values manually every time, especially when you need to keep track of many intervals.