Touch Response is Very Slow

Hi All,

I’m trying to create one dashboard with data from sensors , but my touche screen works fine when I don´t have to read data from sensors , and when I put the call to the temperature function , my touch start to be very slow response , I tryed to do the same with others sensors and i had the same problem.

Can you help me to identify what´s the problem on the code below ?
#include <Adafruit_GFX.h> // biblioteca de graficos
#include <Adafruit_TFTLCD.h> // biblioteca de LCD
#include <TouchScreen.h>
#include <max6675.h>

#define YP A2
#define XM A3
#define YM 8
#define XP 9
#define MAXPRESSURE 1000
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);
short TS_MINX = 150;
short TS_MINY = 120;
short TS_MAXX = 850;
short TS_MAXY = 891;
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
int X;
int Y;
int Z;

// ThermoCouple
int thermo_gnd_pin = 53;
int thermo_vcc_pin = 51;
int thermo_so_pin = 45;
int thermo_cs_pin = 47;
int thermo_sck_pin = 49;
int led_pin = 24;

MAX6675 thermocouple(thermo_sck_pin, thermo_cs_pin, thermo_so_pin);
int led =0;
void setup(void)
pinMode(thermo_vcc_pin, OUTPUT);
pinMode(thermo_gnd_pin, OUTPUT);
pinMode(led_pin, OUTPUT);
digitalWrite(thermo_vcc_pin, HIGH);
digitalWrite(thermo_gnd_pin, LOW);
digitalWrite(led_pin, LOW);

void loop()
if((X > 194 && X < 315) && (Y > 0 && Y < 240) && (Z > MINPRESSURE && Z < MAXPRESSURE)){
if (led==0){
digitalWrite(led_pin, HIGH);
digitalWrite(led_pin, LOW);

void temperature(){
tft.setTextColor(CYAN, BLACK);
tft.setCursor(55, 50);
tft.print(“Temp: “);
tft.print(” C”);
void TouchPanel()
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint(); // Realizamos lectura de las coordenadas
digitalWrite(13, LOW);
pinMode(XM, OUTPUT); // La librería utiliza estos pines como entrada y salida
pinMode(YP, OUTPUT); // por lo que es necesario declararlos como salida justo
X = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
Y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
Z = p.z;

The first problem is that you need to go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

If you do not do this, the code you post could well be garbled and is certainly anything but easy to read.

Note: Also mark up any data in the same way. This includes error output that you get from the IDE.

And - before you post code, use "Auto Format" in the Tools menu to properly present the code.

Until you reply and demonstrate that you understand these points that have been made so far, there is absolutely no point working through other problems with the code, so I will leave it at that for the present.

There is nothing wrong with the code but a slight change of approach will help.

The problem is that it takes time to read the sensor and update the display. The sensor library bit bashes the serial interface with a 1 ms delay per bit, so that takes 20ms. The display is not attached to the hardware SPI pins, so that is also being bit bashed and will take a significant time to update since large digits are being drawn.

As a starter I suggest you update the display less frequently, assuming the temperature does not change very fast. Maybe every 10s, then in the time in between screen updates the touch screen will be more responsive.

To speed up the display you could connect it to the hardware SPI pins.