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

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
250
hello, I'm a student and for my final grade I have to repare a sepro robot. the only problem I have for now is that i have an error code 3...
Replies
0
Views
37
I received an email from a student with the following code attached. This is supposed to control a floodgate system, and supposed to be written...
Replies
23
Views
782
I have a machine which is undergoing upgradation. As part of the process two SEW drives are being replaced., existing Gen B with new Gen C. The...
Replies
3
Views
199
I want to set user security level based on the value of a tag in my PLC called "ActiveUser". That tag will contain the A-P code which we use for...
Replies
6
Views
216
Back
Top Bottom