The biggest problem is that by forcing the code to use integral inches you've restricted it to 31 discrete values. And since <a href="https://electronics.stackexchange.com/questions/1983/correcting-for-non-linear-brightness-in-leds-when-using-pwm">the brightness response of a PWMed LED is non-linear</a> you effectively have *very* large steps in brightness when the LED is dim.

`pulseIn()` has *much* more flexibility with regards to its range so use the raw value directly instead of converting it to "pretty" values.

    val = map(pulse, ..., ..., 0, 255);