Noob needs some help please...


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Noob needs some help please...

    This looks like trouble in your code. You gosub to "check_button:" and then goto "button_loop:" if a button is pressed. This creates a case where the gosub never returns, which can cause problems. Beer may help.

    Code:
    check_button:
    
            if btn_up = 0 OR btn_dwn = 0 OR btn_back = 0 _  ' If any button is pressed 
                OR btn_nxt = 0 OR btn_entr = 0 then         ' Go to button loop
    
                High lcd_lite                               ' Turn on the LCD Backlight
                goto button_loop
            endif
            return

  2. #2
    Join Date
    May 2008
    Posts
    31


    Did you find this post helpful? Yes | No

    Default Re: Noob needs some help please...

    That bit has been reworked as follows...

    Code:
    check_button:
            if btn_up = 0 OR btn_dwn = 0 OR btn_back = 0 _  ' If any button is pressed... 
                OR btn_nxt = 0 OR btn_entr = 0 _            ' and the LCD Backlight is on...
                and lcd_lite = 1 then                       ' Then Go to button loop
                goto button_loop
            endif
            if btn_up = 0 OR btn_dwn = 0 OR btn_back = 0 _  ' If any button is pressed...
                OR btn_nxt = 0 OR btn_entr = 0 _            ' and the LCD backlight is not on...
                and lcd_lite = 0 then                       ' Then turn on the backlight...
                high lcd_lite                               ' ...and pause 2 seconds
                pause 2000               ' Holding the button longer than 2 sec. gets you to...
            endif                        ' the button loop VIA the mainloop & check_button and...
            return                       ' IF statement above.
    But the GOTO / GOSUB seems ok... At least is seems to be working.

    Code:
    button_loop:
    '
    if btn_up = 1 and btn_dwn = 1 and btn_back = 1 _  ' If no buttons are pressed go to mainloop...
        and btn_nxt = 1 and btn_entr = 1 then 
     goto mainloop                                 
        endif                                         ' Otherwise continue...
    
    button_set_loop:
    if btn_up = 0 then up                   ' If the up button is pressed go to the Up loop
    if btn_dwn = 0 then down                ' If the down button is pressed go to the Down loop
    if btn_back = 0 then back               ' If the back button is pressed go to the Back loop
    if btn_nxt = 0 then forward             ' If the next button is pressed go to the Next loop
    if btn_entr = 0 then                    ' If the enter button is pressed go to the Enter loop
            goto enter
        else
            goto button_loop                ' If no buttons are pressed run the button loop again...
    endif                                   '   ... which takes you to the main loop.
    
    up:
    while btn_up = 0                ' While the Up button is pressed do this loop...                    
    pause 30                        ' pause for 30 milliseconds
    alrm_set = alrm_set + 1         ' increase alarm set point by one degree F
    lcdout $FE, 1, "Alarm Set ", _  ' Display the alarm set point as it's changed
    dec (alrm_set),$DF, "F"
    pause 300
    wend                            ' Repeat, increasing it 1 degree F every ~1/3...
    lite_timer = 0                     '   ... second until the switch is released
    goto button_loop
    
    down:
    while btn_dwn = 0
    pau... (and it continues)
    The check_button loop returns to the main loop, the button_loop series all end with a GOTO mainloop so it finds its way back. In the code above what I wanted to accomplish was that any button press would turn on the LCD backlight but not increment the variable in button_loop in doing so. The above code work pretty well though I may back off the 2 second pause.
    I'm aware that there is much room for improvement in both the code and the hardware... I'm learning as I go. I'm working with a deadline of April 17th so I don't have the time to pretty it up. The hardware is functional with some software work-arounds... Rev B will be awesome!
    The IF statement about all the buttons being "1" (not pressed) is likely not necessary since it was a button press that got me there in the first place. Cleaning up that stuff will come later, right now it doesn't break anything and I'm not tight on space yet.

    Thanks for looking it over I'm grateful for any tips.

    PS I know... interrupts and timers... in Rev B
    Last edited by Neosec; - 15th April 2013 at 02:20. Reason: typo

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,170


    Did you find this post helpful? Yes | No

    Default Re: Noob needs some help please...

    GOTO out of a subroutine is never a good idea.

    Set a flag before GOSUB, change the flag within the subroutine, then check the flag once you've returned and GOTO at that point.

    Robert

    Edit: it may work this time, but it's a habit you don't want to get into.

  4. #4
    Join Date
    May 2008
    Posts
    31


    Did you find this post helpful? Yes | No

    Default Re: Noob needs some help please...

    I don't follow... can you give a quick example?

  5. #5
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: Noob needs some help please...

    In most programming languages, when you GOSUB, a number of registers get stored in memory to free them up for use by the subroutine. When you RETURN, those values are put back so the program can continue where it left off. When you GOTO out of a subroutine, the registers are not restored. If you did this when writing a program in a Windows or Linux environment, where the values are store in program RAM, the memory is not released when you GOTO out of the subroutine. This is called a memory leak in your program. Do this often enough, and all the RAM is used and not released until a reboot. It's a good habit to always return from a GOSUB with a RETURN to prevent this.

  6. #6
    Join Date
    Apr 2007
    Location
    Pennsylvania, USA
    Posts
    158


    Did you find this post helpful? Yes | No

    Default Re: Noob needs some help please...

    I had a similar routine once and it worked fine, until the 23rd time pressing the button it would cause the pic to reset. As Demon said, just have the button set a bit to 1 then return. Immediately after the gosub check if the bit is a 1 and if it is then goto wherever you want.
    Shawn

  7. #7
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,170


    Did you find this post helpful? Yes | No

    Default Re: Noob needs some help please...

    Quote Originally Posted by Neosec View Post
    I don't follow... can you give a quick example?
    Flag=0
    Gosub routine
    If flag = 1 then goto somewhere
    .
    .
    Routine:
    Bla bla bla
    If something then flag = 1
    Return

    Robert

  8. #8
    Join Date
    May 2008
    Posts
    31


    Did you find this post helpful? Yes | No

    Default Re: Noob needs some help please...

    OK, got it. Thanks to everyone for the input.

Similar Threads

  1. PICKit2 vs. JDM (Noob question)
    By loamobn in forum General
    Replies: 4
    Last Post: - 19th September 2010, 18:19
  2. NOOB in need of help
    By studysession in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 26th January 2009, 23:01
  3. Help a noob out?
    By yasiryassin in forum mel PIC BASIC
    Replies: 2
    Last Post: - 15th January 2008, 06:37
  4. (noob)hello world?
    By m4gill4 in forum mel PIC BASIC
    Replies: 2
    Last Post: - 30th November 2007, 00:23
  5. Uber noob needs clarity!
    By bill12780 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 23rd July 2007, 21:40

Members who have read this thread : 0

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