vl53l0x my project detection

Hello, i'm new to arduino world, so bare with me, i'll try my best to explain my issue, i've been trying for couple of days to make this work, but nothing worked so far, used the forums here, google etc, what i'm trying to do is. if i stand in front of the sensor, to show me that someone or something is in front of it, this part is covered by some code i found and works well so far, i'll attach it below, however the real issue that i cant figure it out is, if that person or object moves outside of senzor scope for 1 or 2 or 3 seconds the sensor/code "thinks" the person "left for good" what i want is to count those seconds that's its off the sensor view, and based on the seconds to decide what i do next. the reason i want this, is due to sensor might get an bad reading, or person in front moves for 1 or 2 seconds and sensor doesnt read him.

Also sorry for my english and mistakes. If can provide a working example it would be much appreciated, since i'm a noob with software i dont fully understand the jargon and functions of a code. Much appreciated and i hope someone can help me !

My partial code so far:

Sorry about that, i updated the code with the fully working version, can you please modify my current code with the one that i'm trying to do ? i would really appreciated, i've tried the method you were saying, but i wasn't able to do it. thanks for your time !

// 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);
  }

}

The easiest way would be to start a second timer when you transition from "detection" to "no detection" similar to what you are doing now. The current time minus that no detection time would be the amount of time you have not detected anything.

Also, that code does not even compile. Please post a complete sketch with all your variable declarations, #includes, etc.

Sorry about that, i've updated my code

What have you tried? Post your attempt and the community can help you fix it. If you ask people to write code for you, then you should hire a programmer. That is a different forum.

I cant give here all trial and erros that i've tried since i deleted them, i just gave the code that got me here, and im stuck, if its to hard for you to help someone then dont bother to answer, i dont ask anyone to write code for me, just to point me in a right direction with an example code to my problem.

I did that in my very first reply... Add a second timer to your code.

I've tried that but i dont know how to tie a second timer to last timer. But ok, thank you for your time and help.

It'd look like this:

if (presence) {
  latestPresence = millis();
}
if (milis() - latestPresence > timeout) {
  leftForGood = true;
}
else {
  leftForGood = false;
}

So while there's someone present, keep updating the latestPresence value. When not present, stop updating and look for some time to pass.

wvmarle thanks for taking the time to answer, i've tried the method you were giving me, but doesn't really work like i'd hoped it would its almost perfect but, as soon as i start the sensor this line gets executed and then after 5 seconds it changes to "left for good".

else {
  leftForGood = false;
  Serial.println("still there");
}

Is there a way to start "your sample code" if distance < 20 cm start your code to run ? i've added my self distance < 20, but doesnt work as i tought it would, really appreciate it, and thanks in advance !

i'll post my sketch from your sample code

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

VL53L0X sensor;


boolean leftForGood;
unsigned long latestPresence;
unsigned long timeout = 5000; // 5 seconds


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" );



if ( distance <= 20 ) {   // if distance less than 20 cm start timer ?
  latestPresence = millis();
  }
if (millis() - latestPresence > timeout) {   
  leftForGood = true;
  Serial.println("left for good");
}
else {
  leftForGood = false;
  Serial.println("still there");
}



}

negusoi:
as soon as i start the sensor this line gets executed and then after 5 seconds it changes to "left for good".

Initialise your leftForGood flag to be true, as it seems you want that to be your starting condition:

boolean leftForGood = true;

thanks again for taking the time to response, i really appreciate it. I've tried setting that boolean to true, but is like is not even there, i get same behavior. For some reason, the if condition is not taken in consideration when distance is < 20, and the code starts automatically, is not tied to if distance < 20, and i've tried to add entire code into that condition, and works ok if i put my hand in front of the sensor "it displays still there" but when i remove the hand from it, nothing happens its just gets normal reading, something is missing but i dont know what. i've attached a screenshot of the sensor reading and explained there the behavior i'm after.