# Programming protocols, Includes, subroutines, procedures.

#### camerart

Joined Feb 25, 2013
1,678
Hi,
When I program, I try to keep it as linear as possible, so that I can read it, otherwise I get lost.

I usually get sections of programs written for me, that I add into the MAIN program, and as usual I change it to be linear. E,G, say I get a section of CODE with a PROCESS in, instead of jumping to the process, I place it in a linear place in the program instead.

I'm used to GOSUBS, and I've come across INCLUDES, but again I suppose they could be placed linearly.

Thinking about it, perhaps, they are kept separately in sections, so that they can be used in different programs.

Taking PROCESSES, as an example, are done in this way for a reason? Can anyone tell me why please.

I have a linear MAIN program, that I'm just about to 'section' and need some pointers.

Camerart.

#### ericgibbs

Joined Jan 29, 2010
8,729
hi,
If a block of Code is used a number of times in a program, it should be written as a Subroutine/Proc/Function.

I place Subs at the End of the Code listing, in fact Oshonsoft states they must be after the End directive.

Includes are often used in order the keep the 'Glue' code uncluttered, eg: I use them for 'storing' character generator codes, which are usually in a fixed format, etc

On occasions some Code has to be written so that it is executed as fast as possible, eg: such as SPI comms, where a Call to Sub may not be fast enough.

Your Code should be commented, but uncluttered.

Use Label names that have some obvious meaning, but keep them short as possible

E

#### camerart

Joined Feb 25, 2013
1,678
hi,
If a block of Code is used a number of times in a program, it should be written as a Subroutine/Proc/Function.

I place Subs at the End of the Code listing, in fact Oshonsoft states they must be after the End directive.

Includes are often used in order the keep the 'Glue' code uncluttered, eg: I use them for 'storing' character generator codes, which are usually in a fixed format, etc

On occasions some Code has to be written so that it is executed as fast as possible, eg: such as SPI comms, where a Call to Sub may not be fast enough.

Your Code should be commented, but uncluttered.

Use Label names that have some obvious meaning, but keep them short as possible

E
Morning E,

Ok, I'll re-write my MAIN program, which has and INCLUDE and some PROCESSES and SUBROUTINES, then post it here, just for protocols. not for checking the program.
Thanks, C

#### camerart

Joined Feb 25, 2013
1,678
Hi E,
As I'm re-writing the MAIN program, I'm getting questions!

Will you scan the attached program, and give me comments please. (Only for PROTOCOLS not the program) I haven't added any CALLS, only put sections in order, below END. Note the order is just below the title.
Now it's been cut up, I find it really difficult to read!

SEARCH for XYZ, and you should find, READ COMPASS. Here is an SPI READ, than the calculations and settings, how would you treat this when setting up PROCESSESS? (Following you SPI comment)
C.

#### Attachments

• 28 KB Views: 12

#### jjw

Joined Dec 24, 2013
497
What do you mean by processes?

#### ericgibbs

Joined Jan 29, 2010
8,729
Hi C,
Started look at that text.
This sort of 'commenting' is OTT, there is a lot of this clutter throughout the listing, please give it a severe pruning and repost.

E.
Clip:
'GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG GPS SSSS GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
If str_done = 1 Then 'a valid packet has arrived
Hserout "Got it", CrLf, CrLf '2ND CRLF OK.
'For g = 0 To valid
'Hserout str1(g) 'G instead of i
'Next g
valid = 0
rxin = 0
str_done = 0
Endif
WaitMs 1
'_______________________________ SSSSSSSSSS _________________________________

Hserout " REMOTE=", "-", strtx1, "-", strtx2, "-", strtx3, "-", strtx4, "-", strtx5, CrLf
WaitMs 10
Hserout " T=", strtim, " N=", strlat, " W=", strlong, CrLf
'GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG

'SSSSSSSSSSSSSSSSSSSSSSSSSSS WRITE TO 5110 SCREEN SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
'Call clear() '??????????????????????????????????
'Gosub print
'SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
For x = 0 To 79 'CLEARS STR1
str1(x) = 0
Next x
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

INTCON.GIE = 1 '??????????????????????????????????????????

'IEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIE INCREMENTAL ENCODER IEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIE

#### camerart

Joined Feb 25, 2013
1,678
Hi J,

Q/ What do you mean by processes?

A/
Call clear() [ In the LOOP ]

[ PROCESS below END ]

Proc clear()
For yp = 0 To 5
Call clr_row(yp)
Next yp
End Proc
'clears all normal font row xp
Proc clr_row(yp As Byte)
lcd_cs = 0 'LCD CS ON
xp = 0
lcd_dc = 0 'COMMAND MODE
xp = xp * 6
-------------------------------------ETC

C.

#### camerart

Joined Feb 25, 2013
1,678
Hi C,
Started look at that text.
This sort of 'commenting' is OTT, there is a lot of this clutter throughout the listing, please give it a severe pruning and repost.

E.
Clip:
'GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG GPS SSSS GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
If str_done = 1 Then 'a valid packet has arrived
Hserout "Got it", CrLf, CrLf '2ND CRLF OK.
'For g = 0 To valid
'Hserout str1(g) 'G instead of i
'Next g
valid = 0
rxin = 0
str_done = 0
Endif
WaitMs 1
'_______________________________ SSSSSSSSSS _________________________________

Hserout " REMOTE=", "-", strtx1, "-", strtx2, "-", strtx3, "-", strtx4, "-", strtx5, CrLf
WaitMs 10
Hserout " T=", strtim, " N=", strlat, " W=", strlong, CrLf
'GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG

'SSSSSSSSSSSSSSSSSSSSSSSSSSS WRITE TO 5110 SCREEN SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
'Call clear() '??????????????????????????????????
'Gosub print
'SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
For x = 0 To 79 'CLEARS STR1
str1(x) = 0
Next x
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

INTCON.GIE = 1 '??????????????????????????????????????????

'IEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIE INCREMENTAL ENCODER IEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIE
Hi E,
I agree it is a mess, but it's the only way I can keep things in groups. I've spent months trying, then having to start again because I lost my way, until I started using this method.
Not to worry, I'll carry on and see what happens for a while, then post the result.
C.

#### jjw

Joined Dec 24, 2013
497
Hi J,

Q/ What do you mean by processes?

A/
Call clear() [ In the LOOP ]

[ PROCESS below END ]

Proc clear()
For yp = 0 To 5
Call clr_row(yp)
Next yp
End Proc
'clears all normal font row xp
Proc clr_row(yp As Byte)
lcd_cs = 0 'LCD CS ON
xp = 0
lcd_dc = 0 'COMMAND MODE
xp = xp * 6
-------------------------------------ETC

C.
It is a procedure, not a process.

#### dl324

Joined Mar 30, 2015
8,879
I agree it is a mess, but it's the only way I can keep things in groups. I've spent months trying, then having to start again because I lost my way, until I started using this method.
That resonates with me. I have chronic back pain from an auto accident and can't work on code for more than half an hour or so in one sitting and found that if I don't make notes to myself, I forget to do things and waste a lot of time tracking down dumb mistakes. Or I waste a lot of time analyzing the same problem repeatedly. Once I get a section of code working, I remove all extraneous notes.

If you adopt a convention for your "extra" comments, you can use a program to filter them before you show your code to others. Prefix them with some unique string, e.g. "'*ca* ...". Then you can use something like 'grep' to remove them.

#### camerart

Joined Feb 25, 2013
1,678
It is a procedure, not a process.
Hi J,
We live and learn don't we
I've changed the title.
C

Last edited:

#### camerart

Joined Feb 25, 2013
1,678
That resonates with me. I have chronic back pain from an auto accident and can't work on code for more than half an hour or so in one sitting and found that if I don't make notes to myself, I forget to do things and waste a lot of time tracking down dumb mistakes. Or I waste a lot of time analyzing the same problem repeatedly. Once I get a section of code working, I remove all extraneous notes.

If you adopt a convention for your "extra" comments, you can use a program to filter them before you show your code to others. Prefix them with some unique string, e.g. "'*ca* ...". Then you can use something like 'grep' to remove them.
Hi d,
I'm guessing that you use pain killers, which don't help the ol' brain box.

I hope it doesn't stop you programming. As I move forward, I add time to the title of the program, and save it, then if I get mixed up, I can skip back a few minutes.

In this particular case, each section, say COMPASS, has a SET_UP, a RUN, and a RESULT, and being in different areas, gets me completely mixed up. Using these silly comments, I am able to find them much easier. I think that now the program is settling down, I may be able to lessen the comments

I also type capitals for programming stuff, and lower for writing, e,g READ and read, which has helped me a lot.

Best of luck with your back and programming.
C

#### dl324

Joined Mar 30, 2015
8,879
I'm guessing that you use pain killers, which don't help the ol' brain box.
Unless pain gets very severe, I don't use prescription pain relievers because they all have adverse side effects. I usually try to tough it out and only use OTC pain medication to take the edge off so I can sleep. I can tolerate severe pain for things that don't require much time or concentration.
I hope it doesn't stop you programming.
It only impacts my productivity. Thanks for your concern.

#### camerart

Joined Feb 25, 2013
1,678
Hi,
I haven't finished changing the program, but a quick [ CALL ] test doesn't seem right!

If I RUN the program including this section:
---------------------------------------------------------------------------------
Call alt_setup()

main:
''If BAUDCON.RCIDL = 1 Then
'dataswitch = 1 '0=REMOTE 1=GPS {Set DATASWITCH once REMOTE is connected !!!!!!!)
'Toggle yled
'WaitMs 100
''Endif
'Goto main

'SPICSOn
'For i = 0 To 18 '5 'READ XYZ Xx2 Yx2 Zx2
'compss = 0 'CHIP SELECT COMPASS ON
'b(i) = data
'compss = 1 'CHIP SELECT COMPASS OFF
'Next i
'SPICSOff

'WRITE TO 5110 SCREEN
'Call clear() '???????
'Gosub print

Goto main
End

Symbol radset = LATD.3 'HC-12 0=COMMAND ON 1=DATA ON

'INITIALISATION
radset = 0 'SET HC-12 COMMAND ON
WaitMs 100
Hserout "AT+C002", CrLf '433.800
WaitMs 100
radset = 1 'SET HC-12 RUN ON
WaitMs 100

'RRRRRR
'AAAAA BMP280 ALTIMETER
Proc alt_setup()

Symbol altmtr = LATD.1 'BMP280 BAROMETER/TEMP
'BMP280 CALC
Dim var1 As Single
Dim var2 As Single
Dim t_fine As Single
Dim t_raw As Long
Dim p_raw As Long
-----------------------------------------------------------------------------
it doesn't compile with [ Variable not declared: 'VAR1' ]

It can be seen that [ Call alt_setup() ] is declared at the beginning, then just after [ Proc alt_setup() ] there is [ Dim var1 As Single ] which is before where the error is.

C

#### ericgibbs

Joined Jan 29, 2010
8,729
hi C,
Always place your Dims, Defines etc before the Main Coding, else the Compiler will not know the variables.
E

#### camerart

Joined Feb 25, 2013
1,678
hi C,
Always place your Dims, Defines etc before the Main Coding, else the Compiler will not know the variables.
E
Hi E,
Ok.
Am I correct that everything else can be a [ NOT!! PROCESS? ] but [ PROCEDURES ] e,g, Calculations, set-up, printing?

EDITED
C.

Last edited:

#### dl324

Joined Mar 30, 2015
8,879
Code:
''If BAUDCON.RCIDL = 1 Then
'dataswitch = 1 '0=REMOTE 1=GPS {Set DATASWITCH once REMOTE is connected !!!!!!!)
'Toggle yled
'WaitMs 100
''Endif
'Goto main

'SPICSOn
'For i = 0 To 18 '5 'READ XYZ Xx2 Yx2 Zx2
'compss = 0 'CHIP SELECT COMPASS ON
'b(i) = data
'compss = 1 'CHIP SELECT COMPASS OFF
'Next i
'SPICSOff

'WRITE TO 5110 SCREEN
'Call clear() '???????
'Gosub print
Is this supposed to be Basic/Visual Basic? If it is, why are you posting entire blocks of code that are commented out?

#### ericgibbs

Joined Jan 29, 2010
8,729
hi C,
I would suggest you do not call them a process.
They are Procedures [Proc] or Functions (Func], also there are Gosubs..

These types are usually used when a price of Code is frequently used in a program.
With Proc's and Func's, data can be included in the Call to the Proc or Func, the data is then processed by the code in the Proc or Func.

The main part of the program code is what determines when these Proc's Func's are Called.

E

#### camerart

Joined Feb 25, 2013
1,678
hi C,
I would suggest you do not call them a process.
They are Procedures [Proc] or Functions (Func], also there are Gosubs..

These types are usually used when a price of Code is frequently used in a program.
With Proc's and Func's, data can be included in the Call to the Proc or Func, the data is then processed by the code in the Proc or Func.

The main part of the program code is what determines when these Proc's Func's are Called.

E
Hi E,
I'm in the habit of calling them processes, I'll edit to PROCEDURES, and try to remember.
C

#### camerart

Joined Feb 25, 2013
1,678
Is this supposed to be Basic/Visual Basic? If it is, why are you posting entire blocks of code that are commented out?
Hi d,
This is Oshonsoft BASIC

I've just rearranged a whole program, and commented OUT the unready sections.
The commented IN lines are relevant to my question.
C