2023/12/28
// Pico Game 外接 DHT11 sensor 做溫濕度顯示測試
// 用的 library 為 Arduino_GFX_Library https://github.com/moononournation/Arduino_GFX
// 指針及溫度顯示參考以下範例做修正
// Ref : https://github.com/Philc333/ESP32_ILI9341_DHT11
// 2023/10/5 by Mason
#include "SPI.h"
#include <Arduino_GFX_Library.h>
#include <SimpleDHT.h>
int pinDHT11 = 5; // 宣告 DHT11 接的腳位
SimpleDHT11 dht11(pinDHT11);
#define TFT_DC 20
#define TFT_CS 17
#define TFT_RST 21
#define TFT_SCK 18
#define TFT_MOSI 19
#define TFT_MISO 16
//Define Chip Select Pins, and Rotation
#define ROTATION 1
uint32_t setUPTime = 0; // time for next update
int my_Ntemp = 0;
int my_Otemp = 0;
int oBlock_H = 50;
int mH = 0, mH2 = 0;
// 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
#define DARK_GREY 0x4A89
#define MEDIUM_GREY 0x8492
#define LIGHT_GREY 0xD6BA
#define LIGHT_GREEN 0x970E
int redNeedle = 220, greyNeedle = 220,checkNeedle = 220;//Values set to 220,which equal 0 on meter.
Arduino_DataBus *bus = new Arduino_RPiPicoSPI(TFT_DC, TFT_CS, TFT_SCK, TFT_MOSI, TFT_MISO, spi0);
Arduino_GFX *gfx = new Arduino_ILI9341(bus, TFT_RST);
void setup() {
Serial.begin(115200);
gfx->begin();
gfx->setRotation(ROTATION);
gfx->invertDisplay(true);
gfx->fillScreen(BLACK);
gfx->setTextSize(3);
gfx->setTextColor(MAGENTA);
gfx->setCursor(30, 14);
gfx->print("Pico Game test");
myHumiditySetup();
myTemperatureSetup();
setUPTime = millis(); // Next update time
Serial.print("Setup Time: ");Serial.println(setUPTime);
}
//*********************************Loop Function Code**********************************
unsigned long lastFrame = millis();
void loop() {
while((millis() - lastFrame) < 2000);
lastFrame = millis();
Serial.print("Loop lastFrame Time(millis()): "); Serial.println(lastFrame);
// read without samples.
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000);
return;
}
mH2 = mH;
mH = (int)humidity;
// fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
gfx->fillRect(146, 220, 24, 16,WHITE);
gfx->setTextSize(2);
gfx->setTextColor(BLUE);
gfx->setCursor(146, 220);
gfx->print(mH);
temp_Update((int)temperature);
myMeter(mH + 220);
}
//*****************************Temperature Setup Function Code***************************
void myTemperatureSetup(){
//**********Title Bar with Temperature Code********
gfx->setTextSize(2);
gfx->setTextColor(WHITE);
gfx->setCursor(5, 74);
gfx->print("Temperature ");
//*********Temperature Gauge Code*********
gfx->fillRoundRect(gfx->width()-135+50, gfx->height()-170, 30, 150,20, BLACK);
gfx->fillRoundRect(gfx->width()-132+50, gfx->height()-167, 24, 150,20, WHITE);
gfx->fillCircle(gfx->width()-120+50, gfx->height()-30, 20, BLACK);
gfx->fillCircle(gfx->width()-120+50, gfx->height()-30, 17, WHITE);
gfx->fillRect(gfx->width()-132+50, gfx->height()-50, 24, 30,WHITE);
gfx->fillCircle(gfx->width()-120+50, gfx->height()-30, 12, RED);
gfx->fillRect(gfx->width()-123+50, gfx->height()-50, 6, 20,RED);
//*********Temperature Gauge Lines Code**********
int Line_Height = 270-80;
for(int a = 0; a < 5; a++ ){
gfx->drawFastHLine(gfx->width()-55,Line_Height,10,BLACK);
Line_Height = Line_Height - 25;
}
int sLine_Height = 270-80;
for(int b = 0; b < 20; b++ ){
gfx->drawFastHLine(gfx->width()-55,sLine_Height,5,BLACK);
sLine_Height = sLine_Height - 5;
}
//**********Temperature Gauge Numbers Code**********
gfx->setTextSize(1);
gfx->setTextColor(BLACK);
int number_Height = 267-80; //was 147
int gNum = 0;
for(int c = 0; c < 5; c++ ){
gfx->setCursor(gfx->width()-42, number_Height); //was 225
gfx->print(gNum);
gfx->print((char)223);
gfx->print("C");
number_Height = number_Height - 25;
gNum = gNum + 25;
}
}
//***********************Dial Gauge(Humidity) Setup Function Code************************
void myHumiditySetup(){
gfx->fillRect(200, 60, 320, 240, YELLOW);
gfx->fillRect(0, 60, 200, 240, DARK_GREY);
gfx->fillRect(0, 103, 200, 240, WHITE);
String hum = "H %";
gfx->setTextSize(2);
gfx->setTextColor(BLACK);
gfx->setCursor(200-76, 90+130);
gfx->print(hum);
// https://github.com/moononournation/Arduino_GFX/blob/master/src/Arduino_GFX.h
// drawArc(int16_t x, int16_t y, int16_t r1, int16_t r2, float start, float end, uint16_t color);
// fillArc(int16_t x, int16_t y, int16_t r1, int16_t r2, float start, float end, uint16_t color);
gfx->drawArc(90, 220, 95, 79, 220, 320, BLACK);
gfx->fillArc(90, 220, 95, 80, 295, 319, YELLOW);
gfx->fillArc(90, 220, 95, 80, 270, 295, GREEN);
gfx->fillArc(90, 220, 95, 80, 220, 270, WHITE);
gfx->setTextSize(1);
gfx->setTextColor(BLACK);
gfx->setCursor(7, 150);
gfx->print("0");
gfx->setCursor(164, 150);
gfx->print("100");
gfx->setCursor(42, 125);
gfx->print("25");
gfx->setCursor(125, 125);
gfx->print("75");
gfx->setCursor(85, 115);
gfx->print("50");
for (int a = 220; a < 320; a = a + 25){
gfx->fillArc(90, 220, 95, 80, a, a, BLACK);
}
for (int b = 225; b <= 315; b = b + 5){
gfx->fillArc(90, 220, 90, 80, b, b, BLACK);
}
}
//***************************Dial Gauge Pointer Update Code***************************
void myMeter(int hNeedle){
//int redNeedle = 220, greyNeedle = 220,checkNeedle = 220, hNeedle = 220;
checkNeedle = redNeedle;
if(hNeedle > redNeedle){
redNeedle = hNeedle;
for(int uRN = checkNeedle; uRN <= redNeedle; uRN++){
gfx->fillArc(120-30, 240, 95, 20, uRN - 1, uRN - 1, WHITE);
gfx->fillArc(120-30, 240, 95, 20, uRN, uRN, RED);
gfx->fillRect(95-30, 222, 48, 4, DARK_GREY);
gfx->fillRect(99-30, 226, 42, 3, YELLOW);
delay(10);
}
}else if(hNeedle < redNeedle){
redNeedle = hNeedle;
for(int dRN = checkNeedle; dRN >= redNeedle; dRN--){
gfx->fillArc(120-30, 240, 95, 20, dRN + 1, dRN + 1, WHITE);
gfx->fillArc(120-30, 240, 95, 20, dRN, dRN, RED);
gfx->fillRect(95-30, 222, 48, 4, DARK_GREY);
gfx->fillRect(99-30, 226, 42, 3, YELLOW);
delay(10);
}
}else{};
}
// ***************************Temperature Update Function Start***********************
//int oBlock_H = 50;
void temp_Update(int my_Ntemp){
int nBlock_H;
if(my_Ntemp != my_Otemp){
// fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
gfx->fillRect(150, 130-56, 40, 16,DARK_GREY);
my_Otemp = my_Ntemp;
gfx->setTextSize(2);
gfx->setTextColor(GREEN);
gfx->setCursor(150, 130-56);
gfx->print(my_Ntemp);
gfx->print((char)223);
gfx->print("C");
nBlock_H = map(my_Ntemp, 0, 100, 50, 150);
Serial.print("nBlock: ");Serial.print(nBlock_H);Serial.print(" my_Ntemp: ");Serial.println(my_Ntemp);
gfx->fillRect(gfx->width()-123+50, gfx->height()- oBlock_H, 6, oBlock_H - 50,WHITE);
gfx->fillRect(gfx->width()-123+50, gfx->height()- nBlock_H, 6, nBlock_H - 50,RED);
oBlock_H = nBlock_H;
//To be commented out, or deleted
Serial.print("Height: ");
Serial.print(nBlock_H);
Serial.print(" Temperature: ");
Serial.print(my_Ntemp);
}
}