Skip to content

Instantly share code, notes, and snippets.

@woodif
Created March 12, 2026 15:40
Show Gist options
  • Select an option

  • Save woodif/fec2f2ed762727366cf2c5cbf402a08b to your computer and use it in GitHub Desktop.

Select an option

Save woodif/fec2f2ed762727366cf2c5cbf402a08b to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include <math.h>
Adafruit_BME280 bme;
TFT_eSPI tft = TFT_eSPI();
const float MAX_TEMP_OFFSET = 3.5;
// กำหนดเวลาที่ใช้ในการไต่ระดับความร้อนจนคงที่ (30 นาที)
// 30 นาที * 60 วินาที * 1000 มิลลิวินาที = 1,800,000 ms
const unsigned long RAMP_UP_TIME = 1800000;
// ฟังก์ชันชดเชยค่า
void getCompensatedData(float rawTemp, float rawHum, float currentOffset, float &compTemp, float &compHum) {
compTemp = rawTemp - currentOffset;
float esRaw = 6.112 * exp((17.67 * rawTemp) / (rawTemp + 243.5));
float eActual = esRaw * (rawHum / 100.0);
float esComp = 6.112 * exp((17.67 * compTemp) / (compTemp + 243.5));
compHum = (eActual / esComp) * 100.0;
if (compHum > 100.0) compHum = 100.0;
if (compHum < 0.0) compHum = 0.0;
}
// ฟังก์ชันสำหรับวาดโครงสร้างหน้าจอ
void drawStaticUI() {
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setTextDatum(MC_DATUM);
tft.drawString("BME280 THERMAL COMP", 160, 20, 4);
tft.drawLine(10, 40, 310, 40, TFT_DARKGREY);
tft.drawLine(160, 45, 160, 190, TFT_DARKGREY);
tft.setTextColor(TFT_ORANGE, TFT_BLACK);
tft.drawString("RAW SENSOR", 80, 60, 2);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.drawString("COMPENSATED", 240, 60, 2);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextDatum(ML_DATUM);
tft.drawString("C", 120, 100, 4);
tft.drawString("%", 120, 150, 4);
tft.drawString("C", 280, 100, 4);
tft.drawString("%", 280, 150, 4);
tft.drawLine(10, 200, 310, 200, TFT_DARKGREY);
}
void setup() {
Serial.begin(115200);
Wire.begin(27,22);
tft.begin();
tft.setRotation(1);
drawStaticUI();
if (!bme.begin(0x76)) {
tft.fillScreen(TFT_RED);
tft.setTextColor(TFT_WHITE, TFT_RED);
tft.setTextDatum(MC_DATUM);
tft.drawString("BME280 ERROR!", 160, 120, 4);
while (1) delay(10);
}
}
void loop() {
float measuredTemp = bme.readTemperature();
float measuredHum = bme.readHumidity();
float actualTemp = 0.0;
float actualHum = 0.0;
float currentOffset = 0.0;
unsigned long currentMillis = millis();
// คำนวณ Offset ตามเวลาที่ผ่านไป (ชดเชยภายใน 30 นาที)
if (currentMillis < RAMP_UP_TIME) {
currentOffset = MAX_TEMP_OFFSET * ((float)currentMillis / RAMP_UP_TIME);
} else {
currentOffset = MAX_TEMP_OFFSET;
}
getCompensatedData(measuredTemp, measuredHum, currentOffset, actualTemp, actualHum);
// อัปเดตหน้าจอ TFT
tft.setTextDatum(MR_DATUM);
// แสดงค่า Raw
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawFloat(measuredTemp, 1, 110, 100, 4);
tft.drawFloat(measuredHum, 1, 110, 150, 4);
// แสดงค่า Compensated
tft.setTextColor(TFT_CYAN, TFT_BLACK);
tft.drawFloat(actualTemp, 1, 270, 100, 4);
tft.drawFloat(actualHum, 1, 270, 150, 4);
// คำนวณเวลาเพื่อแสดงผลเป็น นาที:วินาที
unsigned long totalSeconds = currentMillis / 1000;
unsigned int displayMins = totalSeconds / 60;
unsigned int displaySecs = totalSeconds % 60;
// อัปเดตแถบ Status ด้านล่าง
tft.setTextDatum(MC_DATUM);
tft.setTextColor(TFT_LIGHTGREY, TFT_BLACK);
char statusText[50];
// รูปแบบข้อความ "Time: 29:59 | Offset: -2.99 C"
sprintf(statusText, "Time: %02u:%02u | Offset: -%.2f C", displayMins, displaySecs, currentOffset);
tft.fillRect(10, 210, 300, 20, TFT_BLACK);
tft.drawString(statusText, 160, 220, 2);
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment