Hi,
As a hobby project I am doing a Water meter sensor based on Z-Uno and I had issues with sporadic extremely high water flow values.
I found that there were sporadic jumps in the millis() value which I use in calculation of the water flow.
I searched the forum to see if someone else had reported this problem and found this thread that exactly match what I also have seen.
Since I'm running Z-Uno version 2.1.5 I can sadly say that the suggested fix didn't solve the problem. I can see that the fix is implemented in HLCore.cpp in 2.1.5.
I changed my sketch from using millis() function for calculating water flow to instead use ZUNO_1MS_TIMER() to increment a counter and the issues disappered.
To investigate the issue with sporadic jumps in the millis() value even more, I wrote the supplied sketch to test how the millis() function behaves compared to the ZUNO_1MS_TIMER().
I found out that millis() actually do have issues with sporadic jumps and there are also some drift issues.
When looking at the log, as supplied, I can see that the millis() function seem to have problems when wrapping the low 16-bits (from 0x****FFFF to 0x****0000.)
- Most times it goes OK.
- Sometimes it jumps +256.
- Sometimes it jumps -65536.
In the provided log you can see that when the +256 or -65536 jump occurs the 16 low bits of the mills() value has just wrapped around.
As also can be seen from the log:
- millis() is drifting compared to the 1MS_TIMER and is about 1 ms slower per 20-50 s.
What can be the reason for that? Can it be fixed?
- The 1MS_TIMER is drifting compared to the PC clock and is about 100 ms slower per 10 s.
- What is the expected accuracy of the 1MS_TIMER?
- Can the accuracy be determined from the datasheet of the Z-Wave chip?
- Can the accuracy of the 1MS_TIMER be improved?
To summarize:
- The millis() value sometimes jumps +256 or -65536 and the suggested fix does not fix the problem.
- millis() is about 1 ms slower per 20-50 s than 1MS_TIMER
- The 1MS_TIMER is about 100 ms slower per 10 s than the PC clock.
Code: Select all
ZUNO_SETUP_ISR_1MSTIMER(int_1ms_handler);
unsigned long int1ms = 0;
unsigned long last_millis = 0;
boolean print_it;
void setup() {
Serial0.begin(115200);
Serial0.println("int1ms, millis, diff");
}
void loop() {
unsigned long local_int1ms = int1ms;
unsigned long curr_millis = millis();
if (curr_millis < last_millis) {
Serial0.print("ERROR: ");
Serial0.print(last_millis);
Serial0.print(", ");
Serial0.print(curr_millis);
Serial0.print(", ");
Serial0.println((long)(curr_millis - last_millis));
}
if (print_it) {
print_it = false;
Serial0.print(local_int1ms);
Serial0.print(", ");
Serial0.print(curr_millis);
Serial0.print(", ");
Serial0.println((long)(local_int1ms - curr_millis));
}
last_millis = curr_millis;
}
void int_1ms_handler(void) {
if (int1ms % 10000 == 0)
{
print_it = true;
}
++int1ms;
}
Code: Select all
18:33:32.238 -> int1ms, millis, diff
18:33:32.238 -> 2, 3, -1
18:33:42.450 -> 10007, 10007, 0
18:33:52.575 -> 20014, 20014, 0
18:34:02.641 -> 30004, 30004, 0
18:34:12.768 -> 40013, 40013, 0
18:34:22.854 -> 50004, 50004, 0
18:34:32.985 -> 60012, 60011, 1
18:34:43.041 -> 70001, 70000, 1
18:34:53.159 -> 80009, 80008, 1
18:35:03.243 -> 90017, 90016, 1
18:35:13.354 -> 100005, 100003, 2
18:35:23.458 -> 110015, 110013, 2
18:35:33.542 -> 120004, 120001, 3
18:35:43.645 -> 130013, 130010, 3
18:35:53.748 -> 140003, 140000, 3
18:36:03.839 -> 150012, 150009, 3
18:36:13.929 -> 160002, 159999, 3
18:36:24.065 -> 170012, 170009, 3
18:36:34.151 -> 180002, 179999, 3
18:36:44.255 -> 190017, 190013, 4 // 190013 = 0x0002e63d
18:36:54.327 -> 200007, 200259, -252 // About a +256 jump in millis() value
18:37:04.458 -> 210021, 210273, -252
18:37:14.548 -> 220013, 220265, -252
18:37:24.647 -> 230006, 230258, -252
18:37:34.747 -> 240018, 240270, -252
18:37:44.835 -> 250009, 250261, -252
18:37:54.926 -> 260001, 260252, -251 // 260252 = 0x0003 f89c
18:37:56.874 -> ERROR: 262143, 196627, -65516 // About a -65536 jump in millis() value,
18:38:05.046 -> 270006, 204721, 65285
18:38:15.134 -> 280019, 214734, 65285
18:38:25.212 -> 290012, 224727, 65285
18:38:35.326 -> 300005, 234720, 65285
18:38:45.429 -> 310000, 244716, 65284
18:38:55.534 -> 320013, 254727, 65286
18:39:05.636 -> 330006, 264720, 65286
18:39:15.732 -> 340020, 274734, 65286
18:39:25.836 -> 350014, 284728, 65286
18:39:35.935 -> 360009, 294723, 65286
18:39:46.033 -> 370003, 304716, 65287
18:39:56.123 -> 380017, 314730, 65287
18:40:06.231 -> 390016, 324729, 65287
18:40:16.305 -> 400010, 334723, 65287