Pages: [1]   Go Down
Author Topic: One button to serve different purpose  (Read 145 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 2
Posts: 338
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 116
Posts: 4325
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if (state == 0)
{
  //read the button and act accordingly for state 0
}
but when the state is different, say 1
Code:
if (state == 1)
{
  //read the button and act accordingly for state 1
}
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Jr. Member
**
Karma: 1
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
  ….
}
Logged

Pages: [1]   Go Up
Jump to: