altering behavior when a button is depressed for longer than n seconds
DylanB
Posts: 24
Hi,
I have a simple routine for incrementing/decrementing a variable based on button states:
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
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
· 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 Savage
Parallax Tech Support
csavage@parallax.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
note the hack used to prevent the dt variable from going below 2 ... would a MIN 2 fix this ... :
Any ideas?
Thanks again,
Dylan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
with this
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