Increment when a button is pressed?
Vaati
Posts: 712
I need to increment a variable once for each change of a boolean variable from false to true. Is this possible?
The alternative way I can write it is that I need to increment a value every time a button is pressed, but the following code increments it until the button is released:
Thanks.
The alternative way I can write it is that I need to increment a value every time a button is pressed, but the following code increments it until the button is released:
if(ui.btnup) my_var++ if(ui.btndn) my_var--FYI, I'm using the jm_lcd_ui object, since I have the GG Prop Platform + LCD module.
Thanks.
Comments
When your code checks the state of the boolean variable, use intermediate variables to compare the last state of your boolean variable and increment your counter variable accordingly.
Or maybe: using the CASE instead. I'll try an example and post.
Paul
Correct me if I'm wrong, but that would continue to increment until newValue becomes false, correct? I need to increment only once per change from false to true.
Basically, when the up button is pressed on the module I'm using, ui.btnup becomes true. I need to increment a value each time that up button is pressed, yet my code in my first post increments it until the button is released.
Of course this code is only good if newval is already debounced. If not there might appear more counts due to the bouncing. Easy way of adding debounce is to wait for some ms (say 20) inside of the if oldval<>newval.
I probably should reword my question -- how do I increment something each time a button is pressed, and only once per press?
I suggest an edit, for someone who might find this thread later -- your code should be
instead of how it is now. ;-)
http://www.maxim-ic.com/app-notes/index.mvp/id/287