Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store the millis() value in a unsigned long. #1

Open
Koepel opened this issue Mar 10, 2018 · 3 comments
Open

Store the millis() value in a unsigned long. #1

Koepel opened this issue Mar 10, 2018 · 3 comments

Comments

@Koepel
Copy link

Koepel commented Mar 10, 2018

The millis() does a rollover from 0xFFFFFFFF to 0x00000000 after about 50 days. To avoid problems, store a millis value always as unsigned long and subtract it with "currentMillis - previousMillis". That substraction with both being unsigned long will result in a valid number, even if millis() is in the middle of a rollover.

@basvdijk
Copy link
Collaborator

Het unsigned long verhaal begrijp ik. Maar onderstaande niet echt

store a millis value always as unsigned long and subtract it with "currentMillis - previousMillis"

Op https://github.com/BasOnTech/Arduino-Projects-NL/blob/master/mini-weerstation/mini-weerstation-deel-3.ino#L362 sla ik de huidige millis op. Welke previousMillis wil dan hier vanaf halen?

@Koepel
Copy link
Author

Koepel commented Mar 13, 2018

I prefer to write in English, so everyone knows what we are talking about. Is that okay ?

Your 'currentMillis' was already unsigned long, and of course millis() itself returns an unsigned long. With 'previousMillis' I just ment any variable that remembers a previously value of millis().

The substraction should be with unsigned long. That is all.
You can test it yourself.

Examples of the substraction with unsigned long variables:

// global variable
unsigned long previousMillis;

// in the code
if( millis() - previousMillis ...

Or this:

// global variable
unsigned long previousMillis;

// in the code
unsigned long currentMillis = millis();
if( currentMillis - previousMillis ...

You have fixed that. Thanks.

In the file "mini-weerstation-deel-3.ino", there is variable 'buttonLongPressTime' which is used for two things. One thing is to store the millis() value, but the variable is only an 'int'. The other thing is a boolean state. You make it zero when not used. Using a single variable for two completely different things that conflict with each other is not okay.

@basvdijk
Copy link
Collaborator

Dank, ik ga ernaar kijken :) Ik geef overigens de voorkeur aan Nederlands omdat dit de Nederlandstalige code repo is. Het idee van Bas on Tech is om juist Nederlandse informatie te bieden vandaar dat ik ook de comments in de code in het Nederlands zet. De variabelen en functienamen zijn expres Engels omdat in programmeren Engels veelal de voertaal is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants