question on subroutines

maxwellwoody

Member
Join Date
Oct 2017
Location
brighton ,mi
Posts
10
hello all
is there a rule of thumb as to using or not using subroutines when writing a program ?
i am using an Allen Bradley slc 500
thank you much for do's and dont's
 
Technically, there is no rule. Practically, you want your program to be as organized as possible for ease of future edits. You use the subroutines as a way to break your program up into different bite-sized pieces. So if you're programming a conveyor system that just has a motor and a few sensors, you can do it all on one subroutine. But if you're programming an automation line that has an infeed, three separate steps of production, and then a packaging area, you would split each step up into it's own subroutine.

Personally, for each area of a machine I'm programming, I like to have an INPUTS routine that scans the actual hardware addresses for incoming information (sensors, proxes, button states, etc) and then mirrors those state changes into arrays. Then a CONTROL routine that runs those state changes through the logic using my arrays (not my actual hardware addresses) to decide what needs to change, and finally an OUTPUTS routine that changes the state of all my outgoing channels (motor starters, speed settings, light states, etc). You usually also have FAULTS and ALARMS on their own subroutines, and if you have an HMI that needs logic, that usually has it's own as well.
 
Last edited:
I break up the program into subroutines (sections)

if a machine has 10 sections of different operations, break it up that way.
assign different b,t,c,r,n,f files.

if a machine has 10 stations that all do the sane thing, write the code for station 1 and then copy the program to a library file.
import the file into station 2 using index file structure.
repeat for stations 3-10.
that way, if you know station 1, you know all the others.
b3 - general purpose
b13 - station 1
b23 - station 2
b103 - station 10

makes life easier when you find an error in one station and then change the others.
if a machine has multiple test fixtures, have a subroutine for each test fixture.
I had a machine with 32 possibilities. they were easy to debug based on the fixture number.

regards,
james
 
thanks to LoganB and James
for info, the program seems to be jumping allover with rungs added to make the machine continue when those parts and pieces aren't even there.
thanks again
 
What type of machine is this?

from your description, the machine was built for part a and then parts b,c,d... were added or some variation.

that was my issue with a machine that they added different fixtures to it.
each fixture ran different parts and the program was getting complicated.
that's why I added subroutines for each fixture. it was so much easier when you had a problem, look at that subroutine. downtime went down a lot.

james
 
As Logan stated: technically there is no rule.

My personal rule of thumb is recurrence. A good programmer is inherently lazy: you don't want to write the same code twice. If I run into a second instance where I need code that I have already written: that is a flag to put this into a subroutine. Reasons for using subroutines are plenty. Among these are: code once, debug once, modify once.

Future modifications are a sure source for hard to find bugs if you have copied/pasted code instead of using a subroutine. You are going to fix one instance, forget about the other(s). Now there are three things in life that you can be sure of as a programmer: death, tax and future modifications. Write your code with future modifications in mind. The other two don't belong on this forum.
 

Similar Topics

Hi! I'm structuring my ladder program in multiple files. The program "talks" with an IMC 110 motion control module. For example, I've...
Replies
5
Views
5,996
Hi everyone, I am fairly new to programming subroutines and interrupts. I am used to dumping evrything on the main program. But with the Siemens...
Replies
6
Views
8,905
Hello again..trying something on an existing poorly written program and just wanted to double check something system is an A-B MicroLogix 1200 In...
Replies
5
Views
159
Good morning! Let me start by saying, I am still learning about this type of HMI programming. I recently watched a video about recipes and how it...
Replies
0
Views
62
I have some logic that I have written within a 5380 series controller that tracks the time an event is started, while the event is running an RTO...
Replies
2
Views
87
Back
Top Bottom