สอบถามแก้โค้ดค่าฝุ่น PM2.5 ไม่แสดงผลที่หน้าจอ TFT LCD หน่อยครับ

พอดีผมพึ่งเริ่มศึกษาการเขียนโค้ดน่ะครับ ผมทำเครื่องวัดปริมาณฝุ่น ใช้เป็น PMS7003 จะทำให้สีของหน้าจอปรับตามปริมาณฝุ่น PM2.5 ที่วัดได้ แต่ค่าฝุ่น PM2.5 ไม่แสดงขึ้นที่หน้าจอ tft st7735 ขนาด 1.8" เลยอยากสอบถามว่าต้องแก้ไขปัญหายังไงครับ ขอบคุณครับ

#include "PMS.h"
#include <Wire.h>
#include <SoftwareSerial.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 5  
#define DHTTYPE  DHT11
#define TFT_CS     10
#define TFT_RST    9 
#define TFT_DC     8
#define ROTATION_OPTION  1  // 0=แนวตั้ง, 1=แนวนอน, 2=แนวตั้งกลับหัว, 3=แนวนอนกลับหัว

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

DHT_Unified dht(DHTPIN, DHTTYPE);

SoftwareSerial mySerial(6, 7); // TX, RX

PMS pms(Serial);
PMS::DATA data;

void setup() {
  
  Serial.begin(38400);
  Serial.begin(9600);
  tft.initR(INITR_BLACKTAB);
  tft.setRotation(ROTATION_OPTION);

  dht.begin();
  Serial.println(F("DHTxx Unified Sensor Example"));
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  dht.humidity().getSensor(&sensor);

    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(41, 5);
    tft.setTextSize(1);
    tft.println("PM2.5 DETECTOR");
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(10, 20);
    tft.setTextSize(1);
    tft.println("PM2.5");

    sensors_event_t event;
    dht.temperature().getEvent(&event);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(10, 75);
    tft.setTextSize(1);
    tft.println("Temperature");
    tft.setCursor(36, 88);
    tft.println(event.temperature);
    tft.setCursor(70, 88);
    tft.println("C");

    dht.humidity().getEvent(&event);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(92, 75);
    tft.setTextSize(1);
    tft.println("Humidity");
    tft.setCursor(100, 88);
    tft.println(event.relative_humidity);
    tft.setCursor(134, 88);
    tft.println("%");

    tft.drawFastHLine(10, 70, 140, ST7735_WHITE);
    tft.drawFastVLine(80, 71, 29, ST7735_WHITE);

    tft.drawRect(9, 103, 103, 7, ST7735_WHITE);
    tft.fillRoundRect(10, 104, 20, 5, 1, ST7735_BLUE);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(16, 113);
    tft.setTextSize(1);
    tft.println("25");
    tft.fillRoundRect(31, 104, 20, 5, 0, ST7735_GREEN);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(36, 113);
    tft.setTextSize(1);
    tft.println("37");
    tft.fillRoundRect(51, 104, 20, 5, 0, ST7735_YELLOW);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(56, 113);
    tft.setTextSize(1);
    tft.println("50");
    tft.fillRoundRect(71, 104, 20, 5, 0, ST7735_ORANGE);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(76, 113);
    tft.setTextSize(1);
    tft.println("90");
    tft.fillRoundRect(91, 104, 20, 5, 0, ST7735_RED);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(96, 113);
    tft.setTextSize(1);
    tft.println(">91");
  }

void loop() {

  if (pms.read(data))
   {
    Serial.println("PM 2.5");
    Serial.println(data.PM_AE_UG_2_5);
    Serial.println();

    if (data.PM_AE_UG_2_5<=25){
    tft.fillScreen(ST7735_BLUE);
    tft.setTextSize(4);
    tft.setTextColor(ST7735_WHITE);   
    tft.setCursor(44, 34);
    tft.print(data.PM_AE_UG_2_5);
    tft.setTextSize(1);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(126, 58);
    tft.print("ug/m3");
   }
          
    if (data.PM_AE_UG_2_5>=26 & data.PM_AE_UG_2_5<=37){
    tft.fillScreen(ST7735_GREEN);
    tft.setTextSize(4);
    tft.setTextColor(ST7735_WHITE);   
    tft.setCursor(44, 34);
    tft.print(data.PM_AE_UG_2_5);
    tft.setTextSize(1);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(126, 58);
    tft.print("ug/m3");
    }
      
    if (data.PM_AE_UG_2_5>=38 & data.PM_AE_UG_2_5<=50){
    tft.fillScreen(ST7735_YELLOW);
    tft.setTextSize(4);
    tft.setTextColor(ST7735_WHITE);   
    tft.setCursor(44, 34);
    tft.print(data.PM_AE_UG_2_5);
    tft.setTextSize(1);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(126, 58);
    tft.print("ug/m3");
    } 

    if (data.PM_AE_UG_2_5>=51 & data.PM_AE_UG_2_5<=90){
    tft.fillScreen(ST7735_ORANGE);
    tft.setTextSize(4);
    tft.setTextColor(ST7735_WHITE);   
    tft.setCursor(44, 34);
    tft.print(data.PM_AE_UG_2_5);
    tft.setTextSize(1);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(126, 58);
    tft.print("ug/m3");
    }     

    if (data.PM_AE_UG_2_5<=91){
    tft.fillScreen(ST7735_RED);
    tft.setTextSize(4);
    tft.setTextColor(ST7735_WHITE);   
    tft.setCursor(44, 34);
    tft.print(data.PM_AE_UG_2_5);
    tft.setTextSize(1);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(126, 58);
    tft.print("ug/m3");
    }

    delay(2000);
   }

}

ค่าบน serial อ่านได้ปกติไหม

1 Likes

เป็นไงบ้างครับ กระทู้นี้อัพเดทหน่อย

GIF by Snap-on Tools