Shop OBEX P1 Docs P2 Docs Learn Events
Button short or long press — Parallax Forums

Button short or long press

cazzillopocazzillopo Posts: 2
edited 2015-01-30 13:05 in BASIC Stamp
Hi, I have a button on pin 0 that need to have the following behavior: When short press (0,2 second) it must set out 8 high
When long press (1 or more second) it must set out 9 high
I can not solve this problem
Someone have a solution ?
Thank You very mutch.

Comments

  • JonnyMacJonnyMac Posts: 9,014
    edited 2014-11-29 13:46
    You might do something like this:
    Get_Button:
      btimer = 0
      IF (TrButton = 0) THEN Get_Button
      DO WHILE (TrButton = 1)
        PAUSE 100
        btimer = btimer + 1
      LOOP
      RETURN
    


    The value in btimer will tell you how long the button was held -- you can dictate what logic to apply from there. Note that this subroutine blocks the program until the button is pressed.
  • GenetixGenetix Posts: 1,749
    edited 2014-11-29 14:02
  • cazzillopocazzillopo Posts: 2
    edited 2014-11-30 11:36
    Thank you both.
    I'll try to inplement your suggestion in my project.
  • mlerleymlerley Posts: 2
    edited 2015-01-30 13:05
    Thanks for this! I modified it a bit so that it can be used in a non-blocking fashion inside of a spinning main loop. pReadButton is a pin that goes low when pressed so we'll only enter the loop here if the button is pressed. The clicks are counted; I did a 250ms pause as it seems natural for the type of button I have; YMMV. The select loop below allows for granular control of button push length, although press length accuracy here won't be high. I was able to easily trigger 1, 2 and 3 second presses.
    Main:
      ' stuff happens...
    
      B=0
      DO WHILE pReadButton = 0
        PAUSE 250
        B=B+1
      LOOP
    
      SELECT B
        CASE 1
          ' quick press (< 250ms)
        CASE >19
          ' 5 seconds
        CASE >3
          ' one second
      ENDSELECT
    
      ' other stuff happens...
    
    goto Main
    
Sign In or Register to comment.