Pages: [1]   Go Down
Author Topic: Serial.println and lcd.print not identical  (Read 807 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:  https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-18/recipe-18-8

My change from above code:

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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

http://arduino.cc/en/Reference/pulseIn

« Last Edit: May 23, 2013, 05:08:43 am by flameproof » Logged

Pages: [1]   Go Up
Jump to: