GOTO main or RETURN - Question


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Posts
    14

    Default GOTO main or RETURN - Question

    Here is the code for a small program I am trying. Trying to figure out when I replace "GOTO main" with "RETURN" the program only executes one of the items and nothing else happens. If I have "GOTO main" everything works but I really want it to return and continue where it left off instead of always going back to the top of "main" all the time.

    Hopefully I described that ok. Appreciate any help.

    Code:
    main
    if switch1 = 1 then servocenter 
    if switch1 = 0 then low led1
    if switch2 = 1 then servoright
    if switch2 = 0 then low led2
    if switch3 = 1 then servoleft
    if switch3 = 0 then low led3
    if switch4 = 1 then high led4
    if switch4 = 0 then low led4
    goto main
    
    servocenter:
    serout lcd1, t9600, [$fe,1," center"]
    high led1
    pulsout servo1, 150
    pause 200
    goto main
    
    servoright:
    serout lcd1, t9600, [$fe,1," right"]
    high led2
    pulsout servo1, 100
    pause 200
    goto main
    
    servoleft:
    serout lcd1, t9600, [$fe,1," left"]
    high led3
    pulsout servo1, 200
    pause 200
    goto main

  2. #2
    Join Date
    Dec 2005
    Posts
    46


    Did you find this post helpful? Yes | No

    Default

    I'm a rookie, but I think using subs will work like this. Subs have to be after the "end" statement.
    Code:
      main
    if switch1 = 1 then GoSub servocenter 
    if switch1 = 0 then low led1
    if switch2 = 1 then GoSub servoright
    if switch2 = 0 then low led2
    if switch3 = 1 then GoSubservoleft
    if switch3 = 0 then low led3
    if switch4 = 1 then high led4
    if switch4 = 0 then low led4
    goto main
    
    end
    
    servocenter:
    serout lcd1, t9600, [$fe,1," center"]
    high led1
    pulsout servo1, 150
    pause 200
    Return
    
    servoright:
    serout lcd1, t9600, [$fe,1," right"]
    high led2
    pulsout servo1, 100
    pause 200
    Return
    
    servoleft:
    serout lcd1, t9600, [$fe,1," left"]
    high led3
    pulsout servo1, 200
    pause 200
    Return
    OR, you could move each of the items below each if statement.

    Code:
      main
    if switch1 = 1 then GoSub 'servocenter 
    serout lcd1, t9600, [$fe,1," center"]
    high led1
    pulsout servo1, 150
    pause 200
    endif
    
    if switch1 = 0 then low led1
    
    if switch2 = 1 then GoSub 'servoright
    serout lcd1, t9600, [$fe,1," right"]
    high led2
    pulsout servo1, 100
    pause 200
    endif
    
    if switch2 = 0 then low led2
    
    if switch3 = 1 then GoSub 'servoleft
    serout lcd1, t9600, [$fe,1," left"]
    high led3
    pulsout servo1, 200
    pause 200
    endif
    
    if switch3 = 0 then low led3
    if switch4 = 1 then high led4
    if switch4 = 0 then low led4
    goto main
    Last edited by g-hoot; - 31st January 2009 at 15:42.

  3. #3
    Join Date
    Jan 2009
    Posts
    14


    Did you find this post helpful? Yes | No

    Wink

    Having everything within the IF statements work fine. I am trying to get a good handle on subs.

    The return after the "END" did not work. THanks for the input it was worth a try. I am sure it is something simple I am missing. Just unsure what it maybe.

    Keith

  4. #4
    sinoteq's Avatar
    sinoteq Guest


    Did you find this post helpful? Yes | No

    Default Gosub return

    You may use the GOSUB command when you want to jump to a SUB and then in an easy way get back to where you jumped from. The SUB you jump to can be anywhere in your program (before or after MAIN) as long as it has a unique name JUMPHERE: and the sub ends with a RETURN. Please remember PICs has a limited stack and can only make a few (depends on PIC version) nested GOSUBS before the first jump becomes invalid because you do a stack overflow. Same thing if you try a RETURN before you have made a GOSUB, that will give a stack underflow and will probably hang your program.
    Last edited by sinoteq; - 31st January 2009 at 16:08.

  5. #5
    Join Date
    Jan 2009
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    Thanks sinoteq - using the GOSUB command worked perfectly and it returned just like I wanted. Greatly appreciate it.

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,519


    Did you find this post helpful? Yes | No

    Default

    Well, you can only use RETURN when you have jumped to a routine with GOSUB - NOT when you jump to it with GOTO.

    Try:
    Code:
    Main:
    If Switch1 = 1 Then
      Gosub ServoCenter
    Else
      Low LED1
    EndIf
    
    'And so on...
    Goto Main
    END
    
    ServoCenter:
    'Code here
    RETURN
    
    'And so on....
    Edit: Oh well, was a bit late there I see....

Similar Threads

  1. Delayed output 10 secs
    By lilimike in forum mel PIC BASIC Pro
    Replies: 37
    Last Post: - 14th October 2011, 06:28
  2. Making a menu
    By chrisshortys in forum mel PIC BASIC Pro
    Replies: 36
    Last Post: - 12th November 2008, 19:54
  3. Replies: 14
    Last Post: - 26th September 2007, 05:41
  4. Output PIC module
    By freelancebee in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th September 2005, 20:10
  5. Can anyone help a beginner in a struggle?
    By douglasjam in forum mel PIC BASIC
    Replies: 1
    Last Post: - 5th May 2005, 23:29

Members who have read this thread : 2

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