1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | #include <TFT_eSPI.h> #include <Button2.h> #include <DHT.h> // AM2108 溫濕度感測器設定 (與 DHT22 相容) #define DHT_PIN 33 #define DHT_TYPE DHT22 DHT dht(DHT_PIN, DHT_TYPE); // 螢幕設定 TFT_eSPI tft = TFT_eSPI(); // 按鍵設定 #define BUTTON_A_PIN 0 #define BUTTON_B_PIN 35 Button2 buttonA = Button2(BUTTON_A_PIN); Button2 buttonB = Button2(BUTTON_B_PIN); // RGB LED 設定 (使用你的腳位定義) #define RED_PIN 15 #define GREEN_PIN 2 #define BLUE_PIN 13 #define PWM_FREQ 5000 #define PWM_RES 8 #define RED_CH 0 #define GREEN_CH 1 #define BLUE_CH 2 // 變數 float temperature = 0.0; float humidity = 0.0; bool showCelsius = true; // true=攝氏, false=華氏 unsigned long lastUpdate = 0; const unsigned long updateInterval = 3000; // 3秒更新一次 bool sensorError = false; void setup() { Serial.begin(115200); Serial.println("=== AM2108 電子溫濕度計啟動 ==="); // 初始化 AM2108 感測器 dht.begin(); delay(2000); // AM2108 需要啟動時間 // 初始化螢幕 tft.begin(); tft.setRotation(1); tft.fillScreen(TFT_BLACK); // 初始化 RGB LED PWM (使用你的設定) setupRGBLED(); // 設定按鍵事件 buttonA.setPressedHandler(buttonAPressed); buttonB.setPressedHandler(buttonBPressed); // 開機顯示 showStartupScreen(); delay(3000); // 測試感測器 testSensor(); Serial.println("電子溫濕度計啟動完成!"); } void loop() { // 處理按鍵 buttonA.loop(); buttonB.loop(); // 定時更新溫濕度 if (millis() - lastUpdate >= updateInterval) { updateSensorData(); updateDisplay(); updateLEDColor(); lastUpdate = millis(); } delay(100); } void setupRGBLED() { // 初始化 PWM 通道 (使用你的設定) ledcSetup(RED_CH, PWM_FREQ, PWM_RES); ledcSetup(GREEN_CH, PWM_FREQ, PWM_RES); ledcSetup(BLUE_CH, PWM_FREQ, PWM_RES); // 綁定 GPIO 到 PWM 通道 ledcAttachPin(RED_PIN, RED_CH); ledcAttachPin(GREEN_PIN, GREEN_CH); ledcAttachPin(BLUE_PIN, BLUE_CH); setColor(0, 0, 255); // 開機藍色 Serial.println("RGB LED 初始化完成"); } // 設定顏色亮度(使用你的函式) void setColor(uint8_t r, uint8_t g, uint8_t b) { // 對共陰 RGB LED,需要將亮度反轉(0 為全亮,255 為關閉) ledcWrite(RED_CH, 255 - r); ledcWrite(GREEN_CH, 255 - g); ledcWrite(BLUE_CH, 255 - b); } void showStartupScreen() { tft.fillScreen(TFT_BLACK); tft.setTextColor(TFT_CYAN); tft.setTextSize(2); tft.setCursor(40, 20); tft.println("AM2108"); tft.setTextColor(TFT_YELLOW); tft.setTextSize(2); tft.setCursor(20, 50); tft.println("溫濕度計"); tft.setTextColor(TFT_GREEN); tft.setTextSize(1); tft.setCursor(60, 80); tft.println("Temperature &"); tft.setCursor(65, 95); tft.println("Humidity Monitor"); tft.setTextColor(TFT_WHITE); tft.setCursor(85, 115); tft.println("啟動中..."); // 啟動LED動畫 for(int i = 0; i < 3; i++) { setColor(255, 0, 0); // 紅 delay(300); setColor(0, 255, 0); // 綠 delay(300); setColor(0, 0, 255); // 藍 delay(300); } } void testSensor() { Serial.println("測試 AM2108 感測器連接..."); float testTemp = dht.readTemperature(); float testHum = dht.readHumidity(); if (isnan(testTemp) || isnan(testHum)) { Serial.println("❌ AM2108 感測器連接失敗!"); sensorError = true; tft.fillScreen(TFT_BLACK); tft.setTextColor(TFT_RED); tft.setTextSize(2); tft.setCursor(20, 40); tft.println("感測器錯誤!"); tft.setTextColor(TFT_YELLOW); tft.setTextSize(1); tft.setCursor(10, 70); tft.println("請檢查:"); tft.setCursor(10, 85); tft.println("1.電源連接 (3.3V)"); tft.setCursor(10, 100); tft.println("2.數據線連接 (GPIO33)"); tft.setCursor(10, 115); tft.println("3.GND 連接"); // 錯誤閃爍 setColor(255, 0, 0); delay(500); setColor(0, 0, 0); delay(2000); } else { Serial.printf("✅ AM2108 感測器正常!初始溫度: %.1f°C, 濕度: %.1f%%\n", testTemp, testHum); sensorError = false; setColor(0, 255, 0); // 成功綠色 delay(1000); } } void updateSensorData() { if (sensorError) { // 重新嘗試連接 testSensor(); return; } // 讀取 AM2108 溫濕度 humidity = dht.readHumidity(); temperature = dht.readTemperature(); // 檢查讀取是否成功 if (isnan(humidity) || isnan(temperature)) { Serial.println("AM2108 讀取失敗!"); sensorError = true; return; } // 序列埠輸出 Serial.printf("AM2108 - 溫度: %.1f°C, 濕度: %.1f%%, 時間: %lu秒\n", temperature, humidity, millis()/1000); } void updateDisplay() { if (sensorError) { return; // 感測器錯誤時不更新顯示 } tft.fillScreen(TFT_BLACK); // 標題 tft.setTextColor(TFT_CYAN); tft.setTextSize(1); tft.setCursor(80, 5); tft.println("AM2108 溫濕度計"); // 溫度顯示區域 tft.setTextColor(TFT_RED); tft.setTextSize(1); tft.setCursor(20, 25); tft.println("溫度:"); tft.setTextSize(3); tft.setCursor(20, 40); if (showCelsius) { tft.printf("%.1f", temperature); tft.setTextSize(2); tft.print("°C"); } else { float fahrenheit = temperature * 9.0 / 5.0 + 32.0; tft.printf("%.1f", fahrenheit); tft.setTextSize(2); tft.print("°F"); } // 濕度顯示區域 tft.setTextColor(TFT_BLUE); tft.setTextSize(1); tft.setCursor(20, 75); tft.println("濕度:"); tft.setTextSize(3); tft.setCursor(20, 90); tft.printf("%.1f", humidity); tft.setTextSize(2); tft.print("%"); // 舒適度指示 showComfortLevel(); // 操作說明 tft.setTextColor(TFT_GREEN); tft.setTextSize(1); tft.setCursor(5, 115); tft.printf("A:°C/°F B:更新 %lu秒", millis()/1000); } void showComfortLevel() { String comfort = ""; uint16_t color = TFT_WHITE; // 舒適度判斷 if (temperature >= 20 && temperature <= 26 && humidity >= 40 && humidity <= 60) { comfort = "舒適"; color = TFT_GREEN; } else if (temperature < 18) { comfort = "偏冷"; color = TFT_CYAN; } else if (temperature > 28) { comfort = "偏熱"; color = TFT_RED; } else if (humidity < 30) { comfort = "乾燥"; color = TFT_ORANGE; } else if (humidity > 70) { comfort = "潮濕"; color = TFT_BLUE; } else { comfort = "一般"; color = TFT_YELLOW; } tft.setTextColor(color); tft.setTextSize(2); tft.setCursor(140, 50); tft.println(comfort); } void updateLEDColor() { if (sensorError) { // 錯誤時紅色閃爍 setColor(255, 0, 0); delay(100); setColor(0, 0, 0); return; } // 根據溫度改變LED顏色 (使用你的 setColor 函式) if (temperature < 18) { setColor(0, 0, 255); // 冷 - 藍色 } else if (temperature < 22) { setColor(0, 255, 255); // 涼爽 - 青色 } else if (temperature < 26) { setColor(0, 255, 0); // 舒適 - 綠色 } else if (temperature < 30) { setColor(255, 255, 0); // 溫暖 - 黃色 } else { setColor(255, 0, 0); // 熱 - 紅色 } } void buttonAPressed(Button2& btn) { // 切換攝氏/華氏 showCelsius = !showCelsius; updateDisplay(); Serial.println(showCelsius ? "切換到攝氏顯示" : "切換到華氏顯示"); // 按鍵回饋 setColor(255, 255, 255); delay(100); updateLEDColor(); } void buttonBPressed(Button2& btn) { // 立即更新感測器數據 Serial.println("手動更新 AM2108 數據..."); updateSensorData(); updateDisplay(); updateLEDColor(); // 按鍵回饋 setColor(0, 255, 0); delay(200); updateLEDColor(); Serial.println("手動更新完成"); } |
Direct link: https://paste.plurk.com/show/jKnweI0ynC5Hvv3w4NPc