# mills value

hello friends,

I am doing a project to switch two Air conditioners in between 2 hour ,4 hour and 6 hour…the value 2,4,6 is depends on the voltage in AD5 from a potentiometer …the program i used is attach below.in this program i used mills timer… here i used an instruction

unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)

from my search in Google i got Millis() uses unsigned longs which is a 32 bit number. Translates to around 4 billion millseconds (4294967296 if you’re feeling pedantic) or 49 days and 17 hours…

here is my doubt.
-what will be the value of current mills after 50 days?
-my project will work correctly after 50 days?
-can we reset the value of mills() by program before 50 days?

below i attached my program for your kind perusal… i wish the valuable suggestions and helps from my friends… thank you very much

``````int t1=0,t2=0,relay_position=0;
int relay = 9;
unsigned long previousMillis = 0;        // will store last time LED was updated

const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(relay, OUTPUT);
digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
delay(2000);              // wait for a second
digitalWrite(13, LOW);
}

void loop() {
Serial.println(sensorValue);
if((sensorValue==1023)||(sensorValue >700) )
{
t2=21599;
}
if((sensorValue<701)&&(sensorValue >150) )
{
t2=14399;
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}
if((sensorValue==0)||(sensorValue <151) )
{
t2=7199;
}

unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
t1++;
if(t1>t2)
{
t1=0;
}
}
}
``````

from my search in Google i got Millis() uses unsigned longs which is a 32 bit number. Translates to around 4 billion millseconds (4294967296 if you're feeling pedantic) or 49 days and 17 hours........

here is my doubt. -what will be the value of current mills after 50 days?

About 7 (hours) * 60 (minutes) * 60 (seconds) * 1000 (milliseconds per second).

-my project will work correctly after 50 days?

If you follow the simple guidelines, yes.

can we reset the value of mills() by program before 50 days?

You can, but why would you?

ecworks: here i used an instruction

unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval)

That will give the correct answer even when millis() rolls-over provided that interval is less than 49 days

...R

Declare both time variables as uint64_t and add a line before your if statement.

``````uint64_t currentMillis, previousMillis;

currentMillis = millis();

if( currentMillis < previousMillis )   // Account for millis() rollover
currentMillis += 0x100000000;      // Add 2^32

if (currentMillis - previousMillis >= interval)
{
// Do something
previousMillis = currentMillis;    // Update time value
}
``````

Sorry Eddie, that's the wrong way to do it. Robin's answer is correct. For the full explanation, read Nick Gammon's page on millis.

Remember this time-check happens a few thousand times per second in a well-written Arduino program. Promoting an integer to 64-bit is expensive, particularly on an 8-bit processor. Ordinarily I would say "Don't optimise unless you have measurements to show you need to optimise," but this is an obvious de-optimisation.

Agree.

sorry friends i dont understand...

please help me. tell m my program will work fine? i ned to do any modification in my program? whats final value of mills()? what will happen after the final value of mills()?

ecworks: sorry friends i dont understand...

please help me. tell m my program will work fine? i ned to do any modification in my program? whats final value of mills()? what will happen after the final value of mills()?

millis() returns an 32 bit unsigned integer, so after it reaches its max value of 4294967296, it starts again at zero. It will do that forever and ever.

your code looks OK in that it uses unsigned subtraction.

More on that here...

BulldogLowell:
millis() returns an 32 bit unsigned integer, so after it reaches its max value of 4294967296, it starts again at zero. It will do that forever and ever.

your code looks OK in that it uses unsigned subtraction.

More on that here…

thank you sir, now its more clear…

sir here i used an instruction < if (currentMillis - previousMillis >= interval)>
so after the mills become zero then my current mills=0, previous mill=~4294966296,interval=1000…so the condition inside ‘if’ will become
if(0-4294966296>=1000) what will happen here? it will get inside if condition or not?

Unsigned maths is not usually taught in schools. What do you think is the result of putting -4294966296 into an unsigned 32-bit integer? Try it. Make a small test program to print this out.

ecworks:
thank you sir, now its more clear…

sir here i used an instruction < if (currentMillis - previousMillis >= interval)>
so after the mills become zero then my current mills=0, previous mill=~4294966296,interval=1000…so the condition inside ‘if’ will become
if(0-4294966296>=1000) what will happen here? it will get inside if condition or not?

did you forget you are updating previousMillis every interval?

``````previousMillis = currentMillis;    // Update time value
``````

Are you saying you don’t yet believe (i.e. understand) Nick’s and the Arduino explanations provided?

BulldogLowell:
did you forget you are updating previousMillis every interval?

``````previousMillis = currentMillis;    // Update time value
``````

Are you saying you don’t yet believe (i.e. understand) Nick’s and the Arduino explanations provided?

no no sir, i believe .but i wish to make it clear…i am not very good in programming as well as english… sorry for that…
sir i know we update the value of previous mills, but its only inside the if condition…after overflow previousmills need to come inside the if loop to update its value… its my doubte
forgive me if i am saying foolishness… thank you

Try this out:-

``````void setup()
{
Serial.begin(115200);
unsigned long prevMillis = 4294966296, millisAfterRollover = 0;
int interval = 1000;

if ((millisAfterRollover - prevMillis) >= interval)
Serial.println(F("All OK"));
else
Serial.println(F("Failed!"));
}

void loop() {}
``````

.