Millis issue, sudden refreshing

Basicaly I have a code that reads a temperature sensor and gives the temperature when you press the button (on tft) "refresh". The problem is that sometimes it refresh without touching the button and I have a python code that read the serial and print the temperature in a table, but it has a line limit so it desapears with every reading. The point is, I want to refresh only when the button is pressed. I also havea version of this code that only uses a simple delay of 1s.

#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(43, TC_TYPE_K, TC_AVG_OFF);
Nanoshield_Termopar termopar2(44, 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

MCUFRIEND_kbv tft;

#define MINPRESSURE 10
#define MAXPRESSURE 1000

#define PRETO   0x0000
#define BRANCO  0xFFFF
#define AMARELO 0xFFE0

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

int px = 150;
int py = 128;
int largura = 150;
int altura = 205;
int tolerancia = 0;
unsigned long tempoAnterior = 0;
const long intervalo = 1000;

void telaInicial();
void telaBoasVindas();  

void setup() {
  Serial.begin(9600);
  termopar.begin();
  termopar2.begin();
  uint16_t ID = tft.readID();
  tft.begin(ID);
  tft.setRotation(1);

  telaBoasVindas();
  telaInicial();
}

void loop() {
  unsigned long tempoAtual = millis();
  
  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.x > px - tolerancia && p.x < px + largura + tolerancia &&
          p.y > py - tolerancia && p.y < py + altura + tolerancia) {

        if (tempoAtual - tempoAnterior >= intervalo) {  
          tempoAnterior = tempoAtual; 

          tft.fillRect(186, 30, 85, 30, PRETO);
          tft.fillRect(186, 170, 85, 30, PRETO);

          termopar.read();
          termopar2.read();

          tft.setTextColor(BRANCO);
          tft.setCursor(200, 179); 
          tft.print(termopar.getExternal() - 1.25);  

          tft.setCursor(200, 40);   
          tft.print(termopar2.getExternal());  
        }
      }
  }
}

void telaBoasVindas() {
  tft.setRotation(3);
  tft.fillScreen(BRANCO);
  tft.setTextColor(PRETO);
  tft.setTextSize(3);
  tft.setCursor(85, 100);  
  tft.print("TFJ V3.0");
  delay(5000);
  tft.fillScreen(PRETO);
  if (termopar.hasError()) {
    tft.setTextColor(AMARELO);
    tft.setTextSize(1);
    tft.setCursor(85, 80);  
    tft.print("Termopar 1 com erro");
  }
}

void telaInicial() {
  tft.setRotation(3);
  criarBotao(186, 145 , 90, 85, "", PRETO);
  criarBotao(186, 10, 90, 85, "", PRETO);
  criarBotao(20, 100, 120, 50, "", PRETO); 

  escreveEstado(92, 42, "Temp_1", AMARELO);
  escreveEstado(92, 176, "Temp_2", AMARELO);
  escreveEstado(42, 115, "REFRESH", 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);
}

what button?

escreveEstado(42, 115, "REFRESH", AMARELO);

You may have to add some code to filter out phantom readings from your touchscreen. Even something as simple as "N in range readings in a row before a touch is declared".

1 Like

(post deleted by author)

Thanks, I finally made it, I used millis and filtered logs in python.

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