Trouble exiting from a gosub

tracecom

Senior Member
I have a project that requires that I control a DC motor through an H-bridge based on the setting of five switches. The code seems to work well until it hits the gosub watch_sgs, but then hangs. I don't understand what is different, i.e., why the prior gosub calls work and this one doesn't. Thanks for any insight.

ETA: The code isn't finished; I am just hung at this point.

Code:
'19 Feb 2016
'Schematic: RK Motor Controller 2.dch
'Code: RK Motor Controller 2.bas
'Overview: 'Controls motor for RK Door Closer
#picaxe 14M2 'Identify the PICAXE being used as a 14M2.
#no_data 'Prevent data from being downloaded to PICAXE.

symbol MAS = pinC.0
symbol PGS = pinC.1
symbol SGS = pinC.2
symbol FLS = pinC.3
symbol RLS = pinC.4
symbol Fwd = B.1
symbol Rvr = B.5




pullup %0001110000000000

do
'manual set sequence follows
if FLS = 1 and MAS = 0 then
	high Fwd
	low Rvr
'auto set sequence follows
elseif RLS = 1 and MAS = 1 then
	low Fwd
	high Rvr
'open sequence follows
elseif FLS = 0 and SGS = 1 and PGS = 0 and MAS = 1 then
	gosub watch_PGS
	pause 6000
	high Fwd
	low Rvr
	gosub watch_FLS
	low Fwd
	low Rvr
	gosub watch_SGS
	low Fwd
	high Rvr
	
else
	low Rvr
	low Fwd
endif
loop

watch_RLS:
do
	if RLS = 1 then
		exit
	endif
loop
return

watch_FLS:
do
	if FLS = 1 then
		exit
	endif
loop
return

watch_SGS:
do
	if SGS = 0 then
		exit
	endif
loop
return

watch_PGS:
do	
	if PGS = 1 then
		exit
	endif
loop
return

watch_MAS:
do	
	if MAS = 1 then
		exit
	endif
loop
return
 

hippy

Ex-Staff (retired)
You could probably rewrite your ...

Code:
watch_MAS:
do	
	if MAS = 1 then
		exit
	endif
loop
return
and similar as simpler ...

Code:
watch_MAS:
  do : loop until MAS = 1
  return
And even replace your "Gosub watch_MAS" with "do : loop until MAS = 1".
 
Top