To gosub or not to go Gosub

jamesrj

New Member
My program is simples, so I thought. Simple motor up/down in a colliery, with an audable signal to start the process, fine. Upper level to mid level, wait a time, same signal to bottom level, another wait, different signal to raise to mid level another wait, same signal to top level. Repeat. I am using a 18m chip which offers me enough inputs and outputs that are required.
Have dedicated symbols for the motor and the various leds, sound= 5, So I am calling Gosub noise; rem calling sound to next levels
noise: for b3 = 1 to 4
Pause 100
sound 5, ( 100, 75)
next b3
Pause 200
for b3 = 1 to 2
Sound 5, (100,75)
pause 100
next b3
pause 300
return
Now the return loops me back to noise again & on this second return gives me a stack over flow error. Have tried the example in the manual but that takes me back to the end of the main and does not complete any further program. am aware that I require a different Gosub for the raise as it's it different signal .Have tried second
Gosub noise: within the prog but the program never reaches it. If I leave out the command Return the program will run to the next Gosub noise; what am I not doing correctly. RJ
 
I'm guessing there is a way to reach Noise:, without using a Gosub - probably by just falling-through to it.

Can you show the rest of the program?
 
Hi,

You haven't shown your main: program, which needs to be a Loop in its own right. It sounds as if main: may be just "falling into" the subroutine. The basic rule is that you should only enter a subroutine via a GOSUB or a CALL instruction, or there can be no destination recorded for the RETURN.

Also, you are not passing a "parameter" into the subroutine so it cannot produce a "different" sound. You need either the SOUND or PAUSE instructions to contain a variable (e.g. b1, etc. pre-loaded before entry), or to create a separate subroutine, e.g. noiseup: .

Cheers, Alan.
 
Hi,
Meanwhile the poor miners are trapped in the pit.
Good luck...........
edit : PS. I haven't changed the font. Working on it.
 
Thank you Gentlemen for your replies , I am passed my sell date but, still going.

; colliery mine motor control with 18m picaxe

; symbol relay _dwn=7
; symbol relay _up =6
; symbol _sound =5
; symbol red_led =4 rem
; symbol green_led=3
; symbol yellow _led =2
; symbol orange_ led=1
; symbol counter =b0 (magnet on winding drum with a hall effect sensor, using that as an input b0 )
;pause 1000
if pin 0=1 then go to lower
lower:
pause 1000
gosub noise; rem call sound down to next level to banksman 5 raps lower steady 3 raps men on
noise: for b3 = 1 to 5
pause 100
sound 5,(100,75)
next b3
pause 200
for b3= 1 to 3
sound 5, (100,75)
pause 100
next b3
I put in a return here but it looped through noise again and went into stack overflow error
pause 1000
high 7 ; rem motor dwn
if b0 =4 then low 7
end if
pause 1000
let b0 = b0 max 0 ( resetting count to zero)
(have left out led operations to save space, also large time intervals have been left out )
gosub noise:
noise;
then another high 7 as above to get me to the bottom, the raise program is nearly the same ,only difference is in the operation of the leds

Of interest Mr Hornby, have Hornby name in family tree, through Blair family, north of England. RJ
 
Can you post the real code that you are trying? What you posted in #5 is riddled with syntax errors.

Go to your program in the Programming Editor, select all (<Ctrl>A) and then click "Forum" in the "Home" tab. Then paste the contents of the clipboard into a post here.
 
Sorry for delay doing my best , Inglewoodpete, sir, you say prog has syntax errors, would you be kind enough to point these out as prog informs me that syntax check is ok . My problem is where do the returns go within the prog to enable it to run after the sound has been called once.
Have not used a gosub before, therefore am at a loss here. I have left out the leds, as these are simple highs & lows in Prog, and the raise follows in the the same way as lower with different sounds. Sorry , having trouble sending prog to forum , on posting to forum page comes back with text giving colours within prog ???? RJ Think I will have to do prog the long way round, many thanks Gentlemen for your help.
RJ
 
The code you posted in #5 has statements like
Code:
if pin 0=1 then go to lower
This - as it stands - fails syntax checking because
(1) "pin" is not a recognised word
(2) even if you meant (and actually wrote) pin0, that is also not a recognised word or symbol

Taking just a part of your code, and trying to follow it through ...

Code:
'POINTZERO
if pin 0=1 then go to lower
lower:
pause 1000
'POINTA
gosub noise; rem call sound down to next level to banksman 5 raps lower steady 3 raps men on
'POINTB
noise: for b3 = 1 to 5
pause 100
sound 5,(100,75)
next b3
pause 200
for b3= 1 to 3
sound 5, (100,75)
pause 100
next b3
'POINTC
return

Starting at POINTZERO (and assuming that the "if" statement could actually compile ...
whether the statement
Code:
if pin 0=1
is true or not, the next statement executed is "lower:"
then POINTA
then "gosub noise"
which finishes with a "return" at POINTC
the return goes to the next instruction after the gosub which is POINTB,
which is then "noise" (again)
So, "noise" is executed a second time, but was not called (this time) by a gosub, so it has nowhere to return and gives you a stack overflow.
 
I does not look like you have a main program loop defined. It also looks like you have noise declared more that once. We really need to see your entire program, not just little bits of it strung incoherently together.

A subroutine (noise in your case) should be declared once and outside of the main code. I usually put my subroutines at the end of the program.

In the example below, once noise is called by "gosub noise" the subroutine "noise" will be executed and will return to Main and begin execution at ".... first line of code after gosub ....".

Code:
Main:
.... code...
gosub noise
.... first line of code after gosub ....
... code ....
Goto Main

REM Subroutines below

Noise:
... code to implement noise ...
return
 
Of interest Mr Hornby, have Hornby name in family tree, through Blair family, north of England. RJ

I only just noticed this (the forum sometimes declines to send emails to me).

Blair is not a name that appears in my family tree - though I will admit to being definitely 'northern' :)
 
Back
Top