/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "DHT.h"
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
float temp =0;
float hum =0;
float pretemp =0;
float prehum =0;
float currtemp =50;
float currhum =100;
float i=0;
float j=0;
int lembap = 100;
void setup(void)
{
Serial.begin(115200);
dht.begin();
tft.begin();
tft.setRotation(1); //PORTRAIT
tft.fillScreen(BLACK);
tft.setTextColor(YELLOW);
tft.setTextSize(2);
tft.fillScreen(BLACK);
tft.setTextColor(WHITE);
tft.setCursor(0, 130);
tft.print("0");
tft.setTextColor(MAGENTA);
tft.setCursor(20, 70);
tft.print("Humidity");
tft.setTextColor(GREEN);
tft.setCursor(215, 180);
tft.print("0");
tft.setCursor(237, 180);
tft.print("50");
tft.setCursor(215, 100);
tft.print("TEMP");
tft.setCursor(30, 5);
tft.setCursor(0, 0);
tft.setTextColor(YELLOW);
tft.print("Temperature & Humidity");
tft.setCursor(120, 25);
tft.print("Monitor");
tft.setTextColor(WHITE,BLACK);
}
void loop(void)
{
tft.fillRect(0, 100, 130, 20,RED);
tft.setTextSize(2);
tft.setCursor(90, 130);
tft.print(lembap + String(" %"));
tft.setTextSize(3);
tft.setCursor(198, 130);
tft.print(temp);
i=map(pretemp,0,50,0,300);
j=map(currtemp,0,50,0,300);
for (i; i<=j; i=i+0.1)
{
float j=i-150 ;
float angle = (j / 57.2958) - 1.57;
float x1= 80 + cos(angle) * 55;
float y1 = 130+sin(angle) * 55;
float x2= 80 + cos(angle) * 75;
float y2 = 130+sin(angle) * 75;
tft.drawLine(x1+160, y1,x2+160,y2, GREEN);
}
for (i-2; i>=j; i=i-0.05)
{
float j=i-150 ;
float angle = (j / 57.2958) - 1.57;
float x1= 80 + cos(angle) * 57;
float y1 = 130+sin(angle) * 57;
float x2= 80 + cos(angle) * 73;
float y2 = 130+sin(angle) * 73;
tft.drawLine(x1+160, y1,x2+160,y2, BLACK);
}
pretemp=currtemp;
prehum=currhum;
hum = dht.readHumidity();
temp = dht.readTemperature();
if (isnan(hum) || isnan(temp))
{
Serial.println(F("Failed to read from DHT sensor!. Some random numbers displaying in Reading"));
temp =random(0,50);
hum =random(0,100);
return;
}
Serial.println("TEMP: "+String(temp));
Serial.println("Hum: "+String(hum)+String("\n"));
currtemp=temp;
currhum=hum;
lembap=currhum;
tft.fillRect(90, 130, 70, 20, BLACK);
delay(1000);
}