Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 04:30 07 Jul 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 : Calling a subroutine

     Page 1 of 2    
Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 08:47am 26 Jun 2016
Copy link to clipboard 
Print this post

Can you call a sub from within a sub?
or if you can, can you call a sub from within the 2nd sub?

I'm having problems with this

What I ideally want to do is
1: Turn the GPS on 'PIN(23) = 0 does this
2: Get GPS Info 'SUB GetGPSData
3: Print the info coming from the GPS (GPSPRINT)
4: SET the Micromite TIME with the data from the GPS ' NOT WORKED OUT HOW YET (to be added at a later stage)
4A: AFTER 60 SECONDS TURN GPS OFF 'PIN(23) = 1 does this
5: (at a later stage) USE GPS time to SET TIME on Micromite 'not figured out how to do this yet**
6: Carry on and do the weather routing
7: (at a later stage) SET THE time FROM the GPS at 02:05 every morning

I can do numbers 1-4 with no problem
I can do number 5 with no problem

When I put the 2 sections of code together it turns the GPS power on
BUT
Carries on and does the weather routine and doesn't check the GPS data to decode the time and date (the bits I want to print and use)

'**************************************
The gps routine works fine on it's own
The weather routine works fine on it's own
but when I put them both together I cannot get working properly
'*******************************************


' ******************this is the main program loop
Watchdog 600000 'checks program every 10 minutes and if stuck in loop reboots
SETGPS 'call GPS routine and after 60 seconds turn the GPS OFF
do
if secs mod 5 = 0 then 'every 5 secs
windSpeedProc 'wind speed routine
WindDirProc 'wind direction routine
rainProc 'rain data routine
'CLOCKUPDATESUB 'not implemented yet
End if
if secs mod 10 = 0 then 'every 10 secs
'read humidity ***************not implemented yet
'read temp ***************not implemented yet
End if
if secs mod 20 = 0 then 'every 20 secs
SendDataUpdate
'Print "Outside Temperature: "
'Print "Outside Humidity: "
End if

'Select Case mins ***************not implemented yet
'Case 5,10,15,20,25,30,35,40,45,50,55,60
'Print "5 mins_ Sending Data Update"
'SendDataUpdate
'End Select
loop
End


'***********SUBROUTINES*****************************************

SUB SETGPS
PIN(23) = 0 'GPS Power ON
GetGPSData ' get the next line
IF arg$(0) = "GPRMC" THEN ' GPRMC contains lat/long
IF arg$(2) = "A" THEN ' "A" means locked on to satellites
GPSPRINT
if secs = 60 then 'after 4 minutes
PIN(23) = 1 'GPS Power OFF
end if
ELSE
PRINT "GPS searching..."
ENDIF
ENDIF
END SUB

SUB GetGPSData
DO
DO WHILE INPUT$(1, #1) <> "$" : LOOP ' wait for the start
FOR i = 0 TO max
arg$(i) = "" ' clear ready for data
DO ' loops until a specific exit
x$ = INPUT$(1, #1) ' get the character
IF x$ = "," THEN EXIT ' new data item, increment i
IF x$ = "*" THEN EXIT SUB ' we have all the data so exit
arg$(i) = arg$(i) + x$ ' add to the data
LOOP ' keep going
NEXT i ' increment i
LOOP
END SUB


SUB GPSPRINT
PRINT "$"arg$(0)"," arg$(1)"," arg$(2)"," arg$(9)"," 'Print Time and Date
END SUB


'B1 - 1 second Tick interrupt
Sub B1
secs=secs+1 'update seconds timer
if secs => 43200 then '12 hrs reached
secs = 0 'reset counter
Pin(23) = 0 'GPS Power ON
endif
END SUB


CLOCKUPDATESUB
If Time$="02:05" Then 'GET GPS TIME & UPDATE CLOCK
'put code here 'sync Clock
'not figured this bit out
END SUB
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4036
Posted: 10:03am 26 Jun 2016
Copy link to clipboard 
Print this post

Yes you can call a SUB from a SUB - many levels deep. (Eventually you hit a limit.)

JohnEdited by JohnS 2016-06-27
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 10:26am 26 Jun 2016
Copy link to clipboard 
Print this post

Thanks John

Anyone got any ideas why I'm having the problem where it won't do GPS routine and then the weather?

here's the weather routing working on it's own


and her's the GPS routine working on it's own





 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 10:32am 26 Jun 2016
Copy link to clipboard 
Print this post

and in case anyone feels like helping out - here's both the weather routine
and the gps code in text format
2016-06-26_203028_Weather_so_far.zip

What I ideally want is to get the code to do the GPS routine on startup, then turn the gps off after 1 minute (which the gps code does)
Set the Micromite clock from the GPS code
THEN do the weather routine
and at 02:05AM every morning do the gps routine and set the clock again and then carry on with the weather code.

btw the weather routine now has the temperature and humidity code included and working perfectly.

Edited by lew247 2016-06-27
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 10:32am 26 Jun 2016
Copy link to clipboard 
Print this post

Hi Lew,

Add a heap more print statements for debugging purposes.

They can either just scroll on the screen,
or you can format them a bit.

It will help debug where if is getting stuck, or what its making decisions on.

I've got all this coming to the console from the Spa controller ATM, primarily for debugging.

Esc[f returns the cursor home before each print.

[Code]
Sub UpdateConsole
'Output Stuff to console for debugging purposes
Print Esc+"[f";
'Print Esc+"[2J";
Print "Water Temperature = ", Str$(TmpCur,4,2)
Print "Target Temperature = ", Str$(TmpTar,4,2)
Print
Print "Input Temperature = ", Str$(TmpInp,4,2)
Print "Output Temperature = ", Str$(TmpOut,4,2)
Print "Temperature Difference = ", Str$(TmpOut-TmpInp,4,2)
Print "Temp Difference Setting = ", Str$(TmpDif,4,2)
Print
Print "Solar Panel Voltage (mV)= ", Str$(SolarVolts,4,2)
Print "Solar Threshold Voltage (mV)= ", Str$(SolarThres,4,2)
Print "Solar Minimum Voltage (mV)= ", Str$(SolarMin,4,2)
Print "LM336 Voltage Out (mV)= ", Str$(LM336Volts,4,2)
Print
Print "Air Temperature = ", Str$(TmpAmb,4,2)
Print "Panel Temperature = ", Str$(TmpPan,4,2)
Print "Panel Temp Diferential = ", Str$(TmpDifPan,4,2)
Print
Print "Time is ", Time$
Print "Pump is",PumpStat
Print "Pump Run Time is ", SecsTime(PumpRunTime)

Print "Key Pressed is:- ", KeyPress

End Sub
[/code]

Cheers

Phil
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 10:35am 26 Jun 2016
Copy link to clipboard 
Print this post

  lew247 said   Thanks John

Anyone got any ideas why I'm having the problem where it won't do GPS routine and then the weather?

here's the weather routing working on it's own





Add Esc[f at the beginning of that one & run MM chat in VT mode for example.
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 10:42am 26 Jun 2016
Copy link to clipboard 
Print this post

That was MMChat*
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 10:51am 26 Jun 2016
Copy link to clipboard 
Print this post

  lew247 said   That was MMChat*


Yes,
But I notice you are in ASCII mode. (Can see the box at the top).
for formatted output you need to use VT mode.

Even if I'm using VT mode, I can run my code briefly, break it, then switct back to ASCII mode & see all the print out with out formatting.

On occasions, I've copied & pasted the whole lot into an editor & searched.

Some of the other things to print for example are "Starting sub XXX" & Exiting Sub XXX", solely for debugging.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6269
Posted: 12:56pm 26 Jun 2016
Copy link to clipboard 
Print this post

  Phil23 said  
  lew247 said   That was MMChat*


Yes,
But I notice you are in ASCII mode. (Can see the box at the top).
for formatted output you need to use VT mode.


If you are going to leave it running for any length of time in VT mode, please use TeraTerm.
Unless you clear the screen occasionally, MMEdit will not be happy and we don't want any more complications.

In MMEdit ASCII mode with capture to file is a good way to get long term records.
You can add time-stamping to each line (in MMEdit not your program) for a better understanding of when things went belly-up.

Jim
VK7JH
MMedit
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 01:55pm 26 Jun 2016
Copy link to clipboard 
Print this post

  TassyJim said  
  Phil23 said  
  lew247 said   That was MMChat*


Yes,
But I notice you are in ASCII mode. (Can see the box at the top).
for formatted output you need to use VT mode.


If you are going to leave it running for any length of time in VT mode, please use TeraTerm.
Unless you clear the screen occasionally, MMEdit will not be happy and we don't want any more complications.
Jim


Yes,

I do just that.
Run some code briefly in ASCII mode, Capture the output & ignore the formatting characters.

For any long term observation I sit Teraterm on one of my monitors.

I also find TinyTERM Lite handy on my phone for occasional checks on what's happening.

Only annoying thing is it's keyboard buttons are a respectable size, but the font inside them is totally too small to ready on my Galaxy S7.

It doesn't appear that way in their screen examples.

Cheers.

 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 11:17pm 26 Jun 2016
Copy link to clipboard 
Print this post

WHY isn't this working?

It does the GPS routine perfectly
It turns the GPS off after 60 seconds which is what the gps subroutines tell it to do
[quote]TURNED GPS POWER OFF END of B3[/quote]

It just won't get to the 2nd Loop which is simple a PRINT statement for testing purposes


[code]Do while secs =< 65
Do
GetGPSData ' get the next line
'Print "RETURNED FROM GetGPSData"
IF arg$(0) = "GPRMC" THEN ' GPRMC contains lat/long
IF arg$(2) = "A" THEN ' "A" means locked on to satellites
'print "GOING TO GPSPRINT"
GPSPRINT
'print "GOING TO B3"
B3
ELSE
PRINT "GPS searching..."
ENDIF
ENDIF
Loop

print "got to 2nd Loop"
loop
end[/code]
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 11:20pm 26 Jun 2016
Copy link to clipboard 
Print this post

Your second 'Do' loop does not have a condition so it never ends.

Microblocks. Build with logic.
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 11:25pm 26 Jun 2016
Copy link to clipboard 
Print this post

That was intentional

It doesn't get there though - it DOES NOT PRINT "got to 2nd Loop"
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 11:50pm 26 Jun 2016
Copy link to clipboard 
Print this post

I've narrowed the program down to it's simplest stages

It stays in LOOP 1 and never gets to the 2nd loop

any ideas why?

[code]Settick 1000 ,L1,1 'establish seconds "Tick Timer"

Do while secs =< 5
Do
print "Stage 1 loop"
Loop
print "got to 2nd Loop"
loop

end

'L1 - 1 second Tick interrupt
Sub L1
secs=secs+1 'update seconds timer
if secs => 43200 then '12 hrs reached
secs = 0 'reset counter
endif
end sub
[/code]

If I put the Both LOOP statements just above the END the programs just loops stage 1-2nd loop-stage 1 -2nd loop continuallyEdited by lew247 2016-06-28
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 11:52pm 26 Jun 2016
Copy link to clipboard 
Print this post

Again that is because the Do does not have a condition.
It will never ever reach that line.

----------------------
Do while secs =< 65
 Do
  GetGPSData ' get the next line
  'Print "RETURNED FROM GetGPSData"
  IF arg$(0) = "GPRMC" THEN ' GPRMC contains lat/long
   IF arg$(2) = "A" THEN ' "A" means locked on to satellites
    'print "GOING TO GPSPRINT"
    GPSPRINT
    'print "GOING TO B3"
    B3
   ELSE
    PRINT "GPS searching..."
   ENDIF
  ENDIF
 Loop

print "got to 2nd Loop"
loop
end
---------------------

The part in red is an endless loop!

Your second example has the same problem
[code]
Settick 1000 ,L1,1 'establish seconds "Tick Timer"

Do while secs =< 5
Do '<----------------- this needs a condition!! --------------
print "Stage 1 loop"
Loop
print "got to 2nd Loop"
loop

end
[/code]Edited by MicroBlocks 2016-06-28
Microblocks. Build with logic.
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 11:56pm 26 Jun 2016
Copy link to clipboard 
Print this post

It's still stuck
It does ALL of Loop 1 (get gps stuff) and it gets to the end of Loop 1 where it prints TURNED GPS POWER OFF END
But won't get into the 2nd Do statement/loop (print "got to 2nd Loop")

[code]Do
Do while secs < 20
GetGPSData ' get the next line
'Print "RETURNED FROM GetGPSData"
IF arg$(0) = "GPRMC" THEN ' GPRMC contains lat/long
IF arg$(2) = "A" THEN ' "A" means locked on to satellites
PRINT "$"arg$(0)"," arg$(1)"," arg$(2)"," arg$(9)"," 'Print Time and Date
ELSE
PRINT "GPS searching..."
ENDIF
ENDIF
Print "END of 1st Loop"
if secs = 15 then 'after 4 minutes
PIN(23) = 1 'GPS Power OFF
print "TURNED GPS POWER OFF END"
END IF
Loop
print "got to 2nd Loop"
loop

end[/code]




Edited by lew247 2016-06-28
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2932
Posted: 01:20am 27 Jun 2016
Copy link to clipboard 
Print this post

Just try adding a few more 'debug' lines (below in bold).

WW


Do
PRINT "1: ",str$(secs)
Do while secs < 20
PRINT "2: ",str$(secs)
GetGPSData ' get the next line
'Print "RETURNED FROM GetGPSData"
IF arg$(0) = "GPRMC" THEN ' GPRMC contains lat/long
IF arg$(2) = "A" THEN ' "A" means locked on to satellites
PRINT "$"arg$(0)"," arg$(1)"," arg$(2)"," arg$(9)"," 'Print Time and Date
ELSE
PRINT "GPS searching..."
ENDIF
ENDIF
Print "END of 1st Loop"
PRINT "3: ",str$(secs)
if secs = 15 then 'after 4 minutes
PIN(23) = 1 'GPS Power OFF
print "TURNED GPS POWER OFF END"
END IF
Loop
PRINT "4: ",str$(secs)
print "got to 2nd Loop"
loop

end
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 01:30am 27 Jun 2016
Copy link to clipboard 
Print this post

Interesting it's stuck in a loop at Print "2"
[quote]END of 1st Loop
3: 14
2: 14
END of 1st Loop
3: 14
2: 14
$GPRMC,112939.791,A,270616,
END of 1st Loop
3: 15
TURNED GPS POWER OFF END
2: 15
[/quote]


Edit:

Thanks everyone, got it sorted now Edited by lew247 2016-06-28
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 02:46am 27 Jun 2016
Copy link to clipboard 
Print this post

Why would a "FOR" statement not register?

a section of code (the prints are there to test where it fails)
[code]
For z=1 To 10 'addup all data in array
print "Z1"
wResult=wResult+wArray(z)
print "Z2"
Next z
[/code]





result is its adding the array up as I can see by the zA, Z1 and Z2 prints but when it gets to next z if think's there's no "FOR"

EDIT:

I only get this if I put the "turn gps on section" into a subroutine and not when it's in the main program loop

The section it fails on is nothing to do with the gps at all, it only goes to that section after the gps is turned off
Edited by lew247 2016-06-28
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10215
Posted: 03:16am 27 Jun 2016
Copy link to clipboard 
Print this post

  Quote  think's there's no "FOR"


What version of firmware are you running? - I seem to remember this was a bug in one version but may be miss-remembering
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025