Stop Measuring Distance after Averaging Value

Hello, I’m working with TOF10120. The sensor is measuring distance in about max 20 cm. The idea is to build a measuring method like market product Laser Distance Meter that give measurement and stay at that point.

Can anyone give idea to getting closer of that? I tried Averaging and median method of analog read but the result still not meet the requirement.

Post the code and the hardware..

it may help a little

I'm using Arduino Nano + TOF10120 Sensor for single sensor, but my plan is going to use Nano + TCA9548A + TOF10120 + OLED

This is the code that I found from this reference : ESP32 with TOF10120 Laser Rangefinder distance sensor, IoT water Level Monitoring

#include <SimpleTimer.h>

#include<stdlib.h>
#include <Wire.h>

SimpleTimer timerSensor1;
SimpleTimer timerRef;

int secs1 = 0;

unsigned short lenth_val = 0;
unsigned char i2c_rx_buf[16];

int raw_distance;

int resultDistance;

void setup() {
  Wire.begin();
  Serial.begin (9600);

  timerSensor1.setInterval(200L, func_AVG1);
  timerRef.setInterval(1000L, timeRef);
}

void loop() {
  timerSensor1.run();
  timerRef.run();

  raw_distance = ReadDistance();

  Serial.print(F("Distance : "));
  Serial.println(raw_distance);
  Serial.print(F("Distance AVG : "));
  Serial.println(resultDistance);

  delay(200);
}

void func_AVG1()
{
  int x1;
  float raw_distanceAVG = 0;
  for (int tof1 = 0; tof1 <=  19; tof1++)
  {
    x1 = x1 + raw_distance;

  }
  raw_distanceAVG = x1 / 20;
  x1 = 0;

  if ( (raw_distanceAVG < resultDistance) || ( raw_distanceAVG > resultDistance))
  {
    if ( secs1 == 4 )
    {
      resultDistance = raw_distanceAVG;
    }

  }

  if (resultDistance == raw_distanceAVG )
  {
    secs1 = 0;
  }

}

void timeRef()
{
  secs1 = secs1 + 1;
  if ( secs1 == 5 )
  {
    secs1 = 0;
  }

}

void SensorRead(unsigned char addr, unsigned char* datbuf, unsigned char cnt)
{
  unsigned short result = 0;
  Wire.beginTransmission(0x52);
  Wire.write(byte(addr));
  Wire.endTransmission();
  delay(1);
  Wire.requestFrom(0x52, cnt);
  if (cnt <= Wire.available()) {
    *datbuf++ = Wire.read();
    *datbuf++ = Wire.read();
  }
}

int ReadDistance() {
  SensorRead(0x00, i2c_rx_buf, 2);
  lenth_val = i2c_rx_buf[0];
  lenth_val = lenth_val << 8;
  lenth_val |= i2c_rx_buf[1];
  delay(100);
  return lenth_val;
}

I tried on a calibration of 100mm and get tolerance about max 12mm of result and still volatile.
What measurement method to get result like market product of Laser Distance Meter that just display a static result of measurement (maybe just stop when reach at avg value) ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.