for loop in structured text

dotolee

Member
Join Date
Jul 2010
Location
TO
Posts
20
I have to write some rslogix 5000 code that will loop through an array, copy each element in the array one at a time to a separate tag and set a bit to true. Then i want to start a timer. when the timer is done, i want to set the bit to false.

the problem is that the loop that i have is incrementing but the code inside is not executing once for each time the loop counter increases.

the code looks like this:
Code:
for iLoopCounter:=0 to 20 do   //iloopCounter is an int
       COP(arrayOfStrings[iLoopCounter], singleString, 1);
       startBit:=1;
       testTimer.reset:=0;
       testTimer.acc:=3000;
       testTimer.DN:=0;
       testTimer.TimerEnable:=1;
       tonr(testTimer);  //start the timer.
       if testTimer.DN then
             startBit:=0;
       end_if;
end_for;
 
Why do you think that it is not excecuting? I dont get your timer usage tough. What you want to achieve with it?

You should not set testTimer.DN:=0. That bit should not be touched as it is output of timer.

Now you set the timer 20 times in a one scan, you even excecute it 20 in one scan, its done after three seconds has passed. That is many scans...

Whole FOR loop is executed in one scan!
 
Last edited:
hi
i also think you there is something wrong with the understanding of plc programming.

; following text is not a code but should give a hint how this should be programmed....

Code:
; if startcondition is true 

if startflag = true then

   loop x = 0 to 20 - 1 do
      copy ()
      setflag
   endloop
   
   timer = start the timer
   startflag = false

endif

; check if time passed... if yes do some action

if time = done then
   loop x = 0 to 20 - 1 do
      copy ()    ; copy things after timer elapsed
      clrflag    ; do something else
   endloop
   reset timer   ; maybe
endif

I hope this is helping you somehow...

br
bb
 
Why do you think that it is not excecuting? I dont get your timer usage tough. What you want to achieve with it?

You should not set testTimer.DN:=0. That bit should not be touched as it is output of timer.

Now you set the timer 20 times in a one scan, you even excecute it 20 in one scan, its done after three seconds has passed. That is many scans...

Whole FOR loop is executed in one scan!

the timer code was provided to me by rockwell's tech support folks.
essentially, what i need to do is: copy value from array to a string, set a bit to high, start a timer. when timer is done, set bit to 0. then start again. i need to do this 20 times. i think i chose a for loop because of my pc programming background. but perhaps what i need is a count up. how would i implement this using a count up? in structured text ...
 
Booting up my software box, but doesn't time.acc = 3000 seem weird?

I thought you'd be setting the preset.
 
Booting up my software box, but doesn't time.acc = 3000 seem weird?

I thought you'd be setting the preset.
yep. but give it a try. it still works. mind u, the girl i got on tech support took 20 minutes to answer my question and wasnt really comfie with structured text.
 
the timer code was provided to me by rockwell's tech support folks.
essentially, what i need to do is: copy value from array to a string, set a bit to high, start a timer. when timer is done, set bit to 0. then start again. i need to do this 20 times. i think i chose a for loop because of my pc programming background. but perhaps what i need is a count up. how would i implement this using a count up? in structured text ...

PLC is NOT like PC!

You need to learn the basics first. Really, get to some course that teaches plc basics. And watch sample lessons in here:

http://www.ronbeaufort.com/sample_lessons.htm
 
yes, i realize they're not the same. no time to get courses right now but i have / will be looking around for some books. just wanted a quick answer to my structured text question. thanks anyways.
 
if iLoopCounter > 20 iLoopCounter = 0

if timer is done
start bit = 0

if(!startbit)
startbit = 1
copy stuff
iLoopCounter++;
set timer costraints
start timer
end if



I think the above pseudo will work better than what you provided because it wont cause lockups in execution.

Once in that form it may simplify it for you.
 
Have a look at the instruction help file of tonr. Right down the very bottom of the page is the structured text example.
 
Have a look at the instruction help file of tonr. Right down the very bottom of the page is the structured text example.

honga, i gave that a try. that's what i started off with actually. but using that example, the DN bit was never true. so that's why i ended up calling support. in any case, i will review the pseudo code you've posted.
thanks for the help.
 
You should maybe post what you had originally, would probably make more sense to me aswell lol.

I'm a c/assembly programmer naturally so this stuff is fun compared to ladder.
 
Last edited:

Similar Topics

Hey all, Studio v20.05 Background. We are piggy backing another system to record attributes for product travelling down the line and several...
Replies
21
Views
3,488
I am trying to learn structured text on the controllogix platform and have managed to fault the processor by creating an infinite loop. My problem...
Replies
21
Views
3,683
In structured text, can you decrement in a for loop? I need to shift an array and I can either use while loop or if decrement in for loop works...
Replies
6
Views
4,951
Hi guys, I have a basic problem...when the start relay is activated...I want my derived function block to load a set of values to an array...
Replies
8
Views
2,874
Hello Everyone: I have been able to read value via canopen interface using ADS protocol. Actually my Structured Text Programming is weak. I can...
Replies
5
Views
3,979
Back
Top Bottom