Problem with displaying optical encoder and load cell on LCD

Good day. I would appreciate your help with displaying optical and load cell values on an LCD display.

The project involves measuring carbon fiber as it is wound on a spool as well as checking the tension on the carbon. There are two push buttons that can be used to reset the measured length and zeroing the tension.

I am using an Arduino Nano, LCD 16x2, optical encoder, load cell with amplifier and two push buttons.

My problem is that the LCD is not displaying the characters correctly unless I clear the screen completely which then cause flickering.

If I do not clear the screen in the loop, the problem I am facing is when the values increase to two or more characters, and the values decrease again, the characters are not cleared properly which displays the incorrect information.

I have attached photographs of the problem. I start at 0cm and measure up to 12cm, the encoder is turned in the opposite direction which means the measurement should decrease to 8cm but because the previous character is not cleared it shows a value of 80cm.

Photograph 1 is the start of the measurement at 0 length
Photograph 2 measures the length at 12cm
Photograph 3 is supposed to show 8cm but shows 80cm.

Please see my code below which clears the screen in the loop and causes the flickering:

//LCD:  VSS: GND
//      VDD: 5v
//      V0: 10k POT WIPER
//      RS: PIN 12
//      RW: GND
//      E: PIN 11
//      D4: PIN 4
//      D5: PIN 5
//      D6: PIN 6
//      D7: PIN 7
//      A: 5v
//      K: GND
//     10k POT 5v & GND
// Encoder:
//      0V BLACK: GND
//      RED: 5V
//      GREEN A: PIN 2
//      WHITE B: PIN 3
//Load Cel
//      0V
//      5VDC
//      D9: Data
//      D8: Clock
//Digital inputs for zero-ing scale and load cell
//      D10: Length zero
//      D13: Call tare function to zero load cell


//Include LCD library
#include <LiquidCrystal.h>
#include "HX711.h"

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
#define encoderPinA 2
#define encoderPinB 3

// initialise the load cell pins
const int LOADCELL_DOUT_PIN = 9;
const int LOADCELL_SCK_PIN = 8;
//create scale object
HX711 scale;

volatile long counter = 0;

//digital inputs for reset functions
const int length_reset = 10;
const int load_reset = 13;

void setup() {
  //Serial.begin (9600); for debugging
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2); // Initialsie the LCD and define the geometry

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); 
  scale.set_scale(3380.f);// this value is obtained by calibrating the scale with known weights; see the README for details
  //scale.tare();// reset the scale to 0 
  
  //setup the encoder pins and attach to interrupt
  pinMode(encoderPinA, INPUT_PULLUP);
  pinMode (encoderPinB, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(encoderPinA), isr, CHANGE);

  //setup input pins
  pinMode(length_reset, INPUT);
  pinMode(load_reset, INPUT);

  //setup lcd text
  lcd.setCursor(0,0);
  lcd.print("Length-cm:");
  lcd.setCursor(0,1);
  lcd.print("Tension:");
}

void loop() {

  bool length_reset_true = (digitalRead(length_reset));
  if(length_reset_true){
    counter = 0;
  }
  
  bool load_reset_true = (digitalRead(load_reset));
  if(load_reset_true){
    scale.tare();
  }
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Length-cm:");
  lcd.setCursor(0,1);
  lcd.print("Tension:");
  lcd.setCursor(10,0);
  lcd.print((round(counter*0.12880519)/100));
  lcd.setCursor(9,1);
  lcd.print(scale.get_units(), 0);
  delay(50);
  
 
      
    }

void isr() {
  if (digitalRead(encoderPinA) == HIGH) {
    if (digitalRead(encoderPinB) == LOW) {
      counter++;
    } else {
      counter--;
    }
    if (counter < 0){
      counter=0;}
   }
      
  }

the problem I am facing is when the values increase to two or more characters, and the values decrease again, the characters are not cleared properly which displays the incorrect information.

If you print say a 3 digit number starting at a position on the LCD and subsequently print a 2 digit number at the same location then the third digit of the first number will still be on the screen.

To solve the problem either always print spaces to the position that the number will be printed at or always follow the printing of numbers by enough spaces to erase previously written digits

Thank you UKHeliBob. Could you perhaps assist with the code on how to do that?

Could you perhaps assist with the code on how to do that?

Sure thing.

lcd.print(' ');

Call this function as many times as needed, before or after printing the data of interest, to wipe out any non-overprinted data.

It would REALLY help if you defined whether the data to be shown should be left-justified, right-justified, center-justified, printed upside down, etc.

laurence_fourie:
Thank you UKHeliBob. Could you perhaps assist with the code on how to do that?

This portion of your code prints a value at position 10, 0 on the LCD.

  lcd.setCursor(10, 0);
  lcd.print((round(counter * 0.12880519) / 100));

I am sure that you can work out how to print enough spaces at 10, 0 to erase the previously printed value before positioning the cursor back to 10, 0 and printing the new value