Go Down

Topic: One button to serve different purpose (Read 161 times) previous topic - next topic

Vincent19

Dear all,

I am planning to have a menu based system with let say 2 tact switch.

When button 1 is pressed, it enter screen 1.
When it is in screen 1, when I press the same button 1, it will go to screen 2.

How is it possible to do so ?

Thank you.

UKHeliBob

Sounds like you need a finite state machine.  That sounds scary but it's not.

At any time the system will be in one of several states, each of which you give a number or a name.  So, when in say, state 0
Code: [Select]
if (state == 0)
{
  //read the button and act accordingly for state 0
}
but when the state is different, say 1
Code: [Select]
if (state == 1)
{
  //read the button and act accordingly for state 1
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Sleepydoc

Or you could use 1 or 2 buttons (or a rotary encoder, if you're feeling more ambitious) to navigate a menu with several functions and then call a function based on the one you choose, roughly like this:
Code: [Select]

selection = 0;
lcd.print(menuItem[0]);
do {
  if (down button pushed){
    selection = selection + 1;
    if (selection > max menu item #) {
      selection = 0;
    }
    lcd.clear();
    lcd.print(menuItem[selection]);
  }
  if (up button pushed){
    selection = selection -1;
    if (selection = 0) {
      selection = max menu item #;
    }
    lcd.clear();
    lcd.print(menuItem[selection]);
  }
} while (select button not pushed)
switch (selection) {
  case 0:
    function0();
    break;
  case 1:
    function1();
    break;
  ….
}

Go Up