PDA

View Full Version : Pushbutton code routine suggestions?



jessey
- 31st August 2005, 04:34
Hello

Lately I've been playing around with some push buttons to create code that will increase and decrease a word sized variable from 0 to 65535 and to do it fairly quickly while maintaining some kind of reasonable control and visual effect. I did manage to write some code that seems to do the job ok but it uses close to 600 words!!! It still needs a little tweaking but otherwise works great. Would anyone here have any routines or suggestions that would accomplish or complement this, that would possibly use less code space? I've included my code as an attachment below. I've tried to implemented a variable pause that changes (speeds-up) while the push button is held down and then, when the button is released (when you get close to the number you want) and re-pressed, then the number up or down will be slowed down. I checked the archives but couldn't find anything that was related to what I'm trying to do here. Any suggestions or comments will be appreciated.

Thanks
jessey

sougata
- 2nd September 2005, 06:30
Dear Jessey,

You can do the job more easily using the button function. It handles the debounce , and has an autorepeat feature. Autorepeat delay , and rate are also programmable. You need to create a variable for the button and also make sure that the routine runs in a loop.

Here is a code example:

'************************************************* ******************************
SPEEDSET:

B1 = 0 : B2 = 0 : LED1 = 0 : LED2 = 0 : LED3 = 0 ' TURN OFF ALL LEDS
LCDOUT $FE, 1 ' CLEAR THE LCD
LCDOUT "SET SERVO SPEED" ' SHOW THE SUB-MENU ITEM
PAUSE 2000 ' CHECK FOR VALID SPEED
IF SPEED > 127 THEN SPEED = 0
SPEED_PRESET_LOOP: ' PRESET LOOP
LCDOUT $FE, $C0,"TRACKSPEED=",DEC SPEED," "
PAUSE 1 ' SHORT DELAY TO REDUCE LCD FLICKER
IF HALT = 0 THEN QUIT ' CHECK IF STOP BUTTON HIT THEN QUIT TO MAIN MENU
BUTTON INCR, 0, 200, 30, B1, 1, SPEED_PLUS ' IF THE INCREMENT BUTTON IS PRESSED (PULLED-UP)
BUTTON DECR, 0, 200, 30, B2, 1, SPEED_MINUS ' IF THE DECREMENT BUTTON IS PRESSED (PULLED-UP)
GOTO SPEED_PRESET_LOOP

SPEED_PLUS: ' ROUTINE TO INCREASE THE SPEED VARIABLE
SPEED = SPEED + 1
IF SPEED > 127 THEN SPEED = 0
GOTO SPEED_PRESET_LOOP

SPEED_MINUS: ' ROUTINE TO DECREASE THE SPEED VARIABLE
SPEED = SPEED - 1
IF SPEED > 127 THEN SPEED = 0
GOTO SPEED_PRESET_LOOP

'************************************************* ******************************

The speedset routine is practically a sub-menu in my application and is entered from a menu choice. B1 and B2 are button variables (byte). Speed is the variable that is modified through the buttons. INCR is an alias for the increment button normally high (pulled- up) and DECR is an alias for the decrement button. Halt is an alias for the Stop button (that lets one quit from the sub-menu or the main-menu)

Regards
Sougata

jessey
- 3rd September 2005, 01:02
Hi Sougata

Thanks for the suggestion and the sample code, I'll give it a try and see what I can accomplish using the Button command.

Thanks Again
jessey