Using the same push button for different purposes.

Just a question for thought, no solid codes ready yet.

I am coding to make the following happen:

Main loop can select between 3 modes by push button 'A':

  1. Normal
  2. Bypass
  3. Off

In "Normal", another push button 'B' can be used to change between 3 screens:

  1. Screen 1
  2. Screen 2
  3. Screen 3

In Screen 1, a toggle switch will be used to change between 3 modes:

  1. Schedule
  2. Off
  3. Sensor

In Schedule, another push button 'C' changes between displaying the following:

  1. 2s
  2. 5s

Using pushbutton 'A', I access Bypass mode.

What I plan to do is to use push button 'B' and 'C' to control a different purpose. This does not affect any of the above schedules and screens. When in Bypass mode, it only affects Bypass system.

When I press down push button 'B', it displays:
1s

When I press down push button 'C', it displays:
3s

The main issue is how should I program it such that push buttons 'B' and 'C' can control in Normal mode and Bypass mode while not affecting one another's operation in their respective modes.


My current code is not completed and tested yet. Will update when it can work. Hope the above explanation was clear enough. Feel free to ask any questions if it was unclear. Thanks in advance for all

When processing the button press, check what screen it's currently on (which will presumably be stored in some global variable). Use if statement to do something different depending on what mode it is in.

This is a good example of where taking a stab at the coding would have been helpful before asking - I suspect you'd need some if statements when processing the button presses anyway, so it would be natural to extend that to check what screen it's on (in fact, you probably already will need this).

You can also do away with the toggle switch and use one of the buttons to scroll through the options....

You are looking at a simple state machine. With each button push, process the current state and in the same function, change current state to the next state. Switch states using a .... switch statement.

Fun joke, but the switch() statement in a state machine IS the states. Depending on what state it's in, it does different things. In most of those states there are some conditions which, if satisfied, cause the state variables to change. For those, I use if() statements. So the if() switches state.

Thanks. I will look through it when I am free. Oh boy, more state detection.