The while loop keeps polling the ADC and checking the results against the if statements until one of the conditions is met or a timeout is reached. Then x=1 stops the while loop and allows the function to end. The while loop runs approx 150 times before timeout is reached or less if one of the other conditions is met.