[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() {
  // Serial.print(sensor2.readRangeSingleMillimeters());

  switch (current_state) {
    if (isSensor2AboveThreshold()) {
      //elapsed_time = millis() - start_time;
    } else {
      //counter_below_ms = counter_above_ms;
      current_state = THRESHOLD_BELOW;
      //final_ms = counter_above_ms - counter_below_ms;

    if (!isSensor2AboveThreshold()) {
      //counter_below_ms = millis();
    // break;
  } //end of switch(current_state)

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

thanks in advance.


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.