Variable splice when I print it on 3.5 TFT ILI9486

I am doing a temperature control but I am having problems when I try to display the variables on the screen, they update but the previous value is not erased and they are spliced ​​every time I change the temperature.

I have declared a global variable
int Thermostat = 40;

Already try with tft.print (""); and keep doing the same. In the serial monitor if it works correctly, the problem is when displaying on the screen.

someone could help me with this please? I would appreciate it very much.

I leave a bit of the code to see what I'm doing:

int Termostato=40;

void drawHomeScreen() {

// I draw the screen in this section

}


//I USE THIS SECTION TU PRINT THE VARIABLE

void VerVariables() {
  
tft.setCursor(35,140);
tft.setTextSize(5);
tft.setTextColor(WHITE);
tft.print(Termostato);
tft.print(" C");

}

void setup() {
 
  Serial.begin(9600);
    uint16_t ID = tft.readID();
    Serial.print("TFT ID = 0x");
    Serial.println(ID, HEX);
    Serial.println("Calibrate for your Touch Panel");
    if (ID == 0xD3D3) ID = 0x9486; // write-only shield
    tft.begin(ID);
    tft.setRotation(0);            //PORTRAIT
    tft.fillScreen(BLACK);
    drawHomeScreen();
    
  BTMas.initButton(&tft, 51, 221, 47, 47 , GREEN, GREEN, WHITE,"+",4);
  BTMas.drawButton(false);
  BTMenos.initButton(&tft, 131, 221, 47, 47 , GREEN, GREEN, WHITE,"-",4);
  BTMenos.drawButton(false);

}

void loop() { 

bool down = Touch_getXY();

VerVariables();


//############ MORE TEMPERATURE

BTMas.press(down && BTMas.contains(pixel_x, pixel_y));
        
    if (BTMas.justReleased()){

  Termostato = Termostato + 5;
  
  tft.setCursor(35,140);
  tft.setTextSize(5);
  tft.setTextColor(WHITE);
  tft.print(Termostato);
  tft.print(" C");

  Serial.print(Termostato);
  Serial.println(" C");
  delay (50);
  
}

//########### LESS TEMPERATURE        
    
 BTMenos.press(down && BTMenos.contains(pixel_x, pixel_y));
        
    if (BTMenos.justReleased()){

  Termostato = Termostato - 5;

  tft.setCursor(35,140);
  tft.setTextSize(5);
  tft.setTextColor(WHITE);
  tft.print(Termostato);
  tft.print(" C");

  Serial.print(Termostato);
  Serial.println(" C");
  delay (50);
  
}


}

try using tft.setTextColor(WHITE,BLACK);Since you didn't put the complete sketch i can't really see what libraries you are using, but .setTextColor(FORGROUND,BACKGROUND) usually does the trick (excepting the use of exotic fonts)

Have you tried printing five or six spaces at those coordinates before you print the temperature?

sorry, these are the libraries im using

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
#include <MAX6675_Thermocouple.h>

#include <stdint.h>
#include "TouchScreen.h"

Use this:

tft.fillRect (30, 130, 90, 90, RED);//clears prev readings
tft.setCursor(35,140);
tft.setTextSize(5);
tft.setTextColor(WHITE);
tft.print(Termostato);

I suggest a color change so you can see where its located at first. Say RED

Then change the co-ords to fit the previous reading

Since you are using

#include <Adafruit_GFX.h>

My suggestion will do the trick. Just look at this function that does the actual work from Adafruit_GFX.h

// Draw a character
void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
    uint16_t color, uint16_t bg, uint8_t size) {

  if((x >= _width)            || // Clip right
     (y >= _height)           || // Clip bottom
     ((x + 6 * size - 1) < 0) || // Clip left
     ((y + 8 * size - 1) < 0))   // Clip top
    return;

  for (int8_t i=0; i<6; i++ ) {
    uint8_t line;
    if (i == 5) 
      line = 0x0;
    else 
      line = pgm_read_byte(font+(c*5)+i);
    for (int8_t j = 0; j<8; j++) {
      if (line & 0x1) {
        if (size == 1) // default size
          drawPixel(x+i, y+j, color);
        else {  // big size
          fillRect(x+(i*size), y+(j*size), size, size, color);
        } 
      } else if (bg != color) {
        if (size == 1) // default size
          drawPixel(x+i, y+j, bg);
        else {  // big size
          fillRect(x+i*size, y+j*size, size, size, bg);
        }
      }
      line >>= 1;
    }
  }
}

and note the drawing of the 'bg' pixel here

else if (bg != color) {
        if (size == 1) // default size
          drawPixel(x+i, y+j, bg);
        else {  // big size
          fillRect(x+i*size, y+j*size, size, size, bg);
        }
      }

So no need to first completely wipe the screen using fillRect() or by printing white spaces (would that even work ? )
If your new variable size (on screen) is smaller than the old one you will have to let it either be followed or preceded by as many white spaces as characters are lacking.

thank you all for your comments and suggestions. I could fix the problem. I print a fillrect berofe the variable and problem solve.

here the part of the code I modified:

void loop(void) { 


bool down = Touch_getXY();

VerVariables();

//############ AUMENTAR TEMPERATURA

BTMas.press(down && BTMas.contains(pixel_x, pixel_y));
        
    if (BTMas.justReleased()){

  tft.fillCircle(50, 220, 29, WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(41,205);
  tft.setTextSize(4);
  tft.print("+");

  Termostato = Termostato + 5;
  
  tft.fillRect (30, 130, 165, 50, BLACK);  //THIS IS THE CODE I ADDED 
  tft.setCursor(35,140);
  tft.setTextSize(5);
  tft.setTextColor(WHITE);
  tft.print(Termostato);
  tft.print(" C");
  Serial.print(Termostato);
  Serial.println(" C");
  delay (25);

  tft.drawCircle(50, 220, 30,WHITE );
  tft.setCursor(41,205);
  tft.setTextSize(4);
  tft.setTextColor(WHITE);
  tft.fillCircle(50, 220, 29, GREEN);
  tft.print("+");
  
}

//########### BAJAR TEMPERATURA        
    
 BTMenos.press(down && BTMenos.contains(pixel_x, pixel_y));
        
    if (BTMenos.justReleased()){

  tft.drawCircle(130, 220, 30,WHITE);
  tft.setCursor(121,205);
  tft.setTextSize(4);
  tft.fillCircle(130, 220, 29, WHITE);
  tft.setTextColor(BLACK);
  tft.print("-");

  Termostato = Termostato - 5;
  
  tft.fillRect (30, 130, 165, 50, BLACK); // THIS IS THE CODE I ADDED 
  tft.setCursor(35,140);
  tft.setTextSize(5);
  tft.setTextColor(WHITE);
  tft.print(Termostato);
  tft.print(" C");
  Serial.print(Termostato);
  Serial.println(" C");
  delay (25);

  tft.drawCircle(130, 220, 30,WHITE );
  tft.setCursor(121,205);
  tft.setTextSize(4);
  tft.fillCircle(130, 220, 29, GREEN); 
  tft.setTextColor(WHITE);
  tft.print("-");
  
}