Home  |  Contents 

Microcontroller and PC projects
  Forum Index : Microcontroller and PC projects         Section
Subject Topic: LOCAL problem..... Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 5619
Posted: 17 April 2018 at 11:58am | IP Logged Quote Grogster

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 on 17 April 2018 at 12:02pm


__________________
Smoke makes things work. When the smoke gets out, it stops!


Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
Phil23
Guru
Guru


Joined: 27 March 2016
Location: Australia
Online Status: Offline
Posts: 1379
Posted: 17 April 2018 at 12:12pm | IP Logged Quote Phil23

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 on 17 April 2018 at 12:14pm
Back to Top View Phil23's Profile Search for other posts by Phil23
 
Phil23
Guru
Guru


Joined: 27 March 2016
Location: Australia
Online Status: Offline
Posts: 1379
Posted: 17 April 2018 at 12:17pm | IP Logged Quote Phil23

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 on 17 April 2018 at 12:29pm
Back to Top View Phil23's Profile Search for other posts by Phil23
 
Azure
Senior Member
Senior Member
Avatar

Joined: 09 November 2017
Location: Australia
Online Status: Online
Posts: 243
Posted: 17 April 2018 at 12:47pm | IP Logged Quote Azure

You are using E$ as the passed argument and a local variable.
Back to Top View Azure's Profile Search for other posts by Azure
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 5619
Posted: 17 April 2018 at 1:44pm | IP Logged Quote Grogster

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!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
Azure
Senior Member
Senior Member
Avatar

Joined: 09 November 2017
Location: Australia
Online Status: Online
Posts: 243
Posted: 17 April 2018 at 2:19pm | IP Logged Quote Azure

Glad I spotted it first then.

Even more happy it is all working.
Back to Top View Azure's Profile Search for other posts by Azure
 
TassyJim
Guru
Guru


Joined: 07 August 2011
Location: Australia
Online Status: Offline
Posts: 2459
Posted: 17 April 2018 at 3:06pm | IP Logged Quote TassyJim

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

__________________
It all started with the ZX81....
VK7JH
http://www.c-com.com.au/MMedit.htm
Back to Top View TassyJim's Profile Search for other posts by TassyJim Visit TassyJim's Homepage
 
Phil23
Guru
Guru


Joined: 27 March 2016
Location: Australia
Online Status: Offline
Posts: 1379
Posted: 17 April 2018 at 4:06pm | IP Logged Quote Phil23

Azure wrote:
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.
Back to Top View Phil23's Profile Search for other posts by Phil23
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 5619
Posted: 17 April 2018 at 4:32pm | IP Logged Quote Grogster

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

Phil23 wrote:
P&G Zero


I see what you did there.

__________________
Smoke makes things work. When the smoke gets out, it stops!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
Azure
Senior Member
Senior Member
Avatar

Joined: 09 November 2017
Location: Australia
Online Status: Online
Posts: 243
Posted: 17 April 2018 at 4:41pm | IP Logged Quote Azure

Lets not start any scoring, I am just a newbie to the MM and grogster designs some lovely boards .
Back to Top View Azure's Profile Search for other posts by Azure
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 5619
Posted: 17 April 2018 at 4:49pm | IP Logged Quote Grogster

Very kind, but Azure does get an approval tick for noticing this one!

__________________
Smoke makes things work. When the smoke gets out, it stops!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
Chopperp
Regular Member
Regular Member
Avatar

Joined: 03 January 2018
Location: Australia
Online Status: Offline
Posts: 91
Posted: 17 April 2018 at 9:09pm | IP Logged Quote Chopperp

You're way ahead in the number of Posts count though , Mr Grogster

__________________
ChopperP
Back to Top View Chopperp's Profile Search for other posts by Chopperp
 


If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Search the forums using Google.
In the news...
 
Post ReplyPost New Topic
Printable version Printable version
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Powered by Web Wiz Forums version 7.8
Copyright ©2001-2004 Web Wiz Guide

This page was generated in 0.1055 seconds.
Privacy Policy     Process times : 0, 0, 0, 0.11