Constrain() ultrasonic inputs to control LED

pingEcho is an analog input pin.

Just a guess, this might be what you’re trying to do:

duration = constrain(duration, 2, 22);
duration = map(duration, 2, 22, 0, 255);

The two lines above must be after the call to pulseIn(), that’s what Paul is telling you.