Hi,
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
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.
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
Code:
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 ");
Serial.println(keypad.key[i].kchar);
Serial.println(keypad.key[i].kstate);
if (keypad.key[i].kchar == 'h' && analogRead(20) == HIGH)
analogWrite(20, 200);
else
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.