TP Feedback
jjames
Posts: 2,908
I was shown a way on how to shorten mainline code by using FOR loops, rather than the way taught/shown in P1 & P2.
P1/P2 Feedback:
FOR-loop Feedback:
Using the FOR loop way, you can go quite deep with nested FOR loops. I've been using FOR loops for feedback for quite some time, but am stumped with this one.
Here's how to do it the "P1/P2" way:
I know I would probably have to go two or three deep in a nested-FOR loop to achieve the same results, and for some reason - I just can't get it figured out.
Help please?
P1/P2 Feedback:
[dvTP,65]=(nCUR_PSET==1) [dvTP,66]=(nCUR_PSET==2) [dvTP,67]=(nCUR_PSET==3) [dvTP,68]=(nCUR_PSET==4) [dvTP,69]=(nCUR_PSET==5)
FOR-loop Feedback:
FOR(nLOOP=1;nLOOP<=LENGTH_ARRAY(PSET_BTNS);nLOOP++) [dvTP,PSET_BTNS[nLOOP]]=(nCUR_PSET=nLOOP)
Using the FOR loop way, you can go quite deep with nested FOR loops. I've been using FOR loops for feedback for quite some time, but am stumped with this one.
Here's how to do it the "P1/P2" way:
[dvTP,SRC_SEL_BTNS[1]] = (nCUR_SRC[1] = 1) [dvTP,SRC_SEL_BTNS[2]] = (nCUR_SRC[1] = 2) [dvTP,SRC_SEL_BTNS[3]] = (nCUR_SRC[2] = 1) [dvTP,SRC_SEL_BTNS[4]] = (nCUR_SRC[2] = 2) [dvTP,SRC_SEL_BTNS[5]] = (nCUR_SRC[3] = 1) [dvTP,SRC_SEL_BTNS[6]] = (nCUR_SRC[3] = 2)
I know I would probably have to go two or three deep in a nested-FOR loop to achieve the same results, and for some reason - I just can't get it figured out.
Help please?
0
Comments
The (nLoop + 1) / 2 will cause the index into nCUR_SRC to increment every two passes. And the 2 - (nLoop % 2) will compare against 1 on odd numbered passes, and 2 on even numbered passes.
Rob,
Great! Thank you! As most programmers, I'm naturally lazy, and the less typing the better.
I feel I'm almost to the point in my programming skill level that I'm getting confortable that my code works, so now it's a game to shorten the code. Almost like "Name That Tune" (I can name that tune in 3 notes) . . it's now "Write That Code" (I can do that in 2 lines). LOL
Thanks again.
A major problem I had before was that I would not comment my code, and that made it very difficult to read when I went back to see what's going on - especially when the first couple of jobs I was trying to find the "groove" of things and each job's code setup was different. So now I comment nearly everything and explain what's going on. This is especially true with my for loops in mainline. But anyway, like I said - I use A LOT of arrays, so FOR loops fit perfectly. And personally, if something changes in the code to where the FOR loop is out of whack, I'd rather spend the time and fix it than change one or two numbers in 20 to 30 lines of code.
Let's just hope no one has to service my code at a later date, because if they can't understand a feedback FOR loop, that should be the least of their worries on working with my code.
Here's an example: That's one line to a source selection feedback. There are arrays that are indexes to an index in there. The nCP_Count index is incremented in a FOR loop. I could have nested another FOR loop for the 99, [1] and SYSTEM_OFF (and other input values) as well, and got it in a single line, but instead opted to put a half dozen lines like that in my original loop just so I could take on look at it later and know exactly what was going on.
Ahh - I see what you're saying. I thought you meant something like the P1/P2 example I posted earlier. Sorry.
It runs faster without the FOR loop. So you can say the FOR loop method is more efficient coding-wise, but not execution-wise...
- Chip
I draw the line at:
IF (such and such) ON[feedback channel]
ELSE OFF[feedback channel]
FOR loops in DEFINE_PROGRAM are the creation of loops within a loop that run non-stop, not the most graceful programming technique. Nested loops should only run when system events dictate.
As someone who has been at this since well back into the Axcess days, Netlinx has really done wonders for my programming efficiencies.
[dvRelay, RELAY_AMP] = (bSystemState <> FALSE)
- costs you nothing.
I also don't use for loops for feedback but write out each discrete line, which is pretty quick with the ALT-lasso thing and sequential renumber. If for whatever reason that for loop didn't complete in one pass of mainline you'd have problems.
Could you elaborate on this?
--D
--D