all understood, and tested prior to fading code.
Go back to this code and retest (or comment out the fade stuff). It sounds to me as though your test environment is causing you issues - try pointing the sensor at a featureless section of wall, midway up it. Does that give consistent results?