忙しい人のための最低限の分割したバージョンの記事Part1
~ENV Ⅱ HATのサンプルコードの小改造~
下記のコードは、サンプルコードから地磁気を取得する部分を省き、また温度の後にCを付けて(℃はできないらしい?)、湿度を小数点以下一桁まで表示するようにしてその後ろに%を付けて、気圧をhPa表示にするという小改造を行ったものです。
それ以外はまったくいじっていません。
![]() |
| 下のコードで動いている様子 |
code:1 ENV II HAT KAI
/*
note: need add library Adafruit_BMP280 from library manage
Github: https://github.com/adafruit/Adafruit_BMP280_Library
*/
/*注意 私の理解できたところのみ説明が書かれています
説明がないということは…そういうことです
また、英語はガバガバ英語だったりDeepLに頼った英語だったりします。自己満です。 */
//Include Libraries ライブラリをインクルード
#include <M5StickC.h>
#include "SHT3X.h"
#include <Wire.h>
#include "Adafruit_Sensor.h"
#include <Adafruit_BMP280.h>
//Define Valiable 変数を定義
SHT3X sht3x;
Adafruit_BMP280 bme;
//Calculate temperature, humidity and air pressure in floating point? 浮動小数点で温湿度・気圧を計算?
float tmp = 0.0;
float hum = 0.0;
float pressure = 0.0;
void setup() {
// put your setup code here, to run once:
M5.begin(); //Initialize M5StickC M5StickCを初期化
Wire.begin(0,26); //Initialize I2C of ENVHAT2 ENVHAT2のI2Cを初期化
M5.Lcd.setRotation(3); //Set Lcd's Direction 画面を横向き(左上)に
M5.Lcd.fillScreen(BLACK); //Fill Screen Black 背景色を黒に
M5.Lcd.setCursor(0, 0, 2); //Set Cursor to (0,0) and Assign Fonts 2 カーソルを(0,0)へ、フォントは2を指定
M5.Lcd.println("ENV TEST 2"); //Show sentences in "" on the screen 画面に””内の文章を表示
if (!bme.begin(0x76)){
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1); //Error message if the BMP280 sensor was not detected? BMP280センサが検出されなかった場合のエラー表示?
}
}
uint8_t setup_flag = 1;
void loop() {
// put your main code here, to run repeatedly:
if(sht3x.get()==0){
tmp = sht3x.cTemp;
hum = sht3x.humidity; //Acquisition of temperature and humidity data from sht3x? sht3xからの温湿度データの取得?
}
M5.Lcd.setCursor(0, 20, 2); //Set Cursor to (0,20) and Assign Fonts 2 カーソルを(0,20)へ、フォントは2を指定
M5.Lcd.printf("Temp: %2.1fC Humi: %2.1f%%", tmp, hum); //Display of degrees Celsius and humidity in one decimal place 摂氏度および湿度を小数点以下1桁で表示
float pressure = bme.readPressure();
M5.Lcd.setCursor(0, 40, 2); //Set Cursor to (0,40) and Assign Fonts 2 カーソルを(0,40)へ、フォントは2を指定
M5.Lcd.printf("pressure: %4.3fhPa", pressure / 100); //Atmospheric pressure in units of hPa to three decimal places 大気圧をhPaの単位で小数点以下3桁まで表示
delay(100); //Wait 100msecs 100ミリ秒待機
if (!bme.begin(0x76)){
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1); //Error message if the BMP280 sensor was not detected? BMP280センサが検出されなかった場合のエラー表示?
}
}

0 件のコメント:
コメントを投稿