hc-sr04 time how long an object is in range

I need help trying to figure out how long an object stays in front of the sensor in
in a specified range, for example, if an object is in within range of 10cm and 200cm
I start a timer, the moment that's not true the timer stops, but my current problem
has been the variations in the distance will automatically register as a new event.

Here's is my failed attempt on Gitlab, thank you

Save the start time when the object becomes in range, not when it is in range. Set a boolean to true when that occurs and don't record the start time again until after the object has gone out of range (set boolean to false) and back into range.

See the StateChangeDetection example in the IDE.

If you need more help then please post your code here rather than a link to it.

for example, if an object is in within range of 10cm and 200cm
I start a timer

Not in the code on github. There is nothing in that code that is even remotely related to time.

The reason there’s no timer in the code is due to the fact that the Timer library wasn’t supported on the Node MCU ESP12E, so I was going to figure that part later. I welcome all corrections and ideas to rectify my mistakes.

Sketch Code :

#include <NewPing.h>

#define TRIGGER_PIN 5 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 4 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

int personCounter;
int currentState = 0;
int previousState = 0;
int maxDistanceRange = 100;
int minDistanceRange = 10;
int currentPingedDistance;
//int humanPing = sonar.ping_median(iterations);
int pingCounter;
int pingsOnObject;

//int objectInRange = currentPingedDistance <= maxDistanceRange && currentPingedDistance > minDistanceRange;

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {

if (sonar.ping_cm() <= maxDistanceRange && sonar.ping_cm() > minDistanceRange) {

pingsOnObject = 1;

while (sonar.ping_cm() <= maxDistanceRange && sonar.ping_cm() > minDistanceRange)
{
delay(50);
sonar.ping();
Serial.print(sonar.ping_cm());
pingsOnObject++;
Serial.print(“Pings”);
Serial.print(" - ");
Serial.println(pingsOnObject);
if (pingsOnObject >= 5)
{
break;
}
else {
continue;
}
}

if (pingsOnObject >= 5) {

pingCounter = 1;
currentState = 1;

while (sonar.ping_cm() <= maxDistanceRange && sonar.ping_cm() > minDistanceRange) {
// delay(500);
pingCounter ++;
}
Serial.print(“Pings On Person”);
Serial.print(" - ");
Serial.println(pingCounter + 5);
}
else {

currentState = 0;
}

if (currentState != previousState) {
if (currentState == 1) {
personCounter = personCounter + 1;

Serial.print(“Persons”);
Serial.print(" - ");
Serial.println(personCounter);
}
}

} else {

pingsOnObject = 0;
}

}

The reason there's no timer in the code is due to the fact that the Timer library wasn't supported on the Node MCU ESP12E, so I was going to figure that part later.

You need to stop talking about timers. You do NOT need to use a timer to determine how long something has been somewhere.

Suppose you saw a pothole had formed in the street in front of your house. Supposed that you reported it, and were promised that it would be fixed, and that you should call back in 3 days if it wasn't. Could YOU figure out when to call again, using nothing more than a calendar, a watch, a pencil, and some paper? You can NOT use a timer.

Of course you can. You write down the date and time that you called. Periodically, you see if the pothole is still there, and, if it, if it has been three days since you reported it. I'm sure that it is pretty obvious how to determine if now minus then exceeds the threshold.

SimbaSara:
I need help trying to figure out how long an object stays in front of the sensor in
in a specified range, for example, if an object is in within range of 10cm and 200cm
I start a timer, the moment that’s not true the timer stops, but my current problem
has been the variations in the distance will automatically register as a new event.

unsigned long TimerStart;
void loop()
{
    int distance = measureDistance();
    if (distance < 10 || distance > 200)
        TimerStart = millis();
   unsigned long timeInRange = millis() - TimerStart;  // Milliseconds that object has been in range
}