Wierd and spurious data on 16x2 LCD

Newbie here, but I have managed to get two DS18B20s to display on monitor and the 16x2 LCD. The objective is to have initially fixed text on line 0 "temp in out" and the readings below in line 1 regularly updated. The readings are updated on the monitor at the same time. The code is a blend of two established sketches and does give the results I need, although I imagine it could be more elegant.

I have two mysterious problems:

  1. The temperatures on pins A0 and A1 display in the expected order on the monitor but the other way round on the LCD. The data is assembled at the same time for each temperature.

  2. The text on line 0 is in the setup section. The readings have their cursor settings set on line 1 and properly appear there but data for temp 1 appears on line 0 as well. This is not regularly updated but is if I press the reset button. If I remove the power, this figure is (I think) always 85.00. This extra figure is only visible if the line in the setup section is made shorter.

I attach the code. Most of the rems have been stripped. All the commands for printing the readings to LCD are near the bottom.

/*
Reading two DS18B20s pins 14 &15
display to monitor
display to LCD with pin D04 moved to pin 16 (A2)
Code uses Arduino LCD and sheepdog temp reader
*/

#define tture1 14//no ; here
#define tture2 15//no ; here

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,16,5,6,7);

/*Forward declarations. Only the last two need concern the user
Remmed out, as they seem unnecessary
void OneWireReset(int Pin);//Called by readTture
void OneWireOutByte(int Pin, byte d);//Called by readTture
byte OneWireInByte(int Pin);//Called by readTture

void readTture(byte Pin);//Of use to users
void printTture();//Of use to users
*/

//Following globals used to communicate results back
//from readTture(Pin), and to send data to printTture...

int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;

void setup() {
    //For each tture sensor: Do a pinMode and a digitalWrite
    pinMode(tture1, INPUT);
    pinMode(tture2, INPUT);

    digitalWrite(tture1, LOW);//Disable internal pull-up.
    digitalWrite(tture2, LOW);

    Serial.begin(9600);
    delay(300);//Wait for newly restarted system to stabilize
    Serial.print("Temperature measurement, two sensors:\n\n");

  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("temp   in   out");    
        
}

void loop(){

  readTture(tture1);//N.B.: Values passed back in globals
  printTture();//N.B.: Takes values from globals. Also...
     //no newline part of printTture;
  Serial.print("   ");
  delay(120);// Delay... must not be too short.
  
  // set the LCD cursor to column 6, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(4, 1);
  
  
  readTture(tture2);//Now read and report 2nd tture.
  printTture();
  delay(200);// Delay... must not be too short.
  Serial.print("\n");//Start new line
  
// set the LCD cursor 
  lcd.setCursor(11, 1);
  
  delay(880); 
}


//Everything below here... just copy it into your program "as is".
//You are only likely to need to use readTture(pin) and printTture()
//   directly. Others are subordinate to those.
//These routine access the following global variables...
//   int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;

void OneWireReset(int Pin) // reset.  Should improve to act as a presence pulse
{
     digitalWrite(Pin, LOW);
     pinMode(Pin, OUTPUT); // bring low for 500 us
     delayMicroseconds(500);
     pinMode(Pin, INPUT);
     delayMicroseconds(500);
}
//end OneWireReset

void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
{
   byte n;

   for(n=8; n!=0; n--)
   {
      if ((d & 0x01) == 1)  // test least sig bit
      {
         digitalWrite(Pin, LOW);
         pinMode(Pin, OUTPUT);
         delayMicroseconds(5);
         pinMode(Pin, INPUT);
         delayMicroseconds(60);
      }
      else
      {
         digitalWrite(Pin, LOW);
         pinMode(Pin, OUTPUT);
         delayMicroseconds(60);
         pinMode(Pin, INPUT);
      }
      d=d>>1; // now the next bit is in the least sig bit position.
   }
}//end OneWireOutByte

byte OneWireInByte(int Pin) // read byte, least sig byte first
{
    byte d, n, b;

    for (n=0; n<8; n++)
    {
        digitalWrite(Pin, LOW);
        pinMode(Pin, OUTPUT);
        delayMicroseconds(5);
        pinMode(Pin, INPUT);
        delayMicroseconds(5);
        b = digitalRead(Pin);
        delayMicroseconds(50);
        d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
    }
    return(d);
}//end OneWireInByte

void readTture(byte Pin){
//Pass WHICH pin you want to read in "Pin"
//Returns values in... (See global declarations)
  OneWireReset(Pin);
  OneWireOutByte(Pin, 0xcc);
  OneWireOutByte(Pin, 0x44); // perform temperature conversion, strong pullup for one sec

  OneWireReset(Pin);
  OneWireOutByte(Pin, 0xcc);
  OneWireOutByte(Pin, 0xbe);

  LowByte = OneWireInByte(Pin);
  HighByte = OneWireInByte(Pin);
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;
};//end readTture

void printTture(){//Uses values from global variables.
//See global declarations.
//N.B.: No new line inside printTture
if (SignBit) // If it's negative
    {
     Serial.print("-");
     lcd.print("-");
    };
  Serial.print(Whole);
  Serial.print(".");
  lcd.print(Whole);
  lcd.print(".");
  if (Fract < 10)
    {
     Serial.print("0");
     lcd.print("0");
    };
  Serial.print(Fract);
  lcd.print(Fract);
};//end printTture

Pretty long code for me at this hour, but you should add lcd.setCursor(x,y) in loop before everything. Otherwise it prints to undefined locations, left by previous printing, so who knows where it prints. Do (0,1), so you always know the first few lcd.print will print from (0,1).

Thanks, it looks like that comment gave me enough...

The main loop is in two sections, each of which calls two subroutines to read and write the temperatures. I had the cursor commands already but I have now moved them to the top of each section.

Before, the very first reading went into the top line and the readings in the lower line were actually from different passes through the main loop.

Thanks again.

void loop(){
 
  // set the LCD cursor to column 4, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(4, 1);
  readTture(tture1);//N.B.: Values passed back in globals
  printTture();//N.B.: Takes values from globals. Also...
     //no newline part of printTture;
  Serial.print("   ");
  delay(120);// Delay... must not be too short.
  
  // set LCD cursor for second temp
  lcd.setCursor(11, 1);
  readTture(tture2);//Now read and report 2nd tture.
  printTture();
  delay(200);// Delay... must not be too short.
  Serial.print("\n");//Start new line
   
  delay(880); 
}

You are welcome. lots of us are here to help.