Page 1 of 1

millis() returns zero

Posted: 17 May 2019 16:34
by perjar
Dear forum,

I have encountered an issue that I can't quite understand. The issue is that the millis() function sometimes returns 0 instead of the correct timestamp, only to continue to report the correct value in the next call.

Over-simplified code for illustrational purposes:

Code: Select all

loop {
   currenttime = millis();
   Serial.println(currenttime);
   delay(2000);
}
This can give a printout as follows:

Code: Select all

>341020
>343021
>345021
>0
>349023
I am on 2.1.4.

Any ideas?

Re: millis() returns zero

Posted: 19 May 2019 19:23
by PoltoS
Could you please try it in 2.1.5?

Re: millis() returns wrong value

Posted: 27 May 2019 22:02
by perjar
I have upgraded to 2.1.5 now. Same result.

Let me adjust my initial problem statement: It also happens that millis() returns a value which is not zero, but it is lower than the previous call.

My observations so far indicate that the millis() function is set back about 60 seconds from time to time.

Example:

Code: Select all

1: 19:53:36.837 -> currenttime= 82230145, previoustime =82225387, elapsed = 4758
2: 19:53:41.709 -> currenttime= 82234936, previoustime =82230145, elapsed = 4791
3: 19:53:46.703 -> currenttime= 82239695, previoustime =82234936, elapsed = 4759
4: 19:53:51.422 -> currenttime= 82244427, previoustime =82239695, elapsed = 4732
5: 19:53:56.277 -> currenttime= 82183619, previoustime =82244427, elapsed = 4294906488
6: 19:54:01.116 -> currenttime= 82188352, previoustime =82183619, elapsed = 4733
7: 19:54:05.962 -> currenttime= 82193086, previoustime =82188352, elapsed = 4734
The sketch behind this printout counts pulses from an electrical meter and based on that calculates power consumption.
"elapsed" is an unsigned long, hence the huge number, resulting from the substraction currenttime - previoustime.

As you can see in the printout, pulse 5 gets a millis() value which is lower than pulse 4. 60808 milli sec lower to be precise. I have monitored this for a few days now and it appears that when this happens, the clock is set back between 60-66 seconds. (recent observations: 61029, 65376, 63069 and 65404 milliseconds)

Re: millis() returns zero

Posted: 06 Jun 2019 22:47
by PoltoS
Please try the following fix in HLCore.cpp:
dword millis() {
noInterrupts(); // <-Add
g_ms_counter += zunoGI("TMR");
zunoSI("TMR",0);
interrupts(); // <-Add
return g_ms_counter;
}

Looks like interrupts are making troubles.

Would be perfect if you report us back your results as we can not reproduce it.