Update LCD display when potentiometer changes

Im new to programming and im sure this is easy for someone with a little experience, but i just cant work out how to do it.

When the arduino takes the reading from the potentiometer it maps it from 0 - 100 and displays it on the TFT LCD screen. If the reading goes up to 100 and back down the last 0 remains on the screen, making all the numbers look like they are 10x bigger (eg 990, 980 etc) . I can get round this by painting the numbers black before the update but this causes the numbers to flicker

How do i track the potentiometer value so the display only updates if it changes.I have found plenty of material on tracking a digital input but not on analogue. What i have done so far just isnt working for me so i have posted the code with what i have working.

Thanks in advance

#include <UTFT.h>

// Declare which fonts we will be using
extern uint8_t SevenSegNumFont[];

UTFT myGLCD(ITDB50,38,39,40,41);   // Remember to change the model parameter to suit your display module!
//UTouch      myTouch(6,5,4,3,2);


const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)  
int lastoutputValue = 0;   



void setup()
{
  Serial.begin(9600); 
// Setup the LCD
  myGLCD.InitLCD();
  myGLCD.clrScr();
}
  void loop()
{
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 100);  
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);   
  Serial.print("\t lastoutputvalue = ");      
  Serial.println(lastoutputValue);
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);   
  
  if (outputValue != lastoutputValue);
  {
    myGLCD.setColor(0, 0, 0);
  myGLCD.fillRoundRect (5, 5, 150, 53);
  myGLCD.setColor(0, 255, 255);
  myGLCD.setFont(SevenSegNumFont);
  myGLCD.printNumI(int(outputValue), 5, 5);
    int lastoutputValue = outputValue;
  }
}

A trick a lot of people use with various displays is,
You need to display a max of 3 chars ( 100? ) so if the number is less than 100 add a space at the end, if less than 10 add two spaces. Might save tracking numbers.

This is easy to do if you concatenate your string before printing. (itoa)

It would be nice to know how to track the potentiometer and do something if the value is different from before. Im working on a bigger project and the process would help with with other parts of the project, not only with this snippet but other parts require large sub routines. if i can track the change i can set the condition so if there is no change a large part of code can be skipped.

Try using the sprintf() function to format your output. If you only want to update on a reading value change you just need to remember the last reading. If the last reading and the current reading aren't equal then update the display.

I went back and read the code. You should declare lastoutputValue globally. Currently it's scoped only to that display loop. You tried but that int spoils it all.

Thanks Jimmy

by removing the int from the "int lastoutputValue = outputValue;" line and i can see in the serial monitor the inputValue and LastinputValue changes as the potentiometer increases/decreases and settles to be the same when not turned. from these 2 values i would have thought the below part of the code would be enough to check it and run the next part if the values are different.

Why would the myGLCD part run if the input and output value are the same?

 if (outputValue != lastoutputValue);
  {
    myGLCD.setColor(0, 0, 0);
  myGLCD.fillRoundRect (5, 5, 150, 53);
  myGLCD.setColor(0, 255, 255);
  myGLCD.setFont(SevenSegNumFont);
  myGLCD.printNumI(int(outputValue), 5, 5);
   lastoutputValue = outputValue;
  }

Why would the myGLCD part run if the input and output value are the same?

Because of the semi colon at the end ofif (outputValue != lastoutputValue);