[arduino] vl6180x return time duration

Hello there,

I’ve got 3 of these sensor and I’m pretty happy about it. But I can’t figure out how to return the sensor working duration time if the threshold is triggered. I already tried the millis() function but all my attempts are failed. Here is the related code:

boolean isSensor2AboveThreshold() {
  return sensor2.readRangeSingleMillimeters() > sensor2_threshold;
}

void loop() {
  tcaselect(2);
  // Serial.print(sensor2.readRangeSingleMillimeters());

  switch (current_state) {
  case THRESHOLD_ABOVE:
    if (isSensor2AboveThreshold()) {
      //elapsed_time = millis() - start_time;
      Serial.print("threshold_ABOVE");
    } else {
      //counter_below_ms = counter_above_ms;
      current_state = THRESHOLD_BELOW;
      //final_ms = counter_above_ms - counter_below_ms;
    }
    //break;

  case THRESHOLD_BELOW:
    if (!isSensor2AboveThreshold()) {
      //counter_below_ms = millis();
      Serial.print("threshold_below");
    }
    /*else{
      
    }*/
    // break;
  } //end of switch(current_state)

  Serial.print("The blue ball is held up for: ");
  Serial.print(elapsed_time);
  Serial.print(" ms");
} //////////////////////////// end of loop

thanks in advance.

Hello.

In the code you posted, current_state is never declared and does not get set to any specific value anywhere outside of the conditional its value depends on.

If you have not done so already, you might try starting with simple code that just returns a timestamp and the sensor value to your serial monitor as the physical movement you are trying track occurs so you can get a good sense of the data the sensor returns before starting to add conditionals.

-Nathan