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)
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.