Button command question


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2009
    Posts
    13

    Default Button command question

    I am using the 5 button commands to handle 5 push buttons in my application. No auto repeat is used, just a simple if pressed… then do… My software checks on each button in order or importance, and once it finds that one is pressed, it jumps out of the 5 button sequence and performs the necessary task. All this works very well.
    Now my client requests that when 2 buttons are pressed simultaneously, something else is done. Can any one point me in the right direction to check for two of them pressed at once?

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Hello aherrera,
    Ok suppose you are using PortB as follows
    'PortB.0 = switch 1
    'PortB.1 = switch 2
    'PortB.2 = switch 3
    'PortB.3 = switch 4
    'PortB.4 = switch 5
    and you are doing If PortB.0 = 1 then . . .
    the port has a binary value when you push a switch, port B.0 = 1, portB.1 = 2, both = 3
    so you could use a select case statement or a lookup table to direct your code flow.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Jul 2009
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    I am not checking every pin; I am using the button command so I can de-bounce the switches.

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by aherrera View Post
    I am not checking every pin; I am using the button command so I can de-bounce the switches.
    Ok button command is overrated and too complicated.
    i am not home and do not have the manual with me so bear with me if I error.
    Code:
    myVar var byte[5]
    myVar.0 = portB.0
    myVar.1 = portB.1
    myvar.2 = portB.2
    myvar.3 = portB.3
    myvar.4 = portB.4 
    
    check:
    if myVar != 0 then pause 50
    if myVar != 0 then
    
    select case myvar 
    case 0 goto check
    case1  ' button1 pushed
    do something . . . fish
    case2   ' button2 pushed
    do something . . .cut bait
    case3 ' 2 buttons pushed
    do another something . . . look for better lake
    case 4 ' button3 pushed
    . . . 
    case 7 ' 3 buttons pushed
    do or not do you decide
    
    case else
     goto check
    end select
    endif
    else 
    goto check
    some of the syntax may be incorrect above but it should steer you to what you are wanting
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    Jul 2009
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    Thank you for your help, so, you think is over rated, I see you are de-bouncing by pausing, but this will cause the software to repeat the press if someone holds the button pressed, the button command takes care of all this.

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Another idea.. sort of.

    I do not think it is likely that two buttons will be pressed at the same time. So if the buttons are on the same port then read the port twice to see if the second one has been pushed.

    Untested but something like
    Code:
    CHECK_BUTS:
    BUTVAR = PORTB & $1F 
    IF BUTVAR >= 1 THEN CHECK2
    GOTO CHECK_BUTS
    
    CHECK2
    PAUSE 50
    BUTVAR = PORTB & $1F
    SELECT CASE BUTVAR
    'insert case statements
    GOTO CHECK_BUTS
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink BUTTON is really fine ...

    Hi,

    BUTTON Command could be fine here ... let's suppose button tie pin to ground.

    Code:
    SCAN:
    
    FOR I = 0 to 4 
    
       BVar = 0
    
       BUTTON PORTB.0[I],0,255,0,BVar,1,Action
    
    NEXT I
    
    I =  255 ' just to allow CASE 0 ... 
    
    Action :
    
    SELECT CASE I
    
        CASE 0 ...
    
    ...
    
        CASE ELSE  GOTO SCAN
    
    END SELECT
    As the debouncing do not occur if button is not pressed ( Mr de la Pallice )... scanning is very fast !

    CQFD

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink With 2 buttons now ...

    For twin Button press ...

    just add another scan loop ...

    Code:
    
    SCAN:
    
    RESULT = 0 
    
    FOR I = 0 to 4 
    
       BVar = 0
    
       BUTTON PORTB.0[I],0,255,0,BVar,1,Action
    
    NEXT I
    
    GOTO SCAN                                             ' No need to go further ...
    
    Action :
    
          RESULT.I = 1                                     ' Store the first button pushed
          
          PAUSE 250                                         ' Let a little time for pushing the
                                                                  ' next button ...
          FOR J= 0 to 4
     
          IF J = I THEN I = I+1                            ' Button already pushed !!! ... Skip test.
      
          BVar = 0
    
         BUTTON PORTB.0[I],0,255,0,BVar,1,Action2
    
         NEXT J
    
         GOTO Action1                                       ' Only one button Pushed  ... 
    
         
    Action2:
    
    RESULT.J = 1                                              ' Store the second button pushed
    
    Action1:
    
    SELECT CASE RESULT
    
        CASE %00000001                      ' Button 0 alone
        ...
        CASE %00000010                      ' Button 1 Alone
        ...
        CASE %00000100                      ' Button 2 Alone
        ...
        ...
        ...
        CASE %00000011                      ' Buttons 0 and 1 ... or  1 and 0
        ...
        CASE %00000101                      ' Buttons 0 and 2 ... or  2 and 0
        ...
        ...
        ...
        CASE %00000110                      ' Buttons 1 and 2 ... or  2 and 1
        ...
        CASE %00001010                      ' Buttons 1 and 3 ... or  3 and 1
        ...
        ...
        ...
        CASE %00001100                     ' Buttons 2 and 3 ... or  3 and 2
        ...
        CASE %00010100                     ' Buttons 2 and 4 ... or  4 and 2
       
        CASE ELSE  GOTO SCAN
    
    END SELECT
    with 16 bits for RESULT, RESULT.Highbyte for I, RESULT.Lowbyte for J, .... you can even check in which order the buttons have been pushed ...

    Too much ??? Ok, I take it back to my kangaroo pocket ...

    Alain
    Last edited by Acetronics2; - 30th August 2009 at 13:38.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  9. #9
    Join Date
    Jul 2009
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    I want to thank everybody for their great help. I think I have enough to get me going here...

  10. #10
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Exclamation

    Arrrgh !!!

    a little typing bug :

    Code:
       IF J = I THEN I = I+1                            ' Button already pushed !!! ... Skip
    should have been

    Code:
       IF J = I THEN J = J+1                            ' Button already pushed !!! ... Skip
    as everybody did notice ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Similar Threads

  1. Sony SIRC IR Issue
    By Ryan7777 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th August 2015, 08:10
  2. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43
  3. Code check -- button not working
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 2nd March 2006, 22:43
  4. Button Push within 3 second Window
    By Tissy in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd December 2005, 10:06
  5. Button subfunction 16F628
    By Jųan in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 19th August 2005, 16:44

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts