Lag(?) issues

The code would be much easier to follow if you used enum values or #defines to name the states. Without those clues I have no idea what states 1, 2, 3 etc are intended to represent so I have no way to judge whether the actions taken in those states are appropriate.