Shop OBEX P1 Docs P2 Docs Learn Events
if...then — Parallax Forums

if...then

TumblerTumbler Posts: 323
edited 2012-08-19 10:29 in Propeller 1
Hi,

I'm having a problem with this code:
i have a button and want to couple the press time to a state.
state 0 : unpressed
state 1 : pressed (less than 1 sec)
state 2: pressed more than 1 sec and less then 2 sec
state 3: pressed more than 2 sec

I have tried different codes, but i never can detect state 3

This is what i have now
PUB Main | time ,state,oldstate  debug.Start(115_200)


  repeat
    if state<>oldstate
      debug.dec(state)
      debug.str(string(" ",cr))
      
    if ina[IN1]==1
      state :=0                 'button unpressed
      
    if state == 0
      time := button.ChkBtnHoldTime(IN1, 0, 60,2000)
      if time
        if time < 800
          state:=1              'normal press          
        if time >= 800
          
          if time >= 1500       'xtra long press
            state:=3
          else
            state:=2            'long press
            


Comments

  • kuronekokuroneko Posts: 3,623
    edited 2012-08-19 00:25
    You want => instead of >=, it's a SPIN thing.
  • TumblerTumbler Posts: 323
    edited 2012-08-19 00:26
    Thx m8! :)
  • JonnyMacJonnyMac Posts: 9,108
    edited 2012-08-19 09:03
    A case statement might make your code easier to follow and maintain. The variable ms is the duration (in milliseconds) the button was pressed.
    case ms
        0          : state := 0
        1..999     : state := 1
        1000..1999 : state := 2
        other      : state := 3
    


    If you prefer if-then, this is another way that is clean
    if (ms == 0)
        state := 0
      elseif ((ms => 1) and (ms =< 999))
        state := 1
      elseif ((ms => 1000) and (ms =< 1999))     
        state := 2
      else
        state := 3
    
  • TumblerTumbler Posts: 323
    edited 2012-08-19 10:29
    I tried that one Jon (without an h)
    But mine was not working because i used >= instead of =>

    And thx for the 'case', didn't tought about that one.
Sign In or Register to comment.