Shop OBEX P1 Docs P2 Docs Learn Events
altering behavior when a button is depressed for longer than n seconds — Parallax Forums

altering behavior when a button is depressed for longer than n seconds

DylanBDylanB Posts: 24
edited 2006-03-15 17:04 in BASIC Stamp
Hi,

I have a simple routine for incrementing/decrementing a variable based on button states:
Get_Buttons:
  Btns = %1111                                  ' enable all four inputs
  FOR BtnIdx = 1 TO 5
    Btns = Btns & ~BtnBus                       ' test inputs
    PAUSE 5                                     ' delay between tests
  NEXT

  'check for a mode switch  (switch 0)
  IF Btns.BIT0 = 1 THEN
    motor_select = motor_select + 1 // 3      ' update motor_select    constrain  to 0-2
  ENDIF

 'check for an increase in dt (switch 1)
  IF Btns.BIT1 = 1 THEN
   dt(motor_select) = dt(motor_select) + 1   'it would be nice to increment this by 10 sometimes...
  ENDIF

 'check for a decrease in dt (switch 2)
  IF Btns.BIT2 = 1 THEN
   dt(motor_select) = dt(motor_select) - 1     'it would be nice to decrement this by 10 sometimes...
  ENDIF

 'check for motor enable/disable  (switch 3)
  IF Btns.BIT3 = 1 THEN
    motorEnable(motor_select) = motorEnable(motor_select) + 1 //2      'constrain to 0-1
    'start the clock for this motor
    idx = motor_select
    GOSUB Set_Time
  ENDIF

RETURN




I would like to increment a value by 10 instead of 1 if a button state remains the same for a couple seconds... kind of like how many alarm clocks work.

Any tips on how i could do this ?

Thanks!

Dylan

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-10 14:51
    If you're reading the the button(s) at a specific rate you can ad a counter that controls the increment rate.· Let's say you're scanning the button every 100 ms and after 1 second you want to change the increment rate.· You can do something like this:

    · rate1 = rate1 + 1 * btns.BIT1
    · IF (rate1 > 10) THEN
    ··· dt(motor_select)·=·dt(motor_select)·+·10
    · ELSE
    ··· dt(motor_select)·=·dt(motor_select)·+·1
    · ENDIF

    Note that the first line of code will either add one to rate, or clear it based on the return value.· You need to be a little careful with this as rate could roll-over from its max value to zero if the variable size is too small for the amount of time the button could be pressed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-03-10 15:47
    You could also put a MAX 11 after the line to limit it from ever rolling over, and then you could use a NIB variable.
      rate1 = rate1 + 1 * btns.BIT1 MAX 11
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-10 21:08
    Thanks, Chris, I don't often use the MAX operator and that's a perfect application.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • DylanBDylanB Posts: 24
    edited 2006-03-15 05:29
    Thanks Joe and Chris for the great pointers. I was able to cobble together a solution like this:

    'check for an increase in dt (switch 1)
      IF Btns.BIT1 = 1 THEN
       dt(motor_select) = dt(motor_select) + i
       i = i + 1   MAX 11        'constrain to NIB size
       IF(dt(motor_select) < 2 ) THEN
          dt(motor_select) = 2
          i = 1
       ENDIF
      ELSEIF Btns.BIT1 = 0 THEN
       i = 1
      ENDIF
    
     'check for a decrease in dt (switch 2)
      IF Btns.BIT2 = 1 THEN
       dt(motor_select) = dt(motor_select) - d
       d = d + 1 MAX 11          'constrain to NIB size
       IF(dt(motor_select) < 2 ) THEN
         dt(motor_select) = 2
         d = 1
       ENDIF
      ELSEIF  Btns.BIT2 = 0 THEN
       d = 1
      ENDIF
    
    



    note the hack used to prevent the dt variable from going below 2 ... would a MIN 2 fix this ... :

    'wonder if this would prevent dt(motor_select) from going below 2
    dt(motor_select) = dt(motor_select) - d MIN 2
    
    



    Any ideas?

    Thanks again,

    Dylan
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-15 13:19
    Using MIN in your hack will prevent dt(motor_select) from falling below 2, yes.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-03-15 17:04
    You can replace this:
    IF Btns.BIT1 = 1 THEN
       dt(motor_select) = dt(motor_select) + i
       i = i + 1   MAX 11        'constrain to NIB size
       IF(dt(motor_select) < 2 ) THEN
          dt(motor_select) = 2
          i = 1
       ENDIF
      ELSEIF Btns.BIT1 = 0 THEN
       i = 1
      ENDIF
    


    with this
    dt(motor_select) = dt(motor_select) + i MIN 2 MAX 300
    i = (i + btns.bit1) * btns.bit1 MAX 11 MIN 1
    
    



    Similarly for the second code block. That does not include setting i=1 when motor_select<2, I don't quite see what purpose that serves.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.