if...then
Tumbler
Posts: 323
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
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
case ms 0 : state := 0 1..999 : state := 1 1000..1999 : state := 2 other : state := 3If 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 := 3But mine was not working because i used >= instead of =>
And thx for the 'case', didn't tought about that one.