Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:56 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : LOCAL problem.....

Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 01:58am 17 Apr 2018
Copy link to clipboard 
Print this post

Hi all.

QUESTION: With the LOCAL command for use in Sub's, can different subs have different LOCAL commands with the same variable name? I thought you could, but it it throwing a wobbly when I try:

  Quote  SUB SETUP_HC12
CPU 10
Const HC12=17 'HC12 SET line. (sleep/wake)
SetPin HC12,DOUT:Pin(HC12)=1 'HC12 SET line
Open "COM1:9600" as #1 'Default baud-rate for a new HC12 is 9600...
Pin(HC12)=0 'Enter command mode
Pause 50 'Wait for module to respond
Print "Now attempting to configure new HC12 module @ 9600 baud..."
Print
Print "<HELLO MODULE?> ";
SET_HC12(
"AT")
Print "<SET 1200 BAUD> ";
SET_HC12(
"AT+B1200")
Print "<SET CHANNEL 64(458.6MHz)> ";
SET_HC12(
"AT+C064")
Print "<SET FU4 MODE> ";
SET_HC12(
"AT+FU4")
Print "<SET 100mW OUTPUT> ";
SET_HC12(
"AT+P8");
Pause 250
Pin(HC12)=1 'Exit command mode - changes take effect now!
Print "DONE! Execute CHECK_HC12 to confirm settings."
Close #1
End SUB

SUB CHECK_HC12
CPU 10
Const HC12=17 'HC12 SET line. (sleep/wake)
LOCAL D$
SetPin HC12,DOUT:Pin(HC12)=1 'HC12 SET line
Open "COM1:1200" as #1 '1200 baud now, as module should be programmed...
Pin(HC12)=0 'Enter command mode
Pause 50 'Wait for module to respond
Print "QUERY MODULE:"
Print
Print #1,"AT+RX"
Print
Pause 1000
D$=
Input$(Loc(#1),#1)
Print D$
Print
Print "DONE."
Close #1
Pin(HC12)=1
End SUB

End

SUB SET_HC12(E$)
LOCAL D$,E$
Print #1,E$
Pause 150
D$=
Input$(Loc(#1),#1) 'Suck HC12 response from buffer
Print D$
Pause 150
End SUB


I am getting 'E already declared.' errors.

As one sub calls the other, D$ is throwing an error, but I figured you could have LOCAL D$,E$ in one Sub, and another LOCAL D$,E$ in another sub and it would not care - as they are local to the sub they are defined in, and vanish into the great RAM monster in the sky when the Sub ends.

Do I actually have that wrong?

If I change the string names to something else, no problem, so that is the workaround I am using for now.Edited by Grogster 2018-04-18
Smoke makes things work. When the smoke gets out, it stops!
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 02:12am 17 Apr 2018
Copy link to clipboard 
Print this post

That's a bit Strange Grog,

I'd expect you could & this little bit of code works in DOS.

  Quote  Do

Sub1
Sub2
Pause 1000

Loop

Sub Sub1

Local N%

for n%=1 to 3
Print "Sub1 N=";n%
Next N%

End Sub

Sub Sub2

Local N%

for n%=1 to 3
Print "Sub2 N=";n%
Next N%

End Sub



Phil.Edited by Phil23 2018-04-18
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 02:17am 17 Apr 2018
Copy link to clipboard 
Print this post

This works too, when Sub2 is called while still in Sub1.

  Quote  Do

Sub1
'Sub2
Pause 1000

Loop

Sub Sub1

Local N%

for n%=1 to 3
Print "Sub1 N=";n%
Next N%
Sub2

End Sub

Sub Sub2

Local N%

for n%=1 to 3
Print "Sub2 N=";n%
Next N%

End Sub


Edit:- Still works if I call Sub2 before the NEXT N in Sub1 also.
Edited by Phil23 2018-04-18
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 02:47am 17 Apr 2018
Copy link to clipboard 
Print this post

You are using E$ as the passed argument and a local variable.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 03:44am 17 Apr 2018
Copy link to clipboard 
Print this post

OH !!!!!!!!

How the hell did I miss that, for God's sake.......(rhetorical!)
All fixed.

Feeling a little silly now.
Smoke makes things work. When the smoke gets out, it stops!
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 04:19am 17 Apr 2018
Copy link to clipboard 
Print this post

Glad I spotted it first then.

Even more happy it is all working.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 05:06am 17 Apr 2018
Copy link to clipboard 
Print this post

I don't think you should be using 'CONST" in a SUB/FUNCTION
If it is a true CONST, it belongs in the main code with the GLOBALs
If not a true CONST, use a LOCAL variable

Jim
VK7JH
MMedit
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 06:06am 17 Apr 2018
Copy link to clipboard 
Print this post

  Azure said   You are using E$ as the passed argument and a local variable.


Was pretty easy to look straight past that.

P&G Zero.
Azure One.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 06:32am 17 Apr 2018
Copy link to clipboard 
Print this post

@ Jim - I will take your comments about CONST under advisement. Yes, I will change that.

[Quote=Phil23]P&G Zero[/Quote]

I see what you did there.
Smoke makes things work. When the smoke gets out, it stops!
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 06:41am 17 Apr 2018
Copy link to clipboard 
Print this post

Lets not start any scoring, I am just a newbie to the MM and grogster designs some lovely boards .
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 06:49am 17 Apr 2018
Copy link to clipboard 
Print this post

Very kind, but Azure does get an approval tick for noticing this one!
Smoke makes things work. When the smoke gets out, it stops!
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1097
Posted: 11:09am 17 Apr 2018
Copy link to clipboard 
Print this post

You're way ahead in the number of Posts count though , Mr Grogster
ChopperP
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025