Newer
Older
D4han / D4han.ino
@22a5065 22a5065 on 22 Apr 2024 2 KB Update D4han.ino
/*
*******************************************************************************
* Copyright (c) 2023 by M5Stack
*                  Equipped with M5Core sample source code
*                          配套  M5Core 示例源代码
* Visit for more information: https://docs.m5stack.com/en/unit/tvoc
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/tvoc
*
* Describe: TVOC/eCO2.
* Date: 2021/8/26
*******************************************************************************
Description: The screen will display TVOC and CO2.  屏幕将显示TVOC和CO2。
Note: SGP30 needs 15 seconds to initialize calibration after power on.
*/

#include <M5StickCPlus.h>

#include "Adafruit_SGP30.h"

Adafruit_SGP30 sgp;
TFT_eSprite spu = TFT_eSprite(&M5.Lcd); // Sprite object

long last_millis = 0; // for 15sec wait timer

void setup() {
  M5.begin(115200);
  M5.Lcd.setRotation(3);
  
  spu.setColorDepth(8);
  spu.createSprite(240, 135);
  
  
  M5.Lcd.setTextSize(2);
  M5.Lcd.fillScreen(TFT_BLUE);
  M5.Lcd.setTextSize(2);
  M5.Lcd.setTextColor(TFT_BLACK);
  M5.Lcd.setCursor(5, 5);
  
  while (!sgp.begin()) {  // Init the sensor.
    M5.Lcd.println("Sensor not found");
    delay(5000);
  }
  //  M5.Lcd.println("Initialization...");
  
}

void loop() {
  static int i = 4; //initializing
  while (i > 0) {
    if (millis() - last_millis > 1000) {
      last_millis = millis();
      i--;
      spu.fillSprite(TFT_YELLOW);
      spu.setTextColor( BLACK, YELLOW );
      spu.setCursor(30, 4, 4);  spu.setTextSize(1);
      spu.println("Initializing...");
      
      spu.setCursor(60, 34, 4);  spu.setTextSize(2);
      spu.printf("  %d  ", i );
      spu.pushSprite(0, 0);
      delay(100);
    }
  }
  
  if (!sgp.IAQmeasure()) {  // Commands the sensor to take a single eCO2/VOC
    // measurement.  命令传感器进行一次eCO2/VOC测量
    Serial.println("Measurement failed");
    return;
  }
  
  spu.fillSprite(LIGHTGREY);
  spu.setCursor(93, 5, 4);  spu.setTextSize(2);
  spu.setTextColor( BLACK, LIGHTGREY);
  spu.printf(" %d \n", sgp.TVOC);
  
  spu.setCursor(93, 65, 4);  spu.setTextSize(2);
  spu.setTextColor( BLACK, LIGHTGREY);
  spu.printf(" %d \n", sgp.eCO2);
  
  spu.setTextColor( BLUE, LIGHTGREY);
  spu.setCursor(10, 20, 4);  spu.setTextSize(1);
  spu.println("TVOC : ");
  spu.setCursor(10, 80, 4);  spu.setTextSize(1);
  spu.println("eCO2 : ");
  
  spu.pushSprite(0, 0); // 画面に反映させる
  
  Serial.printf("TVOC: %d eCO2: %d\n", sgp.TVOC, sgp.eCO2);

  // eCO2が1000を超えたら音を鳴らす
if (sgp.eCO2 > 1000) {
   spu.setCursor(10, 120, 4);  spu.setTextSize(1);
  spu.println("kankisite ");
M5.Beep.setBeep(523, 500);  M5.Beep.beep();  
}

  M5.update();
  if (M5.Axp.GetBtnPress() == 2) {
    M5.Axp.Write1Byte(0x32, M5.Axp.Read8bit(0x32) | 0x80); // Power Off
  }
  delay(500);
}











void loop() {
}