Using a keyswitch to select a function (and announce the change..)

Hi all,

For my new project I have bought a keyswitch with 3 positions which I want to use to control my project. At the moment my idea is to use a switch case construction so that I can switch between three functions I use. However my approach doesn't let me really handle the switch.

For example I would like to send a message to a LCD screen when I switch from one case to another.

switch(mode) {
 case 2:
 Menu2();
 break; 
 case 3:
 Menu1();
 break;
 default:
 ReadTag();
 break;
}

Where mode is determined by the position of the switch.

How would you do this?

I don't see anything wrong with the little bit of code you have posted so I don't understand your question.

Maybe you need to post the whole sketch and/or explain your problem more clearly.

...R

Difficult to post the code as there isn't yet. What I want is a single function to be executed when the mode changes.

I do something similar with the LCD on my custom RC transmitter which has a number of menus. What you are basically doing is building a state machine. Here’s what you need to do:

  1. Have a state variable that indicates which state you are in.
  2. In your main loop(), you need to check for input. What the input should do depends on which state you are in. So use a switch() statement on your state so the input is handled appropriately depending on which state you are in.
  3. After all other processing in your main loop(), render your output to the LCD depending on which state you are in. Again, use a switch statement.

The idea here is you want your main loop to be constantly executing many times per second. You may not want the display to update every iteration of the main loop, so you could set a flag to indicate that it must be updated. Here’s some base code to give you the idea:

int currentstate=0;
int maxstate = 2;
boolean renderflag = true;

void setup()
{
}

void loop()
{
  // step 1:  Process input
  checkForInput();
  
  // Step 2:  Misc processing
  // TODO
  
  // step 3:  Render LCD output
  if (renderflag == true) renderMenus();
}

void checkForInput()
{
  // The following buttons are global and work in all states.
  if (digitalRead(BTN_NEXT) == HIGH)
  {
    currentstate = currentstate + 1;
    if (currentstate > maxstate) currentstate = 0;
    renderflag = true;
  }
  
  if (digitalRead(BTN_PREV) == HIGH)
  {
    currentstate = currentstate - 1;
    if (currentstate < 0) currentstate = maxstate;
    renderflag = true;
  }
  
  // Check for input and react to input based on which state we are in:
  switch (currentstate)
  {
    case 0:
      // Check and process input for this state here
      renderflag = true;
      break;
    
    case 1:
      // Check and process input for this state here
      renderflag = true;
      break;
    
    case 2:
      // Check and process input for this state here
      renderflag = true;
      break;
  }  
  
}

void renderMenus()
{
  switch (currentstate)
  {
    case 0:
      // display this state's info here
      break;
    
    case 1:
      // display this state's info here
      break;
    
    case 2:
      // display this state's info here
      break;
  }
  
  // reset render flag
  renderflag = false;
}

In this example, I’ve decided that I would have a previous and a next button that allows me to cycle through a list of menu options. Pressing the “PREV” button does to the previous option. Pressing the “NEXT” button goes to the next option. The options will wrap around if I get to the first or last option. On my transmitter, for example, I have a volume option that displays a 16 character bar graph indicating the sound volume. Pressing one button lowers the volume and another button increases the volume, which is visually displayed on the bar graph. Pressing the “NEXT” button would go to the next menu option which in this case is SOUND ON/OFF. Now the same 2 buttons used to increase / decrease volume now cycle the sound on & off. Pressing “NEXT” again takes me to the LCD CONTRAST option, which works in a similar fashion to the volume option. In each menu option, the same buttons do different things, all depending on which menu (or state) we are in. That’s where a state machine becomes very handy. Any state can jump to another state (if that’s what you want to allow for). You can even have sub-states. Or whole state trees, like navigating down an automated phone tree. Hope this helps.

Thanks Xpendable, I'm gonna work in this!

Note that your 3-position key switch would always have one position shorted to common at all times except during transfer to another position.

Referring to the diagram, there's 3 inputs to read and you would need to detect when any of the inputs BECOME low. Or, if its wired with common terminal to VCC and others pulled down, you would need to detect when any of the inputs BECOME high.