Help with reducing lag on code

It should detect something as the user sweeps it across himself. From left to right

if (newaverage < 140 - mappedpot)

At what distance to the reflective target are you trying to trigger the buzzer?

Link to the data sheet.