I can't see anything wrong wih the code although it might be possible to restructure it to be more efficient. Possibly the large number of 'if' instructions has made the compiler produce poor code or something else clashes (some interrupt code or timer perhaps) at the same instant the value is 30.
You could try this method instead, it usually produces less code:
1. if 'time' is updating constantly, take and use a copy of it so the value doesn't change while running this part of the code.
2. instead of checking for the upper and lower values each time, check the difference between them and subtract the lower value from the result each time.
So 'time' (or the copy you made of it) reduces as it goes through each check and you only have to test for equality or the maximum difference.
Brian.