I want to know what's wrong with my coding

Of course it is working! If it does not work as you expect then please tell us what it should do.

You also may have to read more about state machines, if the code should do what I guess.