Skip to content

Instantly share code, notes, and snippets.

@nrtkbb
Created May 18, 2020 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nrtkbb/daaf3a9ba766aba6e750903403a79ec9 to your computer and use it in GitHub Desktop.
Save nrtkbb/daaf3a9ba766aba6e750903403a79ec9 to your computer and use it in GitHub Desktop.
#include <M5StickC.h>
#include <Wire.h>
#include "DHT12.h"
DHT12 dht12;
int PIN = 32; // GROVEの黄色い線にパルスを送る
int PWMCH = 0;
void setup() {
M5.begin();
// パルスを送るための初期設定
pinMode(PIN, OUTPUT);
ledcSetup(PWMCH, 12000, 8);
ledcAttachPin(PIN, PWMCH);
M5.Axp.ScreenBreath(10); // 画面の輝度を少し下げる
M5.Lcd.setRotation(3); // 電源ボタン側を上にする
M5.Lcd.fillScreen(BLACK); // 背景を黒にする
Wire.begin(0, 26); // I2Cを初期化する
}
float tmp_magic_number = 2.0; // 気温計の個体差吸収用
float fan_set_temp = 30.0; // この温度に達したらファンが動く
String fan_mode = ""; // ファンのモードを STOP/SLOW/HIGH と切り替える
String pre_fan_mode = ""; // 描画用に一つ前のファンモードを保存する変数
void loop() {
// ボタン情報更新に必要
M5.update();
// ファン設定温度の変更
if (M5.BtnA.wasReleased()) {
fan_set_temp = fan_set_temp - 1; // M5ボタンで1度マイナスする
}
if (M5.BtnB.wasReleased()) {
fan_set_temp = fan_set_temp + 1; // Bボタンで1度プラスする
}
// 気温の取得
float tmp = dht12.readTemperature() - tmp_magic_number;
// ファンの速度をコントロールする
if (fan_set_temp <= tmp) {
// full speed
ledcWrite(PWMCH, 255);
fan_mode = "HIGH";
} else if (fan_set_temp - 1 <= tmp) {
// half speed
ledcWrite(PWMCH, 150);
fan_mode = "SLOW";
} else {
ledcWrite(PWMCH, 0);
fan_mode = "STOP";
}
// モードチェンジの時に画面をリフレッシュする
if (fan_mode != pre_fan_mode) {
// 画面を真っ黒で塗りつぶす
M5.Lcd.fillScreen(TFT_BLACK);
pre_fan_mode = fan_mode;
}
// 画面描画
M5.Lcd.setCursor(0, 12, 4);
M5.Lcd.printf("Fan %2.0f ", fan_set_temp); // ファン用設定温度
M5.Lcd.printf("%s", fan_mode); // ファンのモード
M5.Lcd.setCursor(0, 50, 4);
M5.Lcd.printf("Temp %2.1f C", tmp); // 温度表示
// 0.5秒待機
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment