JSNSR04T V2 unstable values

Im using JSNSR04T version 2, im getting some spikes , some times value jumps.. but im not using this for distance. Im using it for Level measurement

can any one suggest edit on my code for reducing spikes and jumps? my idea is, if there is a spike, i dont need that value to be printed, or I need to print constant value, unless last 5 values have changed

Please help, I need to use this sensor itself b coz the non waterproof sensor is getting damaged frequently

as trial and error method i changed the sending time as 50 micro seconds ... looks little stable

void ultrasonic()
{
  // Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
// Sets the trigPin on HIGH state for 50 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(50);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
a=distance;
 if( (a >= 5) && (a<= 255) ) {
    Serial.println(a);
         }

Im using JSNSR04T version 2

I can accept that you know what that means. I haven't a clue.

can any one suggest edit on my code for reducing spikes and jumps?

Average the readings. Keep track of the previous reading. Ignore any that are radically different. Post ALL of your code. Pick any two.

This is my complete code

const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance,a,b;
 int ircount = 0;
 int level = 0;
 void ultrasonic();
void setup()  {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT_PULLUP); // Sets the echoPin as an Input
Serial.begin(9600); //Start serial communication boud rate at 9600
}
void loop()  {
 while(1)  {
  level++;
   if(level==25){
    ultrasonic();
   }
  if(digitalRead(2)==HIGH){
  ircount=0;}
   delay(50);
   if(digitalRead(2)==LOW)  { 
    // If no signal print collision detected
     ircount++;
      if(ircount==1) {
  
   }
     }
  
 }
}
void ultrasonic()
{

  
  // Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
// Sets the trigPin on HIGH state for 50 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(50);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
a=distance;
 if( (a >= 5) && (a<= 255) ) {
    Serial.println(a);
         }
level = 0;
  }

hope you will give me an edit

void loop()  {
 while(1)  {

The first obvious question is why do you have an infinite loop in a function that gets called in an infinite loop?

The next question concerns your inconsistent placement of curly braces. There are camps that say that every { goes on a new line. I'm firmly in that camp. There are camps that say that the { goes on the line with the statement. They are wrong, but they won't listen to reason. 8).

EVERYONE agrees that NOTHING goes on the same line as the }, except when the } closes out an array initialization statement. Even then, a space before the }, so that it stands out, is a good idea.

Everyone agrees that consistent indenting is a good idea.

Everyone agrees that one letter global variable names are a bad idea.

Most people agree that limiting the scope of variables is a good idea. duration is only used by ultrasonic(), so it should not be a global variable. distance shouldn't be, either, because ultrasonic() (which really needs a better name, like measureDistance()) should be returning a value.

If you need to trigger the sensor, and read the echo time, 10 times, and average the result, this is much easier to do if the variables that are involved are all right there in the function.

while(1)  {

level++

While not actually harmful, this code does seem to have missed out on all the benefits the Arduino environment provides.

loop() is the equivalent of while(1) so that while is unnecessary. It prevents some of the other rarely-used features if the Arduino from working.

The counter called level seems to be an attempt to reduce the number of times that the ultrasonic sensor is used. But the intervals are uncontrolled. Much better to use a millis() timer for this. Even delay() would be an improvement. Level is a strange name to use for a counter when it seems that the purpose of the code is to measure a level.

Here is my favorite filter for this kind of stuff: Recursive Implementation the other chapters of the book have many other useful filters.