Using same button for entering/exiting some part of code?


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Using same button for entering/exiting some part of code?

    my experience with button press usage is to have to wait for button release, can either stay in present routine and wait before switching OR moving to other routine and wait for release before looking for new press and going back.

  2. #2
    Join Date
    Feb 2013
    Posts
    1,132


    Did you find this post helpful? Yes | No

    Default Re: Using same button for entering/exiting some part of code?

    Here is the practical code I'm using, but it sometimes behaves strangely.
    Say, button was pressed, action was made once, user still holds button, so no advance in code. But when user releases the button, sometimes it acts as button was pressed once again. Any ideas how to fix it?

    Code:
    MENULOOP: 'MAIN CONFIG SCREEN
    IF UPBT=0 THEN
    MENUITEM=MENUITEM+1
    IF MENUITEM>4 THEN MENUITEM=1
    GOSUB CENTRAL
    pause 200 'debounce
    ENDIF
    WHILE UPBT=0:WEND 
    
    
    IF DNBT=0 THEN
    MENUITEM=MENUITEM-1
    IF MENUITEM<1 THEN MENUITEM=4
    GOSUB CENTRAL
    pause 200 'debounce
    ENDIF
    WHILE DNBT=0:WEND
    if menuitem=1 and rbut=0 then
    pause 5
    while rbut=0: wend
    goto clockconfig
    endif
    GOTO MENULOOP

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Using same button for entering/exiting some part of code?

    There does not seem to be any debounce on button release. Try
    Code:
      IF UPBT=0 THEN
        MENUITEM = MENUITEM+1
    
        IF MENUITEM > 4 THEN MENUITEM = 1
        GOSUB CENTRAL 
        ' PAUSE 20          ' May be needed if subroutine CENTRAL executes very fast.
        WHILE UPBT = 0 : WEND
        PAUSE 20 'debounce
      ENDIF

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Using same button for entering/exiting some part of code?

    read switch a few times to be sure and debounce......

    IF UPBT=0 THEN
    PAUSE 20 ' or 40 or 50
    IF UPBT=0 THEN 'go ahead........

    MENUITEM = MENUITEM+1

    do same for release check .... if upbt=1 then.....
    Last edited by amgen; - 21st April 2023 at 15:26.

  5. #5
    Join Date
    Feb 2013
    Posts
    1,132


    Did you find this post helpful? Yes | No

    Default Re: Using same button for entering/exiting some part of code?

    For couple of buttons this code works fine, but when I increase number of button handling operations, some weird things occur - the further from beginning is the button handling routine, more rarely it responds to user.

    The code below has 8 "blocks" of button handling.

    first 3 work fine, 4th one works only maybe on 10 or 15th press. 5th and further - do not work at all.
    And this is not issue of particular code - if I move say "5th block" to 1st place of this code, then it works fine.

    PIC18F45K80 @64mhz.

    Code:
    
    IF UPBT=0 THEN
    MENUITEM=MENUITEM+1
    IF MENUITEM>6 THEN MENUITEM=1
    pause 200
    ENDIF
    WHILE UPBT=0:WEND 
    
    
    IF DNBT=0 THEN
    MENUITEM=MENUITEM-1
    IF MENUITEM<1 THEN MENUITEM=6
    pause 200
    ENDIF
    WHILE DNBT=0:WEND
    
    
    
    
    IF LBUT=0 AND MENUITEM=2 THEN  
    RICXVI=RICXVI+1
    IF RICXVI>31 THEN RICXVI=1
    pause 200
    GOSUB SETTIME
    ENDIF
    WHILE LBUT=0:WEND 
    
    
    IF RBUT=0 AND MENUITEM=2 THEN  
    RICXVI=RICXVI-1
    IF RICXVI<1 THEN RICXVI=31
    pause 200
    GOSUB SETTIME
    ENDIF
    WHILE RBUT=0:WEND 
    
    
    
    
    IF LBUT=0 AND MENUITEM=1 THEN  
    TVE=TVE+1
    IF TVE>13 THEN TVE=1
    pause 200
    GOSUB SETTIME
    ENDIF
    WHILE LBUT=0:WEND 
    
    
    IF RBUT=0 AND MENUITEM=1 THEN  
    TVE=TVE-1
    IF TVE<1 THEN TVE=13
    pause 200
    GOSUB SETTIME
    ENDIF
    WHILE RBUT=0:WEND 
    
    
    
    
    
    
    IF LBUT=0 AND MENUITEM=3 THEN  
    DGE=DGE+1
    IF DGE>7 THEN DGE=1
    pause 200
    GOSUB SETTIME
    ENDIF
    WHILE LBUT=0:WEND 
    
    
    IF RBUT=0 AND MENUITEM=3 THEN  
    DGE=DGE-1
    IF DGE<1 THEN DGE=7
    pause 200
    GOSUB SETTIME
    ENDIF
    WHILE RBUT=0:WEND

  6. #6
    Join Date
    Feb 2013
    Posts
    1,132


    Did you find this post helpful? Yes | No

    Default Re: Using same button for entering/exiting some part of code?

    I tried to reduce PAUSE 200 to 20 or even 2 - no change.

  7. #7
    Join Date
    Aug 2011
    Posts
    455


    Did you find this post helpful? Yes | No

    Default Re: Using same button for entering/exiting some part of code?

    what about something like this instead?
    Code:
    IF UPBT=0 THEN
        MENUITEM=MENUITEM+1
        IF MENUITEM>6 THEN MENUITEM=1
        PAUSE 20
        WHILE UPBT=0:WEND 
        PAUSE 20
    ENDIF
    
    IF DNBT=0 THEN
        MENUITEM=MENUITEM-1
        IF MENUITEM<1 THEN MENUITEM=6
        PAUSE 20
        WHILE DNBT=0:WEND
        PAUSE 20
    ENDIF
    
    IF LBUT=0 THEN
        IF MENUITEM=1 THEN  
            TVE=TVE+1
            IF TVE>13 THEN TVE=1
            GOSUB SETTIME
        ENDIF
        IF MENUITEM=2 THEN  
            RICXVI=RICXVI+1
            IF RICXVI>31 THEN RICXVI=1
            GOSUB SETTIME
        ENDIF
        IF MENUITEM=3 THEN  
            DGE=DGE+1
            IF DGE>7 THEN DGE=1
            GOSUB SETTIME
        ENDIF
        
        PAUSE 20
        WHILE LBUT=0:WEND 
        PAUSE 20
    ENDIF
    
    IF RBUT=0 THEN
        IF MENUITEM=1 THEN  
            TVE=TVE-1
            IF TVE<1 THEN TVE=13
            GOSUB SETTIME
        ENDIF
        IF MENUITEM=2 THEN  
            RICXVI=RICXVI-1
            IF RICXVI<1 THEN RICXVI=31
            GOSUB SETTIME
        ENDIF
        IF MENUITEM=3 THEN  
            DGE=DGE-1
            IF DGE<1 THEN DGE=7
            GOSUB SETTIME
        ENDIF
        
        PAUSE 20
        WHILE RBUT=0:WEND 
        PAUSE 20
    ENDIF
    it could be simplified even more, but...

Similar Threads

  1. Final Button Code
    By WarPony in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 22nd May 2008, 14:14
  2. Help with code, button
    By xobx in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 15th July 2007, 17:52
  3. Entering the number from keypad...
    By turkuaz in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 27th April 2007, 09:02
  4. Code entering endless loop
    By Blackhawk in forum mel PIC BASIC
    Replies: 11
    Last Post: - 26th November 2006, 09:12
  5. Code check -- button not working
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 2nd March 2006, 22:43

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