【问题标题】:Arduino vl53l0x sensorArduino vl53l0x 传感器
【发布时间】:2019-03-18 20:15:45
【问题描述】:

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

}

【问题讨论】:

  • 如果你想让它在你的手在前面(小于 20 厘米)时发射,请摆脱定时器的东西并做任何事情if (distance &lt; 20) { Serial.println ("Presence detected."); }
  • 但是如何在 5 秒后触发它?
  • 所以你想让它在手放在传感器前面 5 秒后触发?
  • 是的,例如我把手放在它前面,保持5秒钟,然后做某事,如果不到5秒钟,什么也不做或做某事

标签: arduino sensors arduino-c++


【解决方案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;      
}

【讨论】:

  • 天啊,你不知道我花了多少精力来完成这项工作,我希望我能给你一杯真正的啤酒 :),我可以加你为朋友吗?或者以后有什么联系你的?我试过在 arduino 论坛上寻求帮助,但那里的人很邪恶.. 再次感谢!
  • 当有人回答您的问题时,您可以点赞并接受。见stackoverflow.com/help/someone-answers
  • 遗憾的是我不能投票,说了一些关于 15 名声的事情,可能是因为我刚刚注册
  • 没问题。您在使用网站时获得声誉和“特权”
猜你喜欢
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 2014-03-31
  • 1970-01-01
  • 1970-01-01
  • 2013-07-01
相关资源
最近更新 更多