US-100 ultrasonic sensor is geeting first reading higher than normal

I am trying to use US-100 sensor to measure distance but it gets wrong reading (first reading only) in loop.

My main sketch is for water level monitor / control with EEPROM (pump states in case of power failure). Due to this wrong reading it get trip in loop after startup. When I check the sensor reading separately I observed first reading is wrong and due to this pump trip.

Can any one point out the problem or any solution.

#include <SoftwareSerial.h>;
#include <LiquidCrystal_I2C.h>               //addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int US100_TX = 11;
const int US100_RX = 12;

SoftwareSerial US100Serial(US100_RX, US100_TX);

unsigned int MSByteDist = 0;
unsigned int LSByteDist = 0;
unsigned int mmDist = 0;
int temp = 0;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  US100Serial.begin(9600);
}

void loop() {

  US100Serial.flush();
  US100Serial.write(0x55);

  delay(500);

  if (US100Serial.available() >= 2)
  {
    MSByteDist = US100Serial.read();
    LSByteDist = US100Serial.read();
    mmDist  = MSByteDist * 256 + LSByteDist;
    if ((mmDist > 1) && (mmDist < 10000))
    {
      Serial.print("Distance: ");
      Serial.print(mmDist, DEC);
      Serial.println(" mm");
      lcd.setCursor(0, 0);
      lcd.print("Distance: ");
      lcd.print(mmDist, DEC);
       lcd.print("  ");
    }
  }

  US100Serial.flush();
  US100Serial.write(0x50);

  delay(50);
  if (US100Serial.available() >= 1)
  {
    temp = US100Serial.read();
    if ((temp > 1) && (temp < 130)) // temprature is in range
    {
      temp -= 45; // correct 45º offset
      Serial.print("Temp: ");
      Serial.print(temp, DEC);
      Serial.println(" ºC.");

            lcd.setCursor(1, 1);
      lcd.print(" Temp:  ");
      lcd.print(temp, DEC);
       lcd.print(" ºC.");
    }
  }

  delay(500);
}

Serial out put is like: (only first reading wrong)

Distance: 280 mm
Temp: 27 ºC.
Distance: 200 mm
Temp: 27 ºC.
Distance: 201 mm
Temp: 27 ºC.
Distance: 200 mm
Temp: 27 ºC.

Well, can you code to reject x number of readings at start up?

Idahowalker:
Well, can you code to reject x number of readings at start up?

Can you suggest some method.

Or You can make one reading, a dummy reading or a wast reading of the sensor just before the loop begins.

I use some filtering like:

float filteredReading = 0;
  int sample = mmDist;
  filteredReading = .25 * filteredReading + .75 * mmDist;

It works for me but if my water level is near with trip level it may be trigger wrong limit.

That filtering looks uncertain to me. The variable filteredReading is set to 0.0 and the 0.75 factor works just for this magnitude of the faulty first reading. 0.75 * 280 == 210...… If the bad value would be 500 the filter will not work.

Railroader:
That filtering looks uncertain to me. The variable filteredReading is set to 0.0 and the 0.75 factor works just for this magnitude of the faulty first reading. 0.75 * 280 == 210...… If the bad value would be 500 the filter will not work.

Can you suggest how dump first few readings

If only the very first reading is corrupt than make one reading without using the result. You can do that in the end of Setup(). If You want to drop several readings the make several such dummy readings, waist readings, in the end of Setup().
Just copy the needed lines of code from lop() into the end of Setup().

int NumberOfDumps

if NumberOfDumps < 3
increaseNumberOfDumpsByOne
else
do the other things.

Idahowalker:
int NumberOfDumps

if NumberOfDumps < 3
increaseNumberOfDumpsByOne
else
do the other things.

working as expect but sligltely delay due to serial Eco.

US100Serial.flush();
  US100Serial.write(0x55);

  delay(500);

  if (US100Serial.available() >= 2)
  {
    MSByteDist = US100Serial.read();
    LSByteDist = US100Serial.read();
    mmDist  = MSByteDist * 256 + LSByteDist;
    if ((mmDist > 1) && (mmDist < 10000))
    {
      Serial.print("Distance: ");
      Serial.print(mmDist, DEC);
      Serial.println(" mm");
      lcd.setCursor(0, 0);
      lcd.print("Distance: ");
      lcd.print(mmDist, DEC);
       lcd.print("  ");

Thanks to all for you suggestion and specially for "Idahowalker" I just add boolean logic and run only one time in loop and solve the delay problem also.

Thanks,