Arduino vl53l0x传感器

问题描述 投票:1回答:1

我正在尝试使用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);
  }

}
arduino sensor arduino-c++
1个回答
1
投票

如果想要将手放在传感器前面5秒后再触发,请尝试以下操作:

void loop() {

  // Get distance
  delay(1000);
  int tofdata = sensor.readRangeContinuousMillimeters();
  int distance = tofdata / 10;        // convert mm to cm

  //  Code for presence detection

  if (distance <= 20 ) {
      // Object is close, check if timer is running
      if (!timerRunning) {
          // Timer not running. Start timer
          startTime = millis();
          Serial.println("time started, starting count");
          timerRunning = 1;
      }
      else {
          // Has 5 seconds passed?
          uint32_t elapsed_time = millis() - startTime ;
          if (elapsed_time >= 5000) {
              // YES. DO SOMETHING HERE
              // and reset
              timerRunning = 0;
          }
      }
  }
  else {
      // Object is not close.
      timerRunning = 0;      
}
© www.soinside.com 2019 - 2024. All rights reserved.