Receiving quite erratic MIDI values using an Ultrasonic Sensor

I'm trying to control an effect parameter in my music software using an ultrasonic sensor.
I've managed to get my sensor to send MIDI but my my MIIDI monitor shows that the values are quite erratic and also they don't seem to be reaching 127 or maximum effect strength.

I started with the Control Surface Midi Output example for CC messages using a potentiometer, maybe someone can look at my code and tell me why I might be finding it difficult to get smooth values from my sensor and therefore smooth MIDI values.

Thanks.

Ultrasonic_OMOR15.ino (1.55 KB)