Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: keypad library: how to differentiate between momentary switch and toggle switch

  1. #1

    keypad library: how to differentiate between momentary switch and toggle switch

    I just started to use the keypad library; and I am able to get signals from various controls. I am looking at the state changes to handle the signal accordingly.

    At the moment I have a momentary button and a toggle switch; and I am not able to figure out how to get the difference between the 2

    My code at the moment, look at the getKeys() function; then parse the matrix of keys to see which one has a stateChanged. I have a LED on pin 20 that I turn on when I press the button, to get a feel of the control responsiveness

     if (keypad.getKeys())
        for (int i=0; i<LIST_MAX; i++)   // Scan the whole key list.
          if ( keypad.key[i].stateChanged )   // Only find keys that have changed state.
            Serial.print("Pressed the key ");
            if (keypad.key[i].kchar == 'h' && analogRead(20) == HIGH)
              analogWrite(20, 200);
              analogWrite(20, 0);
    the momentary button is connected to pin 2 and pin 7; while the toggle switch is connected to pin 2 and pin 8.

    When I press the button; I get 3 states: 1 that is pressed, then 3 that is released and finally 4 that is hold. This makes my LED flicker, since it goes through 3 states
    When I use the toggle; I get again 1 for pressed, but then it stay on 2, which is hold. Then goes through 3 and 4 as the momentary switch.

    Since both controls go through the same 1, 3 and 4 state; I am having issues to find a way to differentiate between them; so if I press the button, the LED just flicker once, when I press and release the button (so state 1 and state 3 and 4); while for the switch I can have the LED stay on as long as the state is 2, and then turn off when it goes through state 3 and 4.

    Is there an easy workaround to make a distinction between the momentary switch and the toggle switch; using the states of the key, or am I on the wrong route? I may use multiple IF statements, but if I have a lot of toggles and momentary buttons, this is not manageable.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Matrix connection of switches has a well known problem called phantom keys. Here's a page with excellent info.

    Normally isn't not a big problem if all the switches are momentary contacts and the user generally presses them one at a time. Toggle or on-off type switches are usually not used with matrix connections.

    The solution (while still using the matrix approach) involves adding a diode in series with every switch. It's explained really well at that lengthy page. If you want to make this work reliably with toggle switches, that's probably what you'll need to do.

    Or you might just use dedicated wires for the toggle switches and keep the matrix only for momentary switches, which is the conventional way matrix wiring is used.

  3. #3
    Thanks Paul; I do get a picture of the situation now. Will give a try with the diode and see how it goes.

    BTW, is there a way to remember the state of a keypad button pressed? In this way I can check 2 states and if it goes through pressed and hold, I know it is a toggle.

    While looping through the buttons, I am not getting any unique ID from the button; so I may just use the char used to map the matrix, and constantly check against these to see if they move into hold state; although I hope it won't slow down the interaction.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts