The following processing code is a simulation of the PIC's algorithm.
Move the mouse between the black and white regions; Crossing between the regions signifies a hi/low change on the sensor-pin, similar to when the sensor is being moved. Notice the movement of the red bar during motion and rest. Also, the test-led lights up when the red bar crosses the threshold.

To view this content, you need to install Java from java.com

Download the code to experiment with various settings for this algorithm.