M5StickC简易洗手提醒装置-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1230|回复: 0

M5StickC简易洗手提醒装置

[复制链接]
发表于 2020-4-13 11:19 | 显示全部楼层 |阅读模式
借助于ToF实现距离检测,当靠近水池时,进行20秒洗手消毒计时。默认采用32,33连接传感器,A键可手动操作计时。
[mw_shl_code=arduino,true]#include <M5StickC.h>
#include <VL53L0X.h>                  //Time of flight distance sensor
#include <Wire.h>                     //i2C comms needed for VL sensor

const char* BuildTime = __TIME__;
const char* BuildDate = __DATE__;
const char* Version = "V0.3";
#define SKETCH "Wash Hands"
#define HOSTNAME "M5StickC-1"
#define STARTUPMSGDELAY 1500           // ms to wait to display startup messages
#define SHORTVERSION "YES"             // output just the version no on screen at startup
#define STARTUPSCREENBRIGHTNESS 200    // startup brightness
boolean M5stack = true;               //Enable M5 output text
int StdScreenBrightness = 100;
int BackGroundColor = BLUE;
String BackGround = "RED";
char Build[30];

// Used for Button
const int  M5ButtonPin = 37;
uint8_t lastPinState = 1;
uint8_t pinState = 0;

/** START - Time Of Flight VL53L0X parameters **/
VL53L0X VL53L0Xsensor;
#define HIGH_ACCURACY
boolean TOFSensor = true;
int SensorDist = 50;
int SensorDist1 = 50;
int SensorDist2 = 50;
int SensorDist3 = 50;
int VL53L0XAddress = 0x29;
boolean Trigger = false;
String SensorStatus = "#TOF#";
int TriggerDist = 160;
/** END - Time Of Flight VL53L0X parameters **/

int SecsCountDownFrom = 20;
int MainLoopDelay = 200;

void SetupVL53L0X()
{
  // Check VL53L0X TOF and set for accurate scanning
  Wire.begin();
  Wire.beginTransmission (VL53L0XAddress);
  if (Wire.endTransmission() == 0)
  {
    Serial.print (F("VL53L0X device found at 0x"));
    Serial.println (VL53L0XAddress, HEX);
   
    // only set parameters if sensor was detected
    VL53L0Xsensor.init();
    VL53L0Xsensor.setTimeout(500);
    //VL53L0Xsensor.setMeasurementTimingBudget(200000);
  }
  else
  {
    Serial.println("######  VL53L0X device NOT found   ######");
    Serial.println("######     TEST MODE - BUTTON      ######");
    TOFSensor = false;
  }
}

void CheckTOF()
{
  if (TOFSensor == true and Trigger == false)       // only read if sensor was found at startup and no current trigger
  {
    SensorDist1 = VL53L0Xsensor.readRangeSingleMillimeters();
    delay(50);
    SensorDist2 = VL53L0Xsensor.readRangeSingleMillimeters();
    delay(50);
    SensorDist3 = VL53L0Xsensor.readRangeSingleMillimeters();
    SensorDist = (SensorDist1 + SensorDist2 + SensorDist3)/3;     // take an average of 3 settings to remove 'aborations'
   
    if (VL53L0Xsensor.timeoutOccurred())
    {
      Serial.println("VL53L0X TIMEOUT");
      SensorDist = 50;            // default sensor distance if it timeouts
    }
    else
    {
    Serial.println("Distance = " + String(SensorDist));
      if ( SensorDist <= TriggerDist )         // Door triggered
      {
        Trigger = true;
      }
      else
      {
        Trigger = false;
      }
    Serial.println("Trigger = " + String(Trigger));

    }
  }
  else
  {
    CheckButtons();
  }
}

void OutputBuildInfo()
{
  sprintf(Build, "%s %s", BuildTime, BuildDate );
  Serial.print(SKETCH);
  Serial.print(" ");
  Serial.println(Version);
  Serial.print("Build ");
  Serial.println(Build);
  Serial.print("ESP Name ");
  Serial.println(HOSTNAME);

  if (M5stack == true )
  {
    M5OutputBuildInfo();
  }

  
}

void M5OutputBuildInfo()
{
  //M5.Lcd.setBrightness(StdScreenBrightness);
  M5.Lcd.fillScreen(BackGroundColor);
  if (SHORTVERSION == "Yes" )
  {
    M5.Lcd.setCursor(0,0);
    M5.Lcd.setTextSize(2);
    M5.Lcd.setTextColor(WHITE, BackGroundColor);
    M5.Lcd.println(Version);
    delay(STARTUPMSGDELAY);
  }
  else
  {
    sprintf(Build, "%s %s", BuildTime, BuildDate );
    M5.Lcd.setCursor(0,0);
    M5.Lcd.setTextSize(2);
    M5.Lcd.setTextColor(WHITE, BackGroundColor);
    M5.Lcd.println(Version);
    M5.Lcd.println(SKETCH);
    M5.Lcd.println(BuildTime);
    M5.Lcd.println(BuildDate);
    M5.Lcd.println(HOSTNAME);
    delay(STARTUPMSGDELAY);
  }
}

void CheckButtons()
{
    pinState = digitalRead(M5ButtonPin);
    if(!pinState && lastPinState){
        Trigger = true;
    }
    lastPinState = pinState;

}

/*
* ###################################################
* #              START SETUP FUNCTION               #
* ###################################################
*/

void setup()
{
  Serial.begin(115200);
  M5.begin();
  M5.Lcd.setRotation(3);

  // Setup big M5 button
  pinMode(M5ButtonPin, INPUT_PULLUP);

  OutputBuildInfo();

  SetupVL53L0X();

  Wire.begin();

  delay(3000);
  M5.Lcd.fillScreen( BLACK );
}

/*
* ###################################################
* #                END SETUP FUNCTION               #
* ###################################################
*/

/*
* ###################################################
* #                 MAIN LOOP                       #
* ###################################################
*/
void loop()
{
  delay(MainLoopDelay);
  CheckTOF();

  if (Trigger == true)
  {
    //M5.Lcd.setBrightness(200);
    for (int Count = SecsCountDownFrom; Count > 0; Count--)
    {
      Serial.println(Count);
      BackGround = "RED";
      //M5.Lcd.setRotation(3);
      M5.Lcd.fillScreen( RED );
      M5.Lcd.setTextColor(YELLOW);  
      M5.Lcd.setTextSize(6);
      M5.Lcd.setCursor(60, 20);
      M5.Lcd.println(Count);

      delay(990);
    }
    Serial.println("Rinse Hands");

    M5.Lcd.fillScreen( YELLOW );
    M5.Lcd.setTextColor(BLACK);  
    M5.Lcd.setTextSize(4);
    M5.Lcd.setCursor(28, 4);
    M5.Lcd.print("RINSE");
    M5.Lcd.setCursor(28, 44);
    M5.Lcd.print("HANDS");
    delay(4000);
   
    Serial.println("Well Done");
      
    M5.Lcd.fillScreen( GREEN );
    M5.Lcd.setTextColor(BLACK);  
    M5.Lcd.setTextSize(4);
    M5.Lcd.setCursor(28, 4);
    M5.Lcd.print("WELL");
    M5.Lcd.setCursor(28, 44);
    M5.Lcd.print("DONE");
    delay(2000);
    Serial.println("Ready");
   
    Trigger = false;
  }
  else
  {
    if ( BackGround != "BLACK" )
    {
      Serial.println("Ready");
      //M5.Lcd.setBrightness(20);
      M5.Lcd.fillScreen( BLACK );
      M5.Lcd.setTextColor(GREEN);  
      M5.Lcd.setTextSize(2);
      M5.Lcd.setCursor(48, 14);
      M5.Lcd.print("READY");
      BackGround = "BLACK";
    }
  }
}
[/mw_shl_code]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 08:46 , Processed in 0.156147 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表