Shop OBEX P1 Docs P2 Docs Learn Events
button question — Parallax Forums

button question

NickNick Posts: 11
edited 2005-05-01 14:53 in BASIC Stamp
a while back i saw a post that explained how to have a single button do multiple things ie. the first time you press it it did x. the second it did y, and the third it did z. anyone seen this post, or know how to do it?

I have a keypad that is interfaced to the stamp via serial. and i would like to be able to 'scroll' through commands with one button on the keypad.

Thanks

Comments

  • KenMKenM Posts: 657
    edited 2005-05-01 14:53
    I am going to make a suggestion on how to do what you want. However, I am quite sure there is a more elegant way to accomplish your task.
    Assuming you need no more than 16 tasks based on the next button press....
    1. You will need to de-bounce the button contact. There is plenty of documentation on how to do that
    2. Set up a nibble variable to act as a counter
    Before the button is pressed the first time,·initialize your counter
    Each time the button is pressed, increment the counter by one
    Depending on the counter value, call the subroutine that you want to occur.
    After the last event occurs,·re-initialize your counter
    EX;
    You want to do 4 subroutines, call them sub1, sub2, sub3, sub4 (but you will use more usefull names).

    The example below is by no means complete, but I hope you see the big picture to help you get started.
    buttonmem var nib
     
    buttonmem = 1
     
     
     
    again:
     
     
       SELECT buttonmem
        CASE 1                         'check for 1st button press
         buttonmem = buttonmem + 1     'increment the value of buttonmem by one
         GOSUB sub1                    'call "first" subroutine in sequence
     
        CASE 2                          'check for 2nd button press
         buttonmem = buttonmem + 1      'increment the value of buttonmem by one
         GOSUB sub2                     'call "second" subroutine
     
        CASE 3                          'check for 3rd button press
         buttonmem = buttonmem + 1      'increment the value of buttonmem by one
         GOSUB sub3                     'call "third" subroutine
     
        CASE 3                          'check for last button press
         buttonmem = 1                  'increment the value of buttonmem by one
         GOSUB sub4                     'call "last" subroutine in sequence
     
       ENDSELECT
     
    GOTO again
    

    Post Edited (KenM) : 5/1/2005 2:59:49 PM GMT
Sign In or Register to comment.