JSNSR04T V2 unstable values

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.