Structured Text Increment Integer

dschmiddr

Member
Join Date
Nov 2013
Location
Sacramento
Posts
6
If I have an integer and I want it to increment one value every time a condition becomes true then how can I write that logic in structured text. For example

if MachineState = 0 then
DownCount := DownCount + 1;

But when I run this code the DownCount increments by one every scan. I can't get the logic to work. Surely this is a common scenario and I would appreciate anybody's help. Thank you.
 
I would do it like this using r_trig:

Code:
fbMachineStateR_TRIG(CLK:=bMachineState, Q=>,);

IF fbMachineStateR_TRIG.Q THEN
     
    DownCount := DownCount + 1;
     
END_IF

or without r_trig:

Code:
IF NOT bOldMachineState AND bMachineState THEN

    DownCount := DownCount + 1;

END_IF

bOldMachineState:=bMachineState;

On your code it counts all the time when MachineState condition is 0, that means calculation is done everey cycle if that condition is 0
 
Last edited:
Using one-shot (bool) logic is how I typically approach this:
Code:
if ( MachineState == 0 && !OneShot )
{
   DownCount = DownCount + 1;
   // other code
}
OneShot = ( MachineState == 0 );
This example is C++ syntax but you can easily convert it to ST.

Obviously you won't be able to use the same 'OneShot' reliably elsewhere in your code, so you could declare a BOOL array or DINT (using its bits as individual one-shots) to handle multiple state changes, for example.
 
Last edited:
Increment Integer with Studio 5000 (AB) in ST

Hello,

Everybody's replies have been great. Is there anything to consider if the it is an AB ControlLogix controller. For example the R_TRIG instruction is not available in AB.
 
Basicly you can do powerfull code with minimal cycle time with ST if compare to ladder or fbd programming.
 
Last edited:
Ha - the ST/LD debate goes on. Yes ST is good for a lot of things but LD is a lot quicker DEPENDING on the software you are using. Mostly use Omron CX-Programmer and program with the keyboard and just numbers (no I or Q either) and it is really fast. Doing a job with Unity Pro XL at the moment and it is not set up for ladder and the use of numbers at all - only symbols. Lots of ST and taking twice as long as with Omron. That being said a lot of maths are much easier in ST than LD but can still be painful. I do not do motion at this point so have no knowledge of the good and bad there. For an increment @INC D2500 instruction is just so simple it is not funny. I might also add that mapping data to memory areas in Unity Pro for BMS and the like to extract via Modbus TCP is so easy in Unity Pro it is not funny - lots of it to do and just do in Excel and import. Horses for courses. Pluses and minuses everywhere - depends what you are doing.
 
Is there anything to consider if the it is an AB ControlLogix controller.

personally I think that Structured Text is more trouble than it's worth in this situation - but the following ideas might prove useful to you - assuming that you're interesting in something along the lines of the Allen-Bradley "book" way of doing it ...

one way of looking at the "One Shot Rising with Input" instruction - is that the Structured Text syntax is based on the syntax for a Function Block Diagram version - but - with STX you don't get the "picture" of the instruction ...
.

osri_stx.PNG
 
Last edited:

Similar Topics

Good morning. I'm doing a rehab and I need to recycle some part of the old code that won't change and that I need. This is a calculation that...
Replies
22
Views
1,170
I'm writing some structured text that's handling a data structure that comes from a PC. The PC structure is in the "new" LREAL 64-bit floating...
Replies
3
Views
440
Trying to put these equations in structured text program in a Micro 830. when I run the verify I get this error. ;:expected before this new...
Replies
4
Views
395
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,422
Hi, In our company (programming mainly Beckhoff PLCs) we are establishing internal coding guidelines for ST. Until now, the conventions were...
Replies
5
Views
1,018
Back
Top Bottom