TFT screen-- Improver coder question

Hi Guys, I have been playing around with my AdaFruit 2.8" tft. I have it reading a Temperature sensor and displaying the result I have on the screen that reads “Turn on LED” when this is pressed it turns on an LED and the Text in the box changes to “Turn off LED” when this is pressed it turns off the LED.

My problem is I want the Temperature to update every 5 seconds and the screen to refresh with new temp but but still keep the functionality of the touchscreen button and state of LED.

This is the code I am currently using.

Can someone suggest (in Pseudo code) how i might achieve my goal.

#include <Adafruit_GFX.h>    //
#include <Adafruit_TFTLCD.h> //These need to be included(library files)
#include <TouchScreen.h>     //
Adafruit_TFTLCD tft;         //This declaration can only be used for the shield

#define YP A1
#define XM A2
#define YM 7
#define XP 6
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);


#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940



#define BLACK   0x0000      // 
#define BLUE    0x0011      //  
#define RED     0xF800      //   
#define ORANGE  0xFC00      //    These assign human readable names to 16-bit color values
#define GREEN   0x07E0      //   
#define YELLOW  0xFFE0      //  
#define WHITE   0xFFFF      // 

int tempprobe1 = 5;
int LED = 2;
//int value = LOW;              // Value to store read value

void setup (void)  {           //The setup function is called when a sketch starts 
  Serial.begin(9600);        //This turns on serial monitor
  tft.reset();               //This resets the the RAM
  digitalWrite(10,HIGH);     //Backlight
  pinMode(LED, OUTPUT);      //Sets LED pin 2 as an OUTPUT

    tft.begin(0x7575);        //This Identifies what lcd driver chip you are using

  {

    tft.setRotation (3);                 //1=landscape mode with USB on right
  }

}




#define MINPRESSURE 10
#define MAXPRESSURE 1000



//--------------------------- End of setup -------------------------------------//


//-------------------------- Start of loop --------------------------------------//


void loop(void){

  //digitalWrite(13,HIGH);
  Point p = ts.getPoint();
  //digitalWrite(13,LOW);

  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);


  // turn from 0->1023 to tft.width
  p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);//default is (240, 0) [default puts touch cord. 0=x/y upper right.
  p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);//default is (320, 0) [I change these cause i like 0=xy bottom left.

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {

  int reading = analogRead(tempprobe1);  //getting the voltage reading from the temperature sensor

  float voltage = reading * (4500 / 1024);   //5 volts divided into 1024 individual bits=4.8828 mV

  // now print out the temperature
  float temperatureC = ((voltage ) /10)+5 ;  //converting from 10 mv per degree wit 500 mV offset

  if (temperatureC <=23)
  {
    tft.fillScreen(BLUE);
  }

  else if (temperatureC >23 && temperatureC <30)
  {
    tft.fillScreen(ORANGE);
  }

  else if (temperatureC >=30)
  {
    tft.fillScreen(RED);
  }
  
  


  Serial.print(temperatureC); 
  Serial.println(" Degress C");
  tft.setTextSize(3);
  tft.setTextColor(WHITE);
  tft.setCursor(10,100);
  tft.print(temperatureC);
  tft.print(" Degrees C");
  

  //if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {


    if (digitalRead(LED) == LOW){
      if (p.y > 10 && p.y < 100 && p.x > 10 && p.x < 100){  //looks for touch within x/y box area of button 1
        digitalWrite(LED, HIGH);
        tft.fillRect(25,170,150,75,GREEN);
        tft.setTextSize(2);
        tft.setTextColor(RED);
        tft.setCursor(30,200);
        tft.println("Turn LED OFF");


      }
    }



    else if (digitalRead(LED) == HIGH){
      if (p.y > 10 && p.y < 100 && p.x > 10 && p.x < 100){  //looks for touch within x/y box area of button 
        digitalWrite(LED, LOW);
        tft.fillRect(25,170,150,75,GREEN);
        tft.setTextSize(2);
        tft.setTextColor(RED);
        tft.setCursor(30,200);
        tft.println("Turn LED ON");

      }
    }

  }
}







//---------------------------- End of loop----------------------------------------

Look up the millis() function.

Then you want something like:

lastUpdate = 0;

loop() { if (millis() - lastUpdate > 5000) { lastUpdate = millis(); // store the update time first to make sure we update accurately at 5 seconds UpdateTemperatures(); }

// do LED stuff here }