new value is displayed over old values

Hi everybody. I am measuring distance. When the value change, old values are still displayed. I have mcufriend tft 3,5 and arduino mega2560. can anybody help please?

#include <Adafruit_GFX.h>    // Core graphics library
#include <MCUFRIEND_kbv.h>   // Hardware-specific library
MCUFRIEND_kbv tft;

#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif9pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>


#include <FreeDefaultFonts.h>

#define BLACK   0x0000
#define RED     0xF800
#define STALE   0x07E0
#define WHITE   0xFFFF
#define GREY    0x8410



//ULTRASONIC
const int trigPin = 22;
const int echoPin = 24;
long duration;
int distanceCm;


void setup(void)
{   pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input

    uint16_t ID = tft.readID();

    if (ID == 0xD3D3) ID = 0x9481; //force ID if write-only display
    tft.begin(ID);
    tft.setRotation(1);
    Test();
}


void loop(void)
{  
   getDistance();  
}


void Test()
{
    int16_t x1, y1;
    uint16_t wid, ht;
    tft.fillScreen(BLACK);
 
    tft.setFont(&FreeSerif12pt7b);
    tft.setCursor(5, 230);
    tft.setTextColor(STALE);
    tft.setTextSize(1);
    tft.print("hum");
   
    tft.setFont(&FreeSerif12pt7b);
    tft.setCursor(160, 230);
    tft.setTextColor(STALE);
    tft.setTextSize(1);
    tft.print("%");    
}

void getDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distanceCm= duration*0.034/2;
    tft.setFont(&FreeSerif9pt7b);
    tft.setCursor(100, 232);
    tft.setTextColor(WHITE);
    tft.setTextSize(2);
    tft.print(distanceCm);
    }

Run the "Font_simple.ino" example.

It will show how 7x5 font can be drawn with background or transparent
But FreeFonts are always transparent.

You just need to use fillRect() to draw a fresh background before drawing new text.
You can calculate the minimal rectangle required via getTextBounds()
But it is just as easy to draw a fixed rectangle.

If you "remember" the old text, you can re-draw the old text in background colour to rub it out.

David.

solved with the old text. thank you