Lots of ways to skin this cat (sorry, Pierre)
WhatThe said:
About the suggestion to add 1 for every line in service. You would
then need to subtract 1 for every line taken out of service and reset to zero when no lines are in service.
And a SUM Function would be sweet.
I think you missed a critical piece of Jimbo's code. The whole thing is in a single rung with many branches.
The first branch clears the "Service Counter". Then there is one branch for each line that just adds one if it is "in service", or "running" or whatever the condition you need to count is.
There is no need to "subtract 1 for every line taken out of service" because the "Service Counter" register is cleared each scan, and each scan it gets recalculated based on what's
current.
This
IS a SUM function - it's just handmade instead of pre-canned.
If you are using a PLC-5/40 (and not a SLC 5/04), and you want a SUM function, then use an FAL, like so:
CLR N7:0
FAL R6:0 10 0 ALL N7:0 (N7:0 + #N7:1)
Note the '#' sign in front of the N7:1 in the equation, but not with the N7:0 (and that N7:0 is the destination as well as part of the equation).
What the above code will do is add N7:1 + N7:2 + N7:3 + ... + N7:10 and store the result in N7:0. It's not as good as counting bits (unless, of course, that you only use the /0 bit in N7:x), but it could do what you want.
Ken's idea is another techique youi could use, if you have the bits consecutive in a single word (and don't use any other bits for other purposes).
If the value of that word is zero, you know you have no lines available.
If the value of that word is exactly one, then you know that only line #1 is available, and no other. If the value of that word is exactly two, then only line #2 is available.
Taking it further, if the value is either 1, 2, 4, 8, 16, 32, 64, 128, 256, or 512, then
one and only one line is available. So anything other than zero or those numbers must mean that you have "2 or more".
So you program the individual BITS with coils to indicate the status, but you do a compare on the WORD to determine the overall status.