Ultrasonic distance sensor - Noise filter question

Hello community.

I am seeking assistance to my ultrasonic distance measure project.

I am using a Maxbotix 10 meter sensor with Arduino nano 3.1v.

I am running fast and manually triggered pulses, for real-time operation continuously calculating the distance between a subject (human) and a camera's film plane.

However, I get a lot of reported bad readings, when the subject moves. The sensor reports the background and the values are clearly wrong.

So how do I run a filter that can eliminate these obvious wrong readings? A filter that can ignore a reading that is "unexpected"? I am pretty new to Arduino, and I have been spending a lot of time to get this code right in the first place. So I would really appreciate all the help I can get from you guys!!

This is my code:

#include <SoftwareSerial.h>
SoftwareSerial HC12(5, 4); // HC-12 TX Pin, HC-12 RX Pin

const int pwPin1 = 3;
int triggerPin1 = 13;
long sensor1, cm1;

void setup() {
  Serial.begin(19200);             // Serial port to computer
  HC12.begin(19200);               // Serial port to HC12'
  pinMode(pwPin1, INPUT);
  pinMode(triggerPin1, OUTPUT);

}

void start_sensor() {
  digitalWrite(triggerPin1, HIGH);
  delay(1);
  digitalWrite(triggerPin1, LOW);
  delayMicroseconds(25);
}

void read_sensor() {
  sensor1 = pulseIn(pwPin1, HIGH);
  cm1 = sensor1/10;
}

void send_data()
{
  HC12.println(cm1);
  Serial.println(cm1);
}


void loop ()
{
  start_sensor();
  read_sensor();
  send_data();
  delay(25);
}

Thank you so much!!
All the best,
Lasse

Keep a record of the last distance before you get a new distance reading. Compare the new distance with the old one. If the difference is too much, ignore the new reading.

It's exactly what I need! But I am not sure how to get there.

I would like to be able to measure and act on the difference between previous range reading and current range reading in percentage. So if the new reading is, say 50% different than the previous, it should ignore it.

This is how far I got on my own, and I am not even sure it is right.

#include <SoftwareSerial.h>
SoftwareSerial HC12(5, 4); // HC-12 TX Pin, HC-12 RX Pin

int currentValue;  // global variables are retained on each iteration of loop()
int previousValue;

const int pwPin1 = 3;
int triggerPin1 = 13;
long sensor1, cm1;

void setup() {
  Serial.begin(19200);             // Serial port to computer
  HC12.begin(19200);               // Serial port to HC12'
  pinMode(pwPin1, INPUT);
  pinMode(triggerPin1, OUTPUT);

}

void start_sensor() {
  digitalWrite(triggerPin1, HIGH);
  delay(1);
  digitalWrite(triggerPin1, LOW);
  delayMicroseconds(25);
}

void read_sensor() {
 previousValue = currentValue;  // store what was read last time
 currentValue = pulseIn(pwPin1, HIGH); // get a new reading
 
 if (previousValue != currentValue) {  // compare them
 // Insert code to calculate percentage difference. If the difference is higher than 50%, do nothing

 }
      HC12.println(currentValue);
      Serial.println(currentValue);
 } else {
   // ignore

}


void loop ()
{
  start_sensor();
  read_sensor();
  delay(25);
}