Author Topic: How to reset millis() in Arduino, why not to do it, and what to do instead?  (Read 16047 times)

0 Members and 1 Guest are viewing this topic.

Offline ChumanistaTopic starter

  • Contributor
  • Posts: 41
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 welcome  :D

And the usual: If this is not cool just tell me.
« Last Edit: August 18, 2016, 08:49:24 pm by Chumanista »
 
The following users thanked this post: jancumps

Offline jancumps

  • Supporter
  • ****
  • Posts: 1272
  • Country: be
  • New Low
Re: I made a video on resetting millis() in Arduino.
« Reply #1 on: August 18, 2016, 04:56:08 pm »
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.
 

Offline ChumanistaTopic starter

  • Contributor
  • Posts: 41
Re: I made a video on resetting millis() in Arduino.
« Reply #2 on: August 18, 2016, 04:57:58 pm »
Fair point. I'll try to fix this in the next video.
« Last Edit: August 18, 2016, 05:00:30 pm by Chumanista »
 

Offline macboy

  • Super Contributor
  • ***
  • Posts: 2254
  • Country: ca
Re: I made a video on resetting millis() in Arduino.
« Reply #3 on: August 18, 2016, 05:53:29 pm »
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?
 

Offline ChumanistaTopic starter

  • Contributor
  • Posts: 41
Re: I made a video on resetting millis() in Arduino.
« Reply #4 on: August 18, 2016, 05:59:39 pm »
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().
« Last Edit: August 18, 2016, 06:02:07 pm by Chumanista »
 

Offline Kilrah

  • Supporter
  • ****
  • Posts: 1852
  • Country: ch
Re: I made a video on resetting millis() in Arduino.
« Reply #5 on: August 18, 2016, 06:14:39 pm »
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.
 
The following users thanked this post: Chumanista

Offline ChumanistaTopic starter

  • Contributor
  • Posts: 41
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.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Not sure why you want to reset it.

Looking at the source code will help.
================================
https://dannyelectronics.wordpress.com/
 

Offline ChumanistaTopic starter

  • Contributor
  • Posts: 41
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.
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6190
  • Country: us


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.

It'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.

 

Offline ChumanistaTopic starter

  • Contributor
  • Posts: 41
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.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Quote
Thus I made a video where I explain why it's not and what is the real solution.

maybe I missed that but I don't recall seeing it.

================================
https://dannyelectronics.wordpress.com/
 

Offline ChumanistaTopic starter

  • Contributor
  • Posts: 41
The video is linked in my first post in this thread, the explanation starts around 0:50.
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6190
  • Country: us
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.

Most 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().
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Quote
The video is linked in my first post in this thread, the explanation starts around 0:50.

Your 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.

================================
https://dannyelectronics.wordpress.com/
 

Offline ChumanistaTopic starter

  • Contributor
  • Posts: 41
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: [Select]
struct myMillis {
  unsigned long offset = 0;
  void set(unsigned long current) {
    offset = millis() - current;
  }
  void reset() {
    offset = millis();
  }
  unsigned long get() {
    return millis() - offset;
  }
};
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
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.
================================
https://dannyelectronics.wordpress.com/
 

Offline ChumanistaTopic starter

  • Contributor
  • Posts: 41
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf