Parallax Forums
  HomeLog InRegisterCommunity CalendarSearch the ForumHelp
   
Parallax Forums > Public Forums > BASIC Stamp > altering behavior when a button is depressed for longer than n seconds  Forum Quick Jump
 
New Topic Post Reply Printable Version
[ << Previous Thread | Next Thread >> ] | Show Newest Post First ]

DylanB
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Feb 2006
Total Posts : 24
 
   Posted 3/10/2006 12:38 AM (GMT -8)    Quote This PostAlert An Admin About This Post.
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
Back to Top
 

Jon Williams
Registered Member



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2004
Total Posts : 6440
 
   Posted 3/10/2006 6:51 AM (GMT -8)    Quote This PostAlert An Admin About This Post.

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

Back to Top
 

Chris Savage (Parallax)
Forum Moderator



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2004
Total Posts : 10410
 
   Posted 3/10/2006 7:47 AM (GMT -8)    Quote This PostAlert An Admin About This Post.
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
 

Back to Top
 

Jon Williams
Registered Member



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2004
Total Posts : 6440
 
   Posted 3/10/2006 1:08 PM (GMT -8)    Quote This PostAlert An Admin About This Post.
Thanks, Chris, I don't often use the MAX operator and that's a perfect application.


Jon Williams
Applications Engineer, Parallax

Back to Top
 

DylanB
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Feb 2006
Total Posts : 24
 
   Posted 3/14/2006 9:29 PM (GMT -8)    Quote This PostAlert An Admin About This Post.
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
Back to Top
 

Jon Williams
Registered Member



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2004
Total Posts : 6440
 
   Posted 3/15/2006 5:19 AM (GMT -8)    Quote This PostAlert An Admin About This Post.
Using MIN in your hack will prevent dt(motor_select) from falling below 2, yes.


Jon Williams
Applications Engineer, Parallax

Back to Top
 

Tracy Allen
Registered Member



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2004
Total Posts : 2845
 
   Posted 3/15/2006 9:04 AM (GMT -8)    Quote This PostAlert An Admin About This Post.
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

Back to Top
 
[ << Previous Thread | Next Thread >> ]
New Topic Post Reply Printable Version
 
Forum Information
Currently it is Friday, November 20, 2009 6:00 PM (GMT -8)
There are a total of 393,697 posts in 55,520 threads.
In the last 3 days there were 88 new threads and 710 reply posts. View Active Threads
Who's Online
This forum has 17684 registered members. Please welcome our newest member, Dogg.
46 Guest(s), 10 Registered Member(s) are currently online.  Details
boeboy, Peter Jakacki, Phil Pilgrim (PhiPi), jazzed, Michael O'Brien, Cluso99, localroger, adranus, potatohead, pharseid