LCD 2 lines of text updating at the same time


I am very new to Arduino so this might be a very simple question

I am currently doing this on tinkercad but basically I have 2 temperature sensors to mimic the temperature outside and inside and I want both of the temperatures to show up on the lcd on both separate lines. However I can't seem to get it to work because the text keeps on glitching out on one line.

Here is my code:

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

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int degree_inside;
double realDegree_inside;
int degree_outside;
double realDegree_outside;
String lcdBuffer;
void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  degree_inside = 0;
  realDegree_inside = 0;
  degree_outside = 0;
  realDegree_outside = 0;
  // Print a message to the LCD.

void loop(){
  lcd.print("                ");
  degree_inside = analogRead(0);
  realDegree_inside = (double)degree_inside/1024;
  realDegree_inside *= 5;
  realDegree_inside -= 0.5;
  realDegree_inside *= 100;
  degree_outside = analogRead(1);
  realDegree_outside = (double)degree_outside/1024;
  realDegree_outside *= 5;
  realDegree_outside -= 0.5;
  realDegree_outside *= 100;
  String output_inside = "Inside: " + String(realDegree_inside) + String((char)178) + "C";
  String output_outside = "Outside: " + String(realDegree_outside) + String((char)178) + "C";
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):

Any ideas what I may be doing wrong or maybe any suggestions?

1 Like

So then wouldn't the second line be 0,1 ?

1 Like

Oh wow. That was such a dumb mistake.

Thank you very much!

I just got super confused.
It works perfect now!

Cool code tags used, thanks.

Between "LCD 2 lines of text updating at the same time" and "However I can't seem to get it to work because the text keeps on glitching out on one line." the issue remains vague.

Are you using an Uno?

double is irrelevant on an Uno and is the same as float.

I found that with setting floats to a default or hard coded value to use the 'f' to designate a float. Like this degree_inside = 0.0f; or you may find your floats are converted to ints.

does that pad a bunch of spaces into the LCD, then you add info to those padded spaces, no?

Your code waits too long to convert the value to float, you might end up with ints.

The Uno and the Mega can be tricky with floats, I found being very explicit when using floats works best.

It's also many orders of magnitude more precise than any normal electronic temperature sensor. Actually, even float type is.

Oi! I like using floats and, of course for ML uint64_t's are all the range. I was going with on an Uno a double and a float are the same thing.

That is not true. Any constant you assign to a variable of type float will be properly cast.

On the other hand, initialization to the result of some computation could result in integer math being used prior to the cast. Is that what you meant?

float val = 1000 / 5000 + 2; // value assigned will be 2.0

You may find strange things start to happen after your code has been running for a while because you are using the String class on a real Uno.
Use of the String class can cause memory contamination. Better to use strings (null terminated character arrays).

My experience with Uno's and Mega's has been from time to time the float becomes an int if I store a 0 instead of a 0.0f.

Can you provide an example of that? If that is happening, you may very well be stomping on memory or some other error in your code.

That was my experience 3+ years ago, since then I just 'X.Xf' my floats as a matter of habit. Since I switched over to using ESP32's I do not have anymore Uno's or Mega's. I gave them to a summer STM program. Thus, I'll not be making an attempt to reproduce the matter.

At least, it makes it more explicit for reading the code... although I think I don't need to see the 'f' after it if i see a decimal point...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.