Unable to print rectangles in loop function


I am trying to hook up my DHT11 to a mcufriend 2.4" TFT LCD to an arduino mega 2560. As the readings are continuously updated to the display, I would require a clear and fill rectangle in a loop fashion. Strangely, this can't be achieved with declarations of DHT variables in the loop function. Here is my code.

#include <Adafruit_GFX.h>    // Core graphics library
#include <SWTFT.h> // Hardware-specific library
#include <TouchScreen.h>
#include "DHT.h"

#define DHTPIN 40
#define DHTTYPE DHT11

#define YP A2  // must be an analog pin, use "An" notation!
#define XM A1  // must be an analog pin, use "An" notation!
#define YM 6   // can be a digital pin
#define XP 7   // can be a digital pin

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

#define MINPRESSURE 10
#define MAXPRESSURE 1000

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

// Assign human-readable names to some common 16-bit color values:
#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

SWTFT tft;

#define BOXSIZE 40
#define PENRADIUS 3
//int oldcolor, currentcolor;

// TFT FUNCTIONS----------------------------------------------------------------------------------------------------
//tft.setCursor(22,37) , tft.setTextSize(2) ~ 25 pixels in height , tft.setTextColor(RED) 
  tft.drawRect(x_start, y_start, width(x direction), height(y direction, COLOR) 
  E.g. tft.drawRect(10,0,237,318,CYAN) , tft.fillRect 10,0,237,318,CYAN) */

 tft.drawCircle(x_start, y_start, radius, COLOR)
 E.g. tft.drawCircle(180,140,10,BLACK) , tft.fillCircle(180,140,10,BLACK) 

void setup() {                                      
  // put your setup code here, to run once: LCD Code -------------------------------------------------------
uint16_t identifier = tft.readID();

  //DHT SIGNATURE --------------------------------------------------------
  tft.print("ON 2560");
  TSPoint p = ts.getPoint();

void loop()
  TSPoint p = ts.getPoint();
    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>0 && p.x<120)   
      if (p.y>240 && p.y<320)

  //Touch code---------------------------------------------------------------------------------------
  digitalWrite(13, HIGH);
  digitalWrite(13, LOW);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);

  //DHT code------------------------------------------------------------------------------------------
   // Wait a few seconds between measurements.

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
   // Compute heat index in Fahrenheit (the default)
  //float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  //float hic = dht.computeHeatIndex(t, h, false);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
  //TEMP & HUMIDITY RECTANGLES---------------------------------------------------------------------------------
  tft.fillRect(0,84, 240, 72, YELLOW);
   tft.print(" C");

 // tft.fillRect

//delay code------------------------------

It is nothing to do with DHT library.

The TouchScreen shares pins with the TFT. So you must restore the pins to OUTPUT after every call to ts.getPoint()

You have a pointless call in setup(). I would remove this statement. The Optimiser might remove it anyway.
You have a sensible call in loop()

Just move the pinMode() calls to the appropriate place e.g.

void loop()
  TSPoint p = ts.getPoint();
  pinMode(XM, OUTPUT);      //always restore shared TFT pins immediately
  pinMode(YP, OUTPUT);

Note that the existing digitalWrite(13, ..) is also pointless. It will flash the LED for a few microseconds.
I doubt if you will notice. Especially with your Shield plugged in.


Thanks for your insights, David. It's working fine now! Karma to you.