Averaging ultrasonic sensor reading to reduce noise

Please help! I am a complete noob in programming and I followed a tutorial from Youtube for this. This is a water level monitoring system.

The problem is that when the ultrasonic sensor is getting the distance from the water tank, the ripples or waves from the water when the tank is being filled causes the level indicator from the blynk app to repeatedly say it’s empty.

Please someone help me with the code. My idea is that it needs to read the distance 5 times per sec for 5 seconds then send the average to the blynk app.

#define trigger D6 //D6 digital pin for trigger of ultrasonic sensor
#define echo D7 //D7 for echo
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "v4DOSzwtwwlRpzo5t2aJqVQ8uJeabMaH"; //Get this on the Blynk app (unique for every device)
char ssid[] = "WaterLevelMonitor"; //WiFi name
char pass[] = "14159265";

void setup() 
{
 Serial.begin (9600);
 Blynk.begin(auth, ssid, pass);
 pinMode(trigger, OUTPUT); //D6
 pinMode(echo, INPUT); //D7
 pinMode(BUILTIN_LED, OUTPUT); //Blue light will turn on if the project is online
 
}
void loop()
{
 
 long duration, distance, avedist;
 digitalWrite(trigger, LOW);  
 delayMicroseconds(2); 
 
 digitalWrite(trigger, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigger, LOW);
 duration = pulseIn(echo, HIGH);
 distance = (duration/2) / 29.1; //Distance calculation per cm

 
 if (distance >= 32 ) {
   Blynk.virtualWrite(V0, 255);
}
 else {
   Blynk.virtualWrite(V0, 0); //Empty
 }
 
 if (distance > 28 && distance < 32 ) {
   Blynk.virtualWrite(V5, 255);
}
 else {
   Blynk.virtualWrite(V5, 0); //Nearly empty
 }

  if (distance > 15 && distance < 28 ) {
   Blynk.virtualWrite(V1, 255);
}
 else {
   Blynk.virtualWrite(V1, 0); //Optimal water level
 }
 
 if (distance > 10 && distance < 15 ) {
   Blynk.virtualWrite(V4, 255);
}
 else {
   Blynk.virtualWrite(V4, 0); //Nearly full
 }

  if (distance <= 10 ) {
   Blynk.virtualWrite(V2, 255);
}
 else {
   Blynk.virtualWrite(V2, 0); //Full
 }
 
 Blynk.virtualWrite(V3, distance);
 delay(100);
 Blynk.run();
}

The simplest approach is to add up five distance values and divide by five.

Try it out and if you get stuck, post your best attempt and explain what goes wrong.

As an aside, don't expect the sensor to last long in a humid environment, weeks at best.

Actually the simplest approach is a one-pole digital low pass filter, as you only have to store 1 value, not 5.

void calc_average()
{
  static float average = -1.0 ;
  if (average == -1.0)  // special case for first reading.
  {
    average = read_distance () ;
    return average ;
  }
  average += 0.1 * (read_distance() - average) ;  // 0.1 for approx 10 sample averate, 0.01 for 100 sample etc.
  return average ;
}

float read_distance ()
{
  digitalWrite(trigger, LOW);  
  delayMicroseconds(2);
 
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
 
  digitalWrite(trigger, LOW);
  long duration = pulseIn(echo, HIGH);
  return (duration/2) / 29.1; //Distance calculation per cm
}