Determine which bool array element is TRUE

Here is the result:

Code:
// Loop for checking active alarm and transfer alarm number
#AlarmActive := FALSE;
FOR #I := 0 TO 199 DO
    IF "SCADA_LiveData".Alarm_Status[#I] = TRUE THEN
        "SCADA_ExtendedData".ErrorCode := "SCADA_Ext".AlarmNumber[#I];
        #AlarmActive := TRUE;
        EXIT;
    END_IF;
END_FOR;

IF NOT #AlarmActive THEN
    "SCADA_ExtendedData".ErrorCode := 0;
END_IF;

Thanks guys, appreciate it. :)
 
Simplify, clarify:
Code:
// Loop for checking active alarm and transfer alarm number
FOR #I := 0 TO 199 DO
    IF "SCADA_LiveData".Alarm_Status[#I] THEN
        //>>>#AlarmActive := TRUE;
        "SCADA_ExtendedData".ErrorCode := "SCADA_Ext".AlarmNumber[#I];
        RETURN;
    END_IF;
END_FOR;

//>>>#AlarmActive := FALSE;
"SCADA_ExtendedData".ErrorCode := 0;
I am assuming #AlarmActive is a routine-local variable; if #AlarmActive is a VAR_OUT, then uncomment the //>>> statements.

Also, I would worry about if there might be an alarm number 0 that could be confused in that .ErrorCode when no alarms are active.
 
Simplify, clarify:
Code:
// Loop for checking active alarm and transfer alarm number
FOR #I := 0 TO 199 DO
    IF "SCADA_LiveData".Alarm_Status[#I] THEN
        //>>>#AlarmActive := TRUE;
        "SCADA_ExtendedData".ErrorCode := "SCADA_Ext".AlarmNumber[#I];
        RETURN;
    END_IF;
END_FOR;

//>>>#AlarmActive := FALSE;
"SCADA_ExtendedData".ErrorCode := 0;
I am assuming #AlarmActive is a routine-local variable; if #AlarmActive is a VAR_OUT, then uncomment the //>>> statements.

Also, I would worry about if there might be an alarm number 0 that could be confused in that .ErrorCode when no alarms are active.
Just to clarify for @swedeleaner, if alarm boolean 0 is active, "SCADA_ExtendedData".ErrorCode will be 0. This may appear as if no alarm is active, yet in reality there is an alarm active.

To work around this, I'd set "SCADA_ExtendedData".ErrorCode to a set number out of range of 0-199 (like -1 or 999 or something) when no alarm is active. You can do this if the loop completes without finding a valid alarm or just by initializing "SCADA_ExtendedData".ErrorCode at the start of the loop every time the loop scans. Be sure to add a comment so if someone increases the size of the alarm array they don't make a mistake and treat the no-alarm value as an alarm value.
 
Just to clarify for @swedeleaner, if alarm boolean 0 is active, "SCADA_ExtendedData".ErrorCode will be 0. This may appear as if no alarm is active, yet in reality there is an alarm active.

To work around this, I'd set "SCADA_ExtendedData".ErrorCode to a set number out of range of 0-199 (like -1 or 999 or something) when no alarm is active. You can do this if the loop completes without finding a valid alarm or just by initializing "SCADA_ExtendedData".ErrorCode at the start of the loop every time the loop scans. Be sure to add a comment so if someone increases the size of the alarm array they don't make a mistake and treat the no-alarm value as an alarm value.
Thanks for looking out for me! Array element 0 is not used for alarms, so I it will work in this case. But I agree that it probably looks better if I take an out of range value for clarity. Thanks again!
 
Simplify, clarify:
Code:
// Loop for checking active alarm and transfer alarm number
FOR #I := 0 TO 199 DO
    IF "SCADA_LiveData".Alarm_Status[#I] THEN
        //>>>#AlarmActive := TRUE;
        "SCADA_ExtendedData".ErrorCode := "SCADA_Ext".AlarmNumber[#I];
        RETURN;
    END_IF;
END_FOR;

//>>>#AlarmActive := FALSE;
"SCADA_ExtendedData".ErrorCode := 0;
I am assuming #AlarmActive is a routine-local variable; if #AlarmActive is a VAR_OUT, then uncomment the //>>> statements.

Also, I would worry about if there might be an alarm number 0 that could be confused in that .ErrorCode when no alarms are active.
I’ve never used return, but there is always a first time I guess. I would call it minimalistic, not simpler. ?
 
That is clearer (no IF NOT ... requiring reader to run code with multiple branches in their head).

This is minimalist:
Code:
"SCADA_ExtendedData".ErrorCode := 0;
WHILE #I < 200 AND "SCADA_LiveData".Alarm_Status[#I] DO #I := #I + 1; END_WHILE;
IF #I < 200 THEN "SCADA_ExtendedData".ErrorCode := "SCADA_Ext".AlarmNumber[#I]; END_IF;
 
I don’t think it’s enough to just use a bit per alarm. I also think the OP will discover this the moment they need to think about acknowledgement, suppression of nuisance alarms, and other bookkeeping.

Allocating a status DWORD per alarm (itself a struct), then allocating alarms in groups (arrays of struct), and finally having each alarm OR its status during a single scan cycle to a shared DWORD among all alarms is a lot more robust.
 

Similar Topics

Hello fellow PLC enthusiasts! I work at a relatively small company that has 30 or so industrial machines and I have been tasked with getting a...
6
Replies
1,015
Views
Hello guys, completely noob in the Rockwell/Allen-Bradley ecosystem here. I'm setting up to experiment in the future with different redundancy...
6
Replies
3,764
Views
Hi, I have been asked to head out to a number of sites to upload programs from a number of Rockwell Controllers (L24ER, L30ER, L5x, L6x, L7x...
8
Replies
3,696
Views
Hello, AVEVA OI system drivers such as ABCIP and MBTCP provide a discrete register called $SYS$STATUS, which can be tied to a Discrete tag in...
4
Replies
2,610
Views
We had a site where a rat gnawed through some fiber optic cables which (obviously) prevented communication to a remote rack entirely. We, and the...
6
Replies
1,892
Views
Top