I am experimenting with a servo and I want to use a momentary switch to select whether the servo turns CCW, turns to center, turns CW or is off. The momentary switch will be connected to an input pin for which the internal pull-up will be enabled; and when the momentary switch is pressed, the pin will be pulled low; when the pin is released the pin will go high again (as a result of the internal pull-up). Each time the momentary switch is pressed the code executed will change as follows:
- off -> CCW -> center -> CW -> off
If I am understanding danni's code correctly, as written it is not intended to operate with a momentary switch, but instead with an "on/off" switch. Specifically, danni's code "remembers" whether a pin is high or low. So if a high pin is pressed and pulled down, after the requisite debounce interval the LED is toggled and the low state will be "remembered" as the new current state. If that pin subsequently pulled high, after the requisite debounce interval the LED is toggled again and the high state will be "remembered" as the new current state.
In the case of the momentary switch, I want the debounce routine to confirm only a high -> low change in state, not a low -> high change instate. On other words, I want high to always be the current state. So, it seems to me that in order adapt danni's code for a momentary switch, I should clear "key-state" after the debounce routine confirms that the button has been pressed; so that as a default "key-state" is high. If I read and understand pop_gheorghe's code (which seems to be based on the danni's code) correctly, I think that that is what his code is intended to do.
Does my understanding seem correct? Thanks.