reduse code

hapetter

Member
Join Date
Feb 2009
Location
Kristiansand
Posts
58
Hi,

I want to make a dynamic if sentence. This is my code:

IF i = 4 OR i = 7 OR i = 10 OR i = 13 OR i = 16 OR i = 19 OR

So I like to make this more compact and such that I only specify the last number in if sentence. So there must be a way to use the repeating pattern to make this more compact and dynamic? Any tip?

Thanks
 
In which language are you writing the code?

If it's in C,C++ or Java, and if I remember the syntax correctly you can use a "for loop" like this(assuming last number is 19):

Code:
for(i=4; i <= 19; i + 3 )
  { // Write the code that you want to run conditionally here
  }
 
You dont write what PLC or programming language.

Many programming languages allow to make CASE statements with single values, a range of values or a series of values as selector.

like this:

CASE parameter OF
1 : ...... // single value
2,3,8 : ....... // series
10..20 : ...... // range
 
You could use the following function to perform the dynamic if

Code:
FUNCTION FC789:BOOL
//dynamic if statement
VAR_INPUT
iVar:INT;
iForstart:INT;
iForStep:INT;
iForEnd:INT;
END_VAR
VAR_TEMP
iIndex:INT;
END_VAR
BEGIN
FC789:=false;
FOR iIndex:=iForstart TO iForEnd BY iForStep DO
 IF iIndex=iVar THEN
  FC789:=True;
 END_IF;
end_for;
END_FUNCTION;
 

Similar Topics

I'm working on learning OOP coding for TwinCat 3. I'd like to transfer and optimize the code my company has from AB Studio 5000 to ST TwinCat 3...
Replies
1
Views
95
in allen bradley kinetix 300 drive first E31 error shows after resting drive E11 error occurs need solution to reset E11 fault code
Replies
4
Views
160
Hello, I am trying to read a barcode scanner input using a cognex dataman 280 barcode reader, store it another string, the compare with another...
Replies
1
Views
101
Hi there, I'm new to plc programming and was wondering why I get this error code when I run my simulation for these temperature sensors? What I'm...
Replies
2
Views
104
Hi All, Someone at work has put a PLC system on my desk, that's just been taken off an idle production line. He said "It's an S7 PLC. We don't...
Replies
10
Views
322
Back
Top Bottom