How to refine ultrasonic readings
elapsedTime=millis(); sets elapsedTime equal to the current value of millis(), which in the sketch above is the total time since the Uno powered up.  Thus  elapsedTime will exceed 1000 any time after the first second of execution, in which case the while won't do anything.

Instead say elapsedTime=millis() - StartTime; within the loop.  Whenever you want to start the averaging cycle again, say StartTime = millis();.

If your only use of elapsedTime is to control the while loop, get rid of that variable; instead of  while(elapsedTime &lt; 1000), say while(millis()-StartTime &lt; 1000).