Overlap a temperature when I click in a specific position

Hi guys I have a code that shows in the tft display 2 temperatures, but when I click in a specific area I want to overlap them separately, but when I click in the specific area the screen refresh and both temperatures (that wasnt in the screen before) just apear at the same time.

Note: "criarBotao" means create a square.

#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>
#include <Adafruit_GFX.h>
#include "Nanoshield_Termopar.h"
#include <SPI.h>
#include "Nanoshield_LCD.h"

Nanoshield_Termopar termopar(52 , TC_TYPE_K, TC_AVG_OFF );
Nanoshield_Termopar termopar2(50 , TC_TYPE_K, TC_AVG_OFF );

Nanoshield_LCD lcd;
#define XM A2

#define YP A3
#define YM 9
#define XP 8

#define TS_MINX 90
#define TS_MINY 92
#define TS_MAXX 906
#define TS_MAXY 951

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_RESET A4
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0


#define PRETO   0x0000
#define BRANCO 0xFFFF
#define AMARELO 0x0ff00
#define VERDE   0x07E0
#define VERMELHO 0xF800
MCUFRIEND_kbv tft;

#define MINPRESSURE 10
#define MAXPRESSURE 1000

bool error = false;
bool valor_botao1 = false;
bool valor_botao2 = false;

void telaInicial();
void setup() {

  Serial.begin(9600);
  termopar.begin();
  termopar2.begin();
  uint16_t ID = tft.readID();
  tft.begin(ID);
  tft.setRotation(1);
  tft.fillScreen(PRETO);

  telaInicial();
}

void loop() {
  TSPoint p = ts.getPoint();
  pinMode(XM, OUTPUT);
  digitalWrite(XM, LOW);
  pinMode(YP, OUTPUT);
  digitalWrite(YP, HIGH);
  pinMode(YM, OUTPUT);
  digitalWrite(YM, LOW);
  pinMode(XP, OUTPUT);
  digitalWrite(XP, HIGH);

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    p.x = tft.width() - (map(p.x, TS_MINX, TS_MAXX, tft.width(), 0));
    p.y = tft.height() - (map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));

 if (p.y > 10 && p.y < 85) { 
  termopar.read();

  tft.setTextColor(PRETO);

  tft.setCursor(196, 150); 
  tft.print(termopar.getExternal());
  Serial.println (termopar.getExternal());
  Serial.println ("");
 }
 else {
  criarBotao(186, 130, 125, 115, "", PRETO); 
 }
  if (p.x > 75 && p.x < 170){
  termopar2.read();

  tft.setTextColor(PRETO);

  tft.setCursor(196, 30); 
  tft.print(termopar2.getExternal());
  Serial.println (termopar2.getExternal());
  Serial.println ("");
  delay (1000);}
else {
  criarBotao(186, 10, 125, 115, "", PRETO); 
}
 
  Serial.println ("");
  Serial.println ("");
  Serial.println ("");
  Serial.println ("");
  Serial.println ("");
  Serial.println ("");
  Serial.println ("");
  Serial.println ("");
  Serial.println ("");
  Serial.println ("");
  Serial.println ("");
  Serial.println ("");
  Serial.println ("");
  
     tft.fillScreen (PRETO);
   criarBotao(186, 130, 125, 115, "", BRANCO);
   criarBotao(186, 10, 125, 115, "", BRANCO);
  criarBotao(20, 10, 125, 115, "", BRANCO); 
  criarBotao(20, 130, 125, 115, "", BRANCO);
 escreveEstado(32, 50, "Temp_1", PRETO);
  escreveEstado(32, 170, "Temp_2", PRETO);
delay (1500);
}
}
void telaInicial() {
  tft.setRotation(3);
  tft.fillScreen(BRANCO);

  criarBotao(186, 130, 105, 95, "", BRANCO);
  criarBotao(186, 10, 105, 95, "", BRANCO);
  criarBotao(20, 10, 105, 95, "", BRANCO); 
  criarBotao(20, 130, 105, 95, "", BRANCO); 
  
  escreveEstado(32, 50, "Temp_1", BRANCO);
  escreveEstado(32, 170, "Temp_2", BRANCO);
  escreveEstado(140, 30, "Ext:", AMARELO);
  escreveEstado(140, 60, "Int:", AMARELO);
  escreveEstado(140, 150, "Ext:", AMARELO);
  escreveEstado(140, 180, "Int:", AMARELO);
}

void escreveEstado(int posx, int posy, String texto, int cor) {
  tft.setCursor(posx, posy);
  tft.setTextColor(cor);
  tft.setTextSize(2);
  tft.print(texto);
}

void criarBotao(int posx, int posy, int largura, int altura, String texto, int cor) {
  tft.fillRect(posx, posy, largura, altura, cor);
  tft.drawRect(posx, posy, largura, altura, PRETO);
  tft.setCursor(posx + 12, posy + 1000);
  tft.setTextColor(BRANCO);
  tft.setTextSize(3);
  tft.print(texto);
}

void atualizarBotao(int posx, int posy, int cor) {
  tft.fillRoundRect(posx, posy, 210, 48, 5, cor);
  escreveEstado(295, posy + 14, valor_botao1 ? "" : "", cor == VERDE ? BRANCO : PRETO);
}

Are you certain these lines need to be in "loop()"? The "touchscreen.h" example configures those pins in the object instance creation...

Humm let me see

Nothing changed

Put print statements in setup() (entering and exiting) to see if your mcu is resetting and starting from zero, or if the sketch stays in loop()

I tried it and it didn't work, it's a problem with the "if" and "else"

What did not work? If the print statements did not work, that means you have not uploaded the sketch.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.