/*งานที่ การเขียนโปรแกรมแสดงผลบนจอ LCD TFT ILI9341
นายศุภวิชญ์ ผิวเหลือง เลขที่ 6 ปวส.2/2
*/
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <DHT.h>
#include <SPI.h>
#include <math.h>
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
#define DHTPIN 13
#define DHTTYPE DHT22
#define ARC_THICKNESS 8
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
DHT dht(DHTPIN, DHTTYPE);
int centerX = 120;
int centerY = 170;
int radius = 90;
void drawGaugeFrame() {
for (int a = -150; a <= -30; a++) {
float rad = a * DEG_TO_RAD;
for (int w = 0; w < ARC_THICKNESS; w++) {
int x = centerX + cos(rad) * (radius - w);
int y = centerY + sin(rad) * (radius - w);
tft.drawPixel(x, y, ILI9341_LIGHTGREY);
}
}
}
void drawGaugeValue(int value) {
for (int i = 0; i <= value; i++) {
float angle = map(i, 0, 50, -150, -30) * DEG_TO_RAD;
for (int w = 0; w < ARC_THICKNESS; w++) {
int x = centerX + cos(angle) * (radius - w);
int y = centerY + sin(angle) * (radius - w);
tft.drawPixel(x, y, ILI9341_ORANGE);
}
}
}
void setup() {
tft.begin();
tft.setRotation(0);
dht.begin();
tft.fillScreen(ILI9341_WHITE);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(40, 20);
tft.print("Temperature");
drawGaugeFrame();
}
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
if (!isnan(temp) && !isnan(hum)) {
temp = constrain(temp, 0, 50);
tft.fillRect(20, 60, 200, 220, ILI9341_WHITE);
drawGaugeFrame();
drawGaugeValue((int)temp);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(6);
tft.setCursor(80, 130);
tft.print((int)temp);
tft.print((char)247);
tft.setTextSize(2);
tft.setCursor(40, 270);
tft.print(temp, 1);
tft.print(" C");
tft.setCursor(140, 270);
tft.print(hum, 0);
tft.print(" %");
}
delay(2000);
}