2

vl53l0x センサーを使用してトイレの感覚的なトリガーを作成しようとしています。手がセンサーの前に 5 秒ほどある間、アクションを実行するのに問題があります。また、blinkwithoutdelay スケッチのさまざまなバージョンを試してみました。オンラインで見つかったメソッドはすべて、センサーから手を引っ張った後、5 秒間トリガーされますが、これは私が望んでいるものではありません。事前に感謝します。これまでに得たものにスケッチを投稿しました。前もって感謝します !

// Library for TOF SENSOR
#include <Wire.h>
#include <VL53L0X.h>

VL53L0X sensor;

// Time calculation
unsigned long startTime;
unsigned long endTime;    // store end time here
unsigned long duration;   // duration stored
byte timerRunning;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Wire.begin();
  sensor.init();
  sensor.setTimeout(500);

  // Start continuous back-to-back mode (take readings as
  // fast as possible).  To use continuous timed mode
  // instead, provide a desired inter-measurement period in
  // ms (e.g. sensor.startContinuous(100)).
  sensor.startContinuous();

}

void loop() {
  // put your main code here, to run repeatedly:

  delay(1000);
  int tofdata = sensor.readRangeContinuousMillimeters();
  int distance = tofdata / 10;        // convert mm to cm
  Serial.print( distance );            // print new converted data
  Serial.println( " cm" );


//  Code for presence detection

  if ( timerRunning == 0 && distance <= 20 ){
      startTime = millis() / 1000;
      Serial.println("time started, starting count");
      timerRunning = 1;  

  }

  if ( timerRunning == 1 && distance >= 20 ){
      endTime = millis() / 1000;
      timerRunning = 0;
      duration =  endTime - startTime;
      Serial.println ("Presence detected for seconds: ");
      Serial.print(duration);
  }

}
4

1 に答える 1