PDA

View Full Version : GOTO main or RETURN - Question



studysession
- 31st January 2009, 15:19
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.



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

g-hoot
- 31st January 2009, 15:34
I'm a rookie, but I think using subs will work like this. Subs have to be after the "end" statement.

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.


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

studysession
- 31st January 2009, 15:54
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

sinoteq
- 31st January 2009, 15:59
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.

studysession
- 31st January 2009, 16:04
Thanks sinoteq - using the GOSUB command worked perfectly and it returned just like I wanted. Greatly appreciate it.

HenrikOlsson
- 31st January 2009, 16:13
Well, you can only use RETURN when you have jumped to a routine with GOSUB - NOT when you jump to it with GOTO.

Try:

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