Serial.println and lcd.print not identical

I am still on pulse measuring. I have a strange problem.

Serial.println and lcd.print are not identical

For lcd.print the first digit is always zero, and sometimes it adds one trailing digit.

Code source: Inkling

My change from above code:

        Serial.println(duration / 1000);  // output i.e. : 1707
        lcd.setCursor(0, 1);      
        lcd.print(duration / 1000);    // output i.e. : 0707

The same code in the Arduino LCD sample code works perfectly as expected.

Strange as both derive from the print class . O no that is an assumption. Which LCD class do you use?

can you insert this, to see if there is a pattern in the values where it happens

for (int i=1; i< 32000; i= i*3)
{
  Serial.println(i);  
  lcd.setCursor(0, 1);      
  lcd.print(i); 
  delay(250);  
}

note: println adds a CR/LF pair, print does not.

I use the standard LCD library:

#include <LiquidCrystal.h>

Your code seems identical in the serial mon and on the LCD. However, it seems not to clear the LCD
Last two serials are:

-18207
10915

Last LCD display:

105157 - obviously the 7 was 'leftover'

I found this out, look at:

https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-18/recipe-18-8

When I change:

const int numberOfEntries = 64; // the max number of pulses to measure

To:

const int numberOfEntries = 2; // the max number of pulses to measure

Then I do get an accurate readings, mostly. But when the times get shorter, around 5ms, then the calculations get again wrong. Could the hardware interrupt disturb the software operation?


Update:
Generally it's a good idea to stick to simpler code that one can understand. Now I use the pulseIn() function and so far it works better: