I'd just add that its worth learning to use state-machines for this sort of interface - states and state-transitions
and a good way to understand and develop this sort of code. What happens for each input change commonly
will depend upon what state you are in, so make that explicit in the code and it will be much easier to read.