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


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,124

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

    Hello. This might sound simple, but I can't find the reliable solution.

    Say I have button, input pin, which is tied to VDD via 10K resistor.
    Pressing button pulls input pin down.

    There is in some code, say code1, which runs in loop.
    When user press button, execution goes to code2, which also runs in loop.
    While in loop2, when user presses the button, execution is returned to code1

    The issue is, if user keeps button pressed, the code will jump from code1 to code2 and back from code2 to code1.

    To avoid this, the button state should be checked, so when user presses the button while in code1, jump to code2 will not occur until he releases the button.

    I tried to use BUTTON statement, but it is not handy, since it only can do jump to label, and sometimes I just want to change value of some variable. And sometimes buttons are tied to ADC, so different button returns different ADC value.

    Is there a simple way for fixing the issue I have?

  2. #2


    Did you find this post helpful? Yes | No

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

    I made a multi-function timer that deals with that and does different things according to how long sw is pressed ( and improved it to a touch sw using a/d)...... simply loops through routines using counts of pauses between sw presses.

    part of sw press checks......

    FOR B5= 1 TO 10 '.1 SECONDS FOR TIMER
    PAUSE 6
    if GPIO.1= 1 then start
    NEXT B5

    read 1,b6 :PAUSE 10: W0=60*B6:W1=W0
    HIGH 4 ': HIGH 2

    FOR B5 = 1 TO 100 '2 SECONDS FOR 6 HR
    PAUSE 10
    if GPIO.1=1 then TIMER
    NEXT B5
    W0=28800 '8 HRS
    HIGH 2

  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?

    You might do something like this:
    Obvioulsy you can't have code that takes a long time to exectute in each section or you'll miss the button press. If that's the case, move the code that handles the button to a subroutine and call that routine multiple times from within the section(s) that takes the longest time.
    Code:
    ButtonPressed          CON 0
    FirstPieceOfCode       CON 1
    LastPieceOfCode        CON 3
    
    CodeToRun              VAR BYTE
    ButtonHasBeenPressed   VAR BIT
    
    ChangeButton           VAR PortB.2
    
    CodeToRun              = FirstPieceOfcode
    ButtonHasBeenPressed   = 0
    
    Main:
    
      If ChangeButton = ButtonPressed THEN
        ButtonHasBeenPressed = 1
      ENDIF
    
      IF (ButtonHasBeenPressed = 1) AND (ChangeButton <> ButtonPressed) THEN  ' But was pressed but is now released
        ButtonHasBeenPressed = 0
        CodeToRun = CodeToRun + 1             ' Advance to next code section
        If CodeToRun > LastPieceOfCode THEN   ' Wrap around and start over
          CodeToRun  = FirstPieceOfCode
        ENDIF
      ENDIF  
    
    
      Select Case CodeToRun
    
        Case 1
           'Some code
     
       Case 2
           'Some other code
    
        Case 3
           'Yet some other code
    
      END SELECT
    
    Goto Main
    This is not tested or even compiled so use it as an idea.

  4. #4
    Join Date
    Feb 2013
    Posts
    1,124


    Did you find this post helpful? Yes | No

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

    I think, there is some misunderstanding.

    Here what I need

    Code:
    code1:
    if button=0 then goto code2
    'somecode here
    goto code1
    
    code2:
    if button=0 then goto code1
    'somecode here
    goto code2
    So all I need is when user presses the button in code1, execution won't jump to code2 until he releases the button. And same for code2.

  5. #5
    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?

    The code I posted should do exactly with that, with the advantage that the section of code you're currently in will continue to run UNTIL you release the button. What you're proposing will HANG the current program section until you release the button. But here you go:
    Code:
    code1:
    if button=0 then     'check if button is pressed
      PAUSE 5                       'allow some contact bounce
      WHILE button=0 : WEND  'wait for it to be released
      PAUSE 5                     'allow some contact bounce
      goto code2
    ENDIF
    'somecode here
    goto code1
    
    code2:
    if button=0 then
      WHILE button = 0 : WEND
      goto code1
    ENDIF
    'somecode here
    goto code2
    and obviously, button is a reserved word.

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


    Did you find this post helpful? Yes | No

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

    Thanks!
    No problem for code to "Hang", since this is for going from main menu to sub-menu and back.

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