jsotola wrote a very important term in the comments: state.
You should implement your project as a Finite State Machine (FSM), a very important and powerful, yet easy principle. You divide your program into different states. Each state contains code, that only gets executed, when the program is in this state. Depending on input events (like the user ...
What you likely need is a state machine. The behavior of the actions depends on the state the program is currently in.
In your case, the state machine might look like this:
What does it mean?
black circle: this is where the system powers on
rectangle: a state
arrow: a transition from one state to another
green text: action
For the problematic double ...