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,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.

  2. #2
    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...

  3. #3
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

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

    You could create a bit Flag. When the Button is pressed, the Flag is toggled. When you get to a reasonable point in Code1 Routine, check your Flag. If it has toggled (is now a 1 for example), use GOTO to jump to Code2. Put "IF Flag = 0 THEN : GOTO Code1" in convenient spots throughout in Code2, etc.

  4. #4


    Did you find this post helpful? Yes | No

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

    debounce button check... and instead of while-wend .... use for/next pause here is 2 second check so if holding button > 2 seconds, can advance MENUitem then check again for release


    HERE:
    IF UPBT=0 THEN
    pause 50
    IF UPBT=0 THEN ..............................#debounce check

    MENUITEM=MENUITEM+1
    IF MENUITEM>6 THEN MENUITEM=1
    PAUSE 20

    for a=1 to 20
    pause 50................................................ ............# if 2 second press, advance item and wait again
    IF UPBT=1 THEN ---leave to somewhere .................# XXXXX WHILE UPBT=0:WEND
    next a
    goto HERE


    ENDIF
    Last edited by amgen; - 21st May 2023 at 17:59.

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,139


    Did you find this post helpful? Yes | No

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

    First I would read all button input at once. Debounce and then decide what to do.

    This is like a state machine coding and you will not loose any keypress, especially if it is interrupt driven. Or if you do not want interrupts, then a tight loop can do the job just fine.

    Spaghetti coding unfortunately leads to such results.

    Ioannis

  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?

    Thanks everyone. I've modified that code in the way that it now works - there is additional check for MENUITEM value inside a button press loop, but the question is still active - why the code above slows down in that way? That's simply not logical.

  7. #7


    Did you find this post helpful? Yes | No

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

    if there is room, you could put some 'temporary' code to serout or debug at a few locations and see where it isn't doing what is expected ! looks like there are quite a number of 'states' your program could be looping through.
    Last edited by amgen; - 28th May 2023 at 21:46.

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