I know I am not looping this right!?!

davatv223

Member
Join Date
Feb 2013
Location
SD
Posts
14
Newbie here Trying to do ST in RSlogix5000. I am wondering what I am doing wrong

I want to loop through the array and see if the name exists. Any help is appreciated.

for i := 0 to maxNames - 1 do
if Name<> ListofNamesArray then
NameIsListed :=1;
else
NameIsListed :=0;;
end_if;
end_for;
 
I don't know anything on the syntax of structured text in Logix but you need increment your pointer by 1 after the end if statement.

for i := 0 to maxNames - 1 do
if Name<> ListofNamesArray then
NameIsListed :=1;
else
NameIsListed :=0;;
end_if;
i := i+1
end_for;
 
you're right, I forgot to add that in. My original code does have that thought. :D

I don't know anything on the syntax of structured text in Logix but you need increment your pointer by 1 after the end if statement.

for i := 0 to maxNames - 1 do
if Name<> ListofNamesArray then
NameIsListed :=1;
else
NameIsListed :=0;;
end_if;
i := i+1
end_for;
 
OK, so you have checked the basics?
Tag data types are correct? A valid value in "maxNames". Are you calling the routine correctly from the parent routine? Have you tried using true and false to replace the 1 and 0 values?
Again, I know nothing of ST. Someone will be along soon that can help better...
 
In the Rockwell manual they have this:

SIZE(Inventory,0,Inventory_Items);
For position:=0 to Inventory_Items - 1 do
If Barcode = Inventory[position].ID then
Quantity := Inventory[position].Qty;
Exit;
End_if;
End_for
 
Where Size is an UDT which I am not using , I think this maybe my issue. I am going to read up on UDT's
 
I found your manual. Using UDT's won't help. You don't need to manually increment the loop. It will do that for you. Sorry I can't help you anymore than I have tried.
 
no worries, I'll keep on chiggin along. Thanks though (y)(y)

I found your manual. Using UDT's won't help. You don't need to manually increment the loop. It will do that for you. Sorry I can't help you anymore than I have tried.
 
Never done ST but it seems your loop will check all Names in ListofNamesArry. So if it does find a match part way through and set NameIsListed to 0 then the next name in the array it checks that does not match will change NameIsListed to 1. Unless the Name your looking for is the last one in the array I don't think this will work.

Try something like this:
NameIsListed :=1;
for i := 0 to maxNames - 1 do
if Name= ListofNamesArray then NameIsListed :=0;
end_for;

Another variation would be to exit the loop after finding a match. No sense in continuing to loop and check if you already know the name is in the array.

Jerry
 
Newbie here Trying to do ST in RSlogix5000. I am wondering what I am doing wrong

I want to loop through the array and see if the name exists. Any help is appreciated.
Code:
for i := 0 to maxNames - 1 do 
    if Name<> ListofNamesArray[i] then
        NameIsListed :=1;
    else
        NameIsListed :=0;;
    end_if;
end_for;
That code looks syntactically correct.
What do you think is wrong?
BTW, FOR loops automatically adds 1 to I. The i:=i+1; is not required or right.

You can pretty print your code in a post by inclosing it in code blocks similar to quote blocks.

As noted above, if you are only searching for a match then I would use a BREAK; to stop the loop from searching further after a match is found.
 
Newbie here Trying to do ST in RSlogix5000. I am wondering what I am doing wrong

I want to loop through the array and see if the name exists. Any help is appreciated.

Code:
for i := 0 to maxNames - 1 do 
   if Name<> ListofNamesArray[i] then
      NameIsListed :=1;
   else
      NameIsListed :=0;;
   end_if;
end_for;

I think that NameIsListed will only be true after your for loop if the name you are looking for is the last name in the array. Otherwise NameIsListed will only be true for one iteration of the for loop. If you put do as Jerry & Peter suggested and add a "break;" after the "NamesIsListed := 1;" I think it should work.
 
As Davatv223 indicated in his posted code:

SIZE(Inventory,0,Inventory_Items);
For position:=0 to Inventory_Items - 1 do
If Barcode = Inventory[position].ID then
Quantity := Inventory[position].Qty;
Exit;
End_if;
End_for
the EXIT or Break is going to be needed!
 
JerryH has the right idea, but I would do it in the opposite direction. This issue is you don't want the 'else' case to mess with you. So don't use it. Initialize NameIsListed to whatever value is the "not found" value before the FOR loop. Inside the FOR loop only set NameIsListed if the name is found. Since you were initialized for the not found case prior to the FOR loop you don't need to explicitly set the not found case after every compare.

Exiting the loop after the first occurrence of a find is a good idea from a code efficiency standpoint but it shouldn't be required for the code to work.

Keith
 
Got it working. Here it is. Thanks for the help

Code:
NameisListed :=0;
NameisNotListed :=0;

for i := 0 to maxNames - 1 do 
if currentName<> ApprovedNames_Array[i] then
NameisNotListed :=1;
NameisListed :=0;
else
NameisNotListed :=0;
NameisListed :=1;
exit;
end_if;
end_for;

Nice input Guys!!! I will try it soon and post up the results.
 

Similar Topics

Hi, I am new to ladder logic. I have completed a code in the Xinje PLC XC3-32RT-E for one of the machines I am developing. Currently the program...
Replies
30
Views
1,050
Hello, I am programming a ladder routine that finds a part on a conveyor that has a given part number. I am having no problem finding the part and...
Replies
1
Views
1,911
I've got several ListBoxes that I want to perform the same function on. From my searches, you can accomplish this in VBA by using a FOR loop, but...
Replies
4
Views
2,902
Hello everyone, I am new to ladder logic and am currently working on a small project where I might need some help. So I am modifying the sequence...
Replies
9
Views
2,448
Hello, This is my very first post here - I'm working on a small brewery project with multiple solenoid valves. The instructions follow a set of...
Replies
2
Views
1,753
Back
Top Bottom