I keep getting issues in my DHT11 Temp sensor setup. My aim is to build a touchscreen that would give us the the amount of humidity in the area using a DHT11 Temp sensor.
Here is my code:
#if 1
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
// Sample const int XP = 6, XM = A2, YP = A1, YM = 7; //ID=0x9341
// Sample const int TS_LEFT = 907, TS_RT = 136, TS_TOP = 942, TS_BOT = 139;
//Data from TouchScreen_Calibr_native
const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x9595
const int TS_LEFT=891,TS_RT=108,TS_TOP=101,TS_BOT=912;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button temp_btn, humidity_btn, moisture_btn;
int pixel_x, pixel_y; //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
}
return pressed;
}
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
//https://create.arduino.cc/projecthub/techno_z/dht11-temperature-humidity-sensor-98b03b
//Sensor
#include <DHT.h>
#include <DHT_U.h>
#define dht_apin A5 // Analog Pin sensor is connected to
DHT;
void setup(void)
{
//Sensor
Serial.begin(9600);
delay(500);//Delay to let system boot
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);//Wait before accessing Sensor
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
Serial.println("Calibrate for your Touch Panel");
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(0); //PORTRAIT
tft.fillScreen(BLACK);
temp_btn.initButton(&tft, 60, 200, 100, 40, WHITE, CYAN, BLACK, "TEMP", 2);
humidity_btn.initButton(&tft, 180, 200, 100, 40, WHITE, CYAN, BLACK, "HUMIDITY", 2);
// moisture_btn.initButton(&tft, 100, 240, 140, 40, WHITE, CYAN, BLACK, "MOISTURE", 2);
moisture_btn.initButton(&tft, 120, 260, 140, 40, WHITE, CYAN, BLACK, "MOISTURE", 2);
temp_btn.drawButton(false);
humidity_btn.drawButton(false);
moisture_btn.drawButton(false);
tft.fillRect(40, 40, 160, 80, WHITE);
tft.setCursor(75, 50);
tft.setTextColor(GREEN);
tft.setTextSize(3);
tft.println("Home");
tft.setCursor(60, 80);
tft.print("Screen");
}
/* two buttons are quite simple
*/
void loop(void)
{
//Sensor
dht.read11( dht_apin );
Serial.print("Current humidity = ");
Serial.print(dht.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht.temperature);
Serial.println("C ");
delay(1000);//Wait 1 second before accessing sensor again.
bool down = Touch_getXY();
temp_btn.press(down && temp_btn.contains(pixel_x, pixel_y));
humidity_btn.press(down && humidity_btn.contains(pixel_x, pixel_y));
moisture_btn.press(down && moisture_btn.contains(pixel_x, pixel_y));
if (temp_btn.justReleased())
temp_btn.drawButton();
if (humidity_btn.justReleased())
humidity_btn.drawButton();
if (moisture_btn.justReleased())
moisture_btn.drawButton();
if (temp_btn.justPressed()) {
temp_btn.drawButton(true);
tft.fillRect(40, 40, 160, 80, RED);
tft.setCursor(55, 65);
tft.setTextColor(BLACK);
tft.setTextSize(4);
tft.print(dht.temperature);
}
if (humidity_btn.justPressed()) {
humidity_btn.drawButton(true);
tft.fillRect(40, 40, 160, 80, GREEN);
tft.setCursor(55, 65);
tft.setTextColor(BLACK);
tft.setTextSize(4);
tft.print(dht.humidity);
}
if (moisture_btn.justPressed()) {
moisture_btn.drawButton(true);
tft.fillRect(40, 40, 160, 80, BLUE);
}
}
#endif