Partial Clear and Garbage Characters

I am using a 16x2 LCD with I2C backpack and the following libraries:
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

First question:
I would like the same text to always display on the top row.
Only the text on the second will change.

At the moment I have 2 solutions:

  1. Clear the screen, reprint the top row, print the new second row.
  2. Print 16 spaces to the second row, print the new second row.

Both of the above work, but seem more like hacks.
Is there a specific instruction to clear only the second row and not the whole screen?

Second question:
Using the RCSwitch library I am picking up the id code of a generic door/window sensor ala the RCSwitch simple receive example.

//RC Switch
  if (mySwitch.available()) {
    long sensor = mySwitch.getReceivedValue();
    }

When I print the variable "sensor" to the serial monitor it prints fine.
But when I print it to the LCD, it always has 2 garbage characters at the end.
This happens even after the screen has been cleared.
Also all other variables print fine, only "sensor" gives this problem

I know I can probably calculate the length of "sensor" and print 2 blank spaces after it, but is seems weird the problem does not present in serial monitor.

Any insights on this?

Use lcd.setCursor(x, y) to get to your desired position in the display.

Then print your number. It is always wise to pad the number with spaces so that you are overwriting the previous contents. In C you use printf("%4d", number) to automatically format the number nicely.

Unfortunately Arduino does not have an equivalent method of formatting. You have to either use sprintf() or your own formatting function.

An LCD prints "\r" and "\n" as weird graphic characters. A Serial Terminal interprets them as carriage-return and linefeed.
Some LCD libraries will interpret them. The common Arduino libraries do not.

David.

david_prentice:
An LCD prints "\r" and "\n" as weird graphic characters.

Thanks David - this is obviously the culprit.
Once I understood the problem, the fix was easy.
I simply changed

lcd.println(sensor);

to

lcd.print(sensor);
lcd.println("  ");

The alternative is to write a helper function that can format the result befor sending to the LCD. e.g.

void lcd_value_width(int32_t value, uint8_t width)
{
     char buf[20];
     ltoa(value, buf, 10);
     int len = strlen(buf);
     for (uint8_t i = len; i < width; i++) lcd.print(" ");
     lcd.print(buf);
}

Untested. But you would just call with :

     ...
     lcd_value_width(5678, 4);

and get the numbers line up neatly. I get very irritated by the Arduino unformatted print.

david_prentice:
The alternative is to write a helper function that can format the result befor sending to the LCD.

Well I actually started doing that, when I had the idea to simply print 2 spaces.
It only works because the cursor position does not change when the LCD prints the 2 weird characters.
My data is always less than 14 characters and with this fix the data is presented exactly the way I want, so I don't really need a helper function in this case.

aisc:

lcd.print(sensor);

lcd.println("  ");

Still wrong! :astonished:

lcd.print(sensor);
lcd.print("  ");

Do not use "lcd.println()"! It is meaningless.