Skip to content

Instantly share code, notes, and snippets.

@3110
Last active March 9, 2021 17:09
Show Gist options
  • Save 3110/ff6fd11f2730e1d4711ed2baa154aebe to your computer and use it in GitHub Desktop.
Save 3110/ff6fd11f2730e1d4711ed2baa154aebe to your computer and use it in GitHub Desktop.
M5StickC + PIR Hatによる手洗い30秒のための非接触タイマー
#include <M5StickC.h>
#define PIR_PIN 36
#define PIR_DETECTION_DELAY_MSEC 2000
#define FILL_MSEC 500
#define TIMER_MSEC 30000
bool startTimer(unsigned long ms) {
for (int y = M5.Lcd.height() - 1; y >= 0; --y) {
M5.Lcd.drawLine(0, y, M5.Lcd.width() - 1, y, TFT_BLUE);
delay(FILL_MSEC / M5.Lcd.height());
}
for (int y = 0; y < M5.Lcd.height(); ++y) {
M5.Lcd.drawLine(0, y, M5.Lcd.width() - 1, y, TFT_BLACK);
delay(TIMER_MSEC / M5.Lcd.height());
M5.update();
if (M5.BtnA.wasPressed()) { // Cancel Timer
return false;
}
}
return true;
}
void setup() {
M5.begin();
M5.Lcd.setRotation(2);
M5.Lcd.fillScreen(TFT_BLACK);
pinMode(M5_LED, OUTPUT);
digitalWrite(M5_LED, HIGH);
pinMode(PIR_PIN, INPUT_PULLUP);
}
void loop() {
if (digitalRead(PIR_PIN) == HIGH) {
if (startTimer(TIMER_MSEC)) {
digitalWrite(M5_LED, LOW);
delay(1000);
digitalWrite(M5_LED, HIGH);
} else {
M5.Lcd.fillScreen(TFT_BLACK);
delay(PIR_DETECTION_DELAY_MSEC);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment