Shop OBEX P1 Docs P2 Docs Learn Events
Increment when a button is pressed? — Parallax Forums

Increment when a button is pressed?

VaatiVaati Posts: 712
edited 2011-05-15 15:47 in Propeller 1
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:
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

  • Mike GreenMike Green Posts: 23,101
    edited 2011-05-15 09:33
    When you talk about a change in a Boolean value, you're really talking about two Boolean values, one is the current value of the Boolean and the other is the previous value of the Boolean:
    if newValue and not oldValue
       counter++    ' The oldValue was false and the newValue is true
    oldValue := newValue   ' Update oldValue
    
  • doggiedocdoggiedoc Posts: 2,245
    edited 2011-05-15 09:40
    Vaati,

    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
  • doggiedocdoggiedoc Posts: 2,245
    edited 2011-05-15 09:41
    Dang, Mike Green you are fast.

    :D
  • doggiedocdoggiedoc Posts: 2,245
    edited 2011-05-15 09:46
    Vaati - do you need to count each transition of the Boolean value or just when it goes from False to True?
  • VaatiVaati Posts: 712
    edited 2011-05-15 09:47
    Mike Green,

    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.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-05-15 10:00
      if oldval <> newval
        oldval:=newval
        if newval
          count++
    

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-05-15 10:08
    @Vaati, Nope. The counter will be increment only when the "new" value is true and the "old" value is false. The next time through, the "old" value will be true and the counter will not increment. This is standard software positive edge detection. The counter will increment once only for each button push or click. The button has to be released, then pushed again for the count to increment.
  • VaatiVaati Posts: 712
    edited 2011-05-15 10:34
    Ok, I see what you mean -- however, it isn't working in this case.

    I probably should reword my question -- how do I increment something each time a button is pressed, and only once per press?
  • Mike GreenMike Green Posts: 23,101
    edited 2011-05-15 10:58
    Either what I posted or what MagIO2 posted will do what you want. Initialize oldBtnup and oldBtndn to false, then
    newBtnup := ui.btnup
    if newBtnup and not oldBtnup
      my_var++
    oldBtnup := newBtnup
    newBtndn := ui.btndn
    if newBtndn and not newBtndn
      my_var--
    oldBtndn := newBtndn
    
  • VaatiVaati Posts: 712
    edited 2011-05-15 11:10
    Oh, ok -- thanks!

    I suggest an edit, for someone who might find this thread later -- your code should be
    if newBtndn and not oldBtndn
    
    
    instead of how it is now. ;-)
  • tonyp12tonyp12 Posts: 1,951
    edited 2011-05-15 15:47
    Gone need software debounce, unless you already using hardware debouce.
    http://www.maxim-ic.com/app-notes/index.mvp/id/287
Sign In or Register to comment.