Hi,
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;
DHT dht(DHTPIN, DHTTYPE);
// 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 -------------------------------------------------------
Serial.begin(9600);
dht.begin();
tft.reset();
uint16_t identifier = tft.readID();
//tft.setRotation(0);
tft.begin(identifier);
tft.fillScreen(BLACK);
//DHT SIGNATURE --------------------------------------------------------
tft.fillRect(0,0,240,320,MAGENTA);
tft.setCursor(50,20);
tft.setTextSize(2);
tft.setTextColor(BLACK);
tft.println("DHT11");
tft.setCursor(60,45);
tft.print("ON 2560");
TSPoint p = ts.getPoint();
}
void loop()
{
TSPoint p = ts.getPoint();
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>0 && p.x<120)
{
if (p.y>240 && p.y<320)
{
tft.setCursor(0,180);
tft.setTextSize(2);
tft.setTextColor(RED);
tft.print("Success");
}
}
//Touch code---------------------------------------------------------------------------------------
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//DHT code------------------------------------------------------------------------------------------
// Wait a few seconds between measurements.
//delay(2000);
// 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!");
return;
}
//TEMP & HUMIDITY RECTANGLES---------------------------------------------------------------------------------
//TEMP RECTANGLE
//tft.fillRect(0,80,240,320,BLACK);
tft.drawRect(0,80,240,80,BLACK);
tft.fillRect(0,84, 240, 72, YELLOW);
tft.setCursor(4,110);
tft.setTextSize(2);
tft.setTextColor(BLACK);
tft.print("Temperature:");
tft.print(t);
tft.print(" C");
//Serial.println(t);
//HUMIDITY RECTANGLE
// tft.fillRect
tft.drawRect(0,160,240,80,BLACK);
tft.fillRect(0,164,240,72,CYAN);
tft.setCursor(40,190);
tft.setTextSize(2);
tft.setTextColor(BLACK);
tft.print("Humidity:");
tft.print(h);
//delay code------------------------------
delay(500);
}