Count True BOOL in Array, M340 PLC, with Unity Pro.

Nablar

Member
Join Date
May 2016
Location
Oslo
Posts
8
I am trying to make a function that count the number of True BOOL inn a specific Array and return the count to an INT AlarmArrayActiveAlarms.
I have tried the following, but the INT AlarmArrayActiveAlarms just keeps counting forever if one BOOL is True.

Any ideas what I am doing wrong? in this case i would expect the INT to return 3.


I am using a Modicon M340 PLC, with Unity Pro.



Code:
AlarmTrigger: ARRAY[0..200] OF BOOL
AlarmArrayCounter :INT
AlarmArrayActiveAlarms: INT

AlarmTrigger[73] := true;
AlarmTrigger[74] := true;
AlarmTrigger[75] := true;


FOR AlarmArrayCounter:=0 TO 200 DO

	IF AlarmTrigger[AlarmArrayCounter] = True THEN 
		AlarmArrayActiveAlarms := AlarmArrayActiveAlarms + 1;
	END_IF;

END_FOR;
 
Been a while since I did ST in M340's but are you not trying to compare a Boolean with an array of INTs? + it will keep counting as true if this is the case on every scan as you have loaded the Alarm Triggers as true (for test purposes?)
above the Loop.

Each Scan will see
AlarmTrigger[73] := true;
AlarmTrigger[74] := true;
AlarmTrigger[75] := true;
Then execute the loop

I think........
 
I never used that PLC but...

a) You need to be sure that AlarmArrayActiveAlarms is being initialized to zero before running the FOR loop.

b) I'd use an auxiliar variable to count the alarms and copy the final value to AlarmArrayActiveAlarms.
 
Been a while since I did ST in M340's but are you not trying to compare a Boolean with an array of INTs? + it will keep counting as true if this is the case on every scan as you have loaded the Alarm Triggers as true (for test purposes?)
above the Loop.

Each Scan will see
AlarmTrigger[73] := true;
AlarmTrigger[74] := true;
AlarmTrigger[75] := true;
Then execute the loop

I think........

I'm trying to compare the bool individual, and count the one that are True.
 
I am trying to make a function that count the number of True BOOL inn a specific Array and return the count to an INT AlarmArrayActiveAlarms.
I have tried the following, but the INT AlarmArrayActiveAlarms just keeps counting forever if one BOOL is True.

Any ideas what I am doing wrong? in this case i would expect the INT to return 3.


I am using a Modicon M340 PLC, with Unity Pro.



Code:
AlarmTrigger: ARRAY[0..200] OF BOOL
AlarmArrayCounter :INT
AlarmArrayActiveAlarms: INT

AlarmTrigger[73] := true;
AlarmTrigger[74] := true;
AlarmTrigger[75] := true;


FOR AlarmArrayCounter:=0 TO 200 DO

    IF AlarmTrigger[AlarmArrayCounter] = True THEN 
        AlarmArrayActiveAlarms := AlarmArrayActiveAlarms + 1;
    END_IF;

END_FOR;


You need to zero counter before loop to zero, otherwise it will increase from old value on next scan.


AlarmTrigger: ARRAY[0..200] OF BOOL
AlarmArrayCounter :INT
AlarmArrayActiveAlarmsCnt: INT
AlarmArrayActiveAlarms: INT

AlarmTrigger[73] := true;
AlarmTrigger[74] := true;
AlarmTrigger[75] := true;


AlarmArrayActiveAlarmsCnt:=0

FOR AlarmArrayCounter:=0 TO 200 DO

IF
AlarmTrigger[AlarmArrayCounter] = True THEN AlarmArrayActiveAlarms_cnt :=AlarmArrayActiveAlarmsCnt+1;
END_IF;

END_FOR;

AlarmArrayActiveAlarms :=AlarmArrayActiveAlarmsCnt;
 
Last edited:
AlarmArrayActiveAlarmsCnt:=0

FOR AlarmArrayCounter:=0 TO 200 DO

IF
AlarmTrigger[AlarmArrayCounter] = True THEN AlarmArrayActiveAlarms_cnt :=AlarmArrayActiveAlarmsCnt+1;
END_IF;

END_FOR;

AlarmArrayActiveAlarms :=AlarmArrayActiveAlarmsCnt;

Don't think you need the IF....THEN...END_IF construct, just add the binary 0 or 1 of the triggers to the counter....

AlarmArrayActiveAlarmsCnt:=0

FOR AlarmArrayCounter:=0 TO 200 DO
AlarmArrayActiveAlarms_cnt := AlarmArrayActiveAlarmsCnt + AlarmTrigger[AlarmArrayCounter];
END_FOR;

AlarmArrayActiveAlarms := AlarmArrayActiveAlarmsCnt
 
Don't think you need the IF....THEN...END_IF construct, just add the binary 0 or 1 of the triggers to the counter....

AlarmArrayActiveAlarmsCnt:=0

FOR AlarmArrayCounter:=0 TO 200 DO
AlarmArrayActiveAlarms_cnt := AlarmArrayActiveAlarmsCnt + AlarmTrigger[AlarmArrayCounter];
END_FOR;

AlarmArrayActiveAlarms := AlarmArrayActiveAlarmsCnt

That is true, didn't thinked yesterday.

Converting bool array to int_array 0..100 would give possibility to use Occur_array-block.
Of course it would take more program memory, but on M340 case there is plenty
 
Last edited:

Similar Topics

I have an array of 100 DINTS and I need to count the BITS that are True. I tried a nested FOR in Structured Text but it wouldn't allow the second...
Replies
40
Views
12,223
I am needing to count the number of true bits that are in a file. They start at B34/1 and end at B34/56. All I am trying to do is count how many...
Replies
19
Views
8,324
What is the raw count of Allen Bradley Flex Analog Output Module 5094-OF8 raw count?
Replies
1
Views
126
I am working on a Markem X40 printer which uses NGPCL Commands and needs to send commands using a .net program. I need help with the following...
Replies
1
Views
143
Hi, I'm programming in RSLogix 500, and I'm wondering how I would program a Jog command that does not increase the encoder count. Basically we'd...
Replies
3
Views
302
Back
Top Bottom