You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

---------->>>>>Get FREE PLC Programming Tips

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

PLC training tools sale

Reply
 
Thread Tools Display Modes
Old February 9th, 2019, 12:07 AM   #1
dschmiddr
Member
United States

dschmiddr is offline
 
Join Date: Oct 2013
Location: Sacramento
Posts: 5
Structured Text Increment Integer

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.
  Reply With Quote
Old February 9th, 2019, 12:35 AM   #2
Jkoso
Member
Finland

Jkoso is offline
 
Join Date: Jan 2019
Location: Finland
Posts: 24
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 by Jkoso; February 9th, 2019 at 01:09 AM.
  Reply With Quote
Old February 9th, 2019, 01:10 AM   #3
JeremyM
Member
United States

JeremyM is offline
 
Join Date: May 2014
Location: Texas
Posts: 365
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 by JeremyM; February 9th, 2019 at 01:16 AM.
  Reply With Quote
Old February 9th, 2019, 02:53 AM   #4
BobB
Lifetime Supporting Member
Australia

BobB is offline
 
BobB's Avatar
 
Join Date: Jun 2002
Location: Sydney
Posts: 4,332
@ in ladder is good! LOL
__________________
The Old Pfhaart

  Reply With Quote
Old February 9th, 2019, 08:02 AM   #5
dschmiddr
Member
United States

dschmiddr is offline
 
Join Date: Oct 2013
Location: Sacramento
Posts: 5
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.
  Reply With Quote
Old February 9th, 2019, 08:05 AM   #6
L D[AR2,P#0.0]
Lifetime Supporting Member
United Kingdom

L D[AR2,P#0.0] is offline
 
Join Date: Nov 2006
Location: UK
Posts: 5,715
Post #2 gives you an alternative if there is no R_TRIG
  Reply With Quote
Old February 9th, 2019, 08:11 AM   #7
Steve Bailey
Lifetime Supporting Member + Moderator
United States

Steve Bailey is offline
 
Steve Bailey's Avatar
 
Join Date: Apr 2002
Location: The boondocks of Western Massachusetts USA
Posts: 6,839
if MachineState = 1 then
MachineStateMarker := 1

if ((MachineState = 0) and (MachineStateMarker = 1)) then
DownCount := DownCount + 1
MachineStateMarker := 0;
  Reply With Quote
Old February 9th, 2019, 05:11 PM   #8
Sergei Troizky
Member
Canada

Sergei Troizky is offline
 
Join Date: Oct 2004
Location: Montreal
Posts: 710
Quote:
Originally Posted by BobB View Post
@ in ladder is good! LOL
Fully agree! Never understood this masochism with ST.
  Reply With Quote
Old February 9th, 2019, 10:11 PM   #9
Peter Nachtwey
Member
United States

Peter Nachtwey is offline
 
Peter Nachtwey's Avatar
 
Join Date: Apr 2002
Location: Vancouver, WA, US
Posts: 6,922
Quote:
Originally Posted by Sergei Troizky View Post
Fully agree! Never understood this masochism with ST.
ST is great for motion control or applications that require math. The whole problem with one shots go away if ST is combined with a state machine like SFC.
__________________
"Living is easy with eyes closed, misunderstanding all you see...." Strawberry Fields Forever, John Lennon
  Reply With Quote
Old February 10th, 2019, 12:29 AM   #10
Jkoso
Member
Finland

Jkoso is offline
 
Join Date: Jan 2019
Location: Finland
Posts: 24
Basicly you can do powerfull code with minimal cycle time with ST if compare to ladder or fbd programming.

Last edited by Jkoso; February 10th, 2019 at 12:36 AM.
  Reply With Quote
Old February 10th, 2019, 12:48 PM   #11
Dirt
Member
Sweden

Dirt is offline
 
Join Date: Apr 2008
Location: Stockholm
Posts: 283
http://www.plctalk.net/qanda/showthread.php?t=119371
  Reply With Quote
Old February 10th, 2019, 02:24 PM   #12
Toine
Member
Netherlands

Toine is offline
 
Join Date: Nov 2016
Location: NL
Posts: 257
Quote:
Originally Posted by Sergei Troizky View Post
Fully agree! Never understood this masochism with ST.

Horses for courses.
If all you have is a hammer, the whole world looks like a nail.


In other words, there are problems and situations for which ST is better suited than ladder code.
  Reply With Quote
Old February 10th, 2019, 02:35 PM   #13
Lare
Member
Finland

Lare is offline
 
Join Date: Jan 2006
Location: Finland
Posts: 1,108
Quote:
Originally Posted by Sergei Troizky View Post
Fully agree! Never understood this masochism with ST.
Before you can understand which is easy you have to know which isn't
  Reply With Quote
Old February 10th, 2019, 02:52 PM   #14
BobB
Lifetime Supporting Member
Australia

BobB is offline
 
BobB's Avatar
 
Join Date: Jun 2002
Location: Sydney
Posts: 4,332
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.
__________________
The Old Pfhaart

  Reply With Quote
Old February 10th, 2019, 09:08 PM   #15
Ron Beaufort
Lifetime Supporting Member
United States

Ron Beaufort is offline
 
Ron Beaufort's Avatar
 
Join Date: Jul 2002
Location: Charleston, SC
Posts: 5,533
Quote:
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 ...
.
Attached Images
File Type: png osri_stx.PNG (31.4 KB, 88 views)
__________________

2-B ?
Best regards, ----+----] [----+------------( )----
Ron | |
PLC Training Boot Camp - Retired | 2-B |
+----]/[----+

I once was lost, but now am found, was blind, but now I see.


Last edited by Ron Beaufort; February 10th, 2019 at 09:36 PM.
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
TwinCAT Structured Text Programing (LOOP) Sohaib28 LIVE PLC Questions And Answers 5 November 19th, 2015 12:36 PM
Citect and Text display for Integer Value ? EICS_2 LIVE PLC Questions And Answers 3 August 8th, 2015 11:54 PM
CHANGE Structured Text to Ladder Diagram..! MODICON JLGARFER LIVE PLC Questions And Answers 4 June 29th, 2011 04:20 PM
Structured Text for automating experiments? I'm new... ChrisOfMaryland LIVE PLC Questions And Answers 1 November 4th, 2009 11:40 AM
Documentation on Structured Text cmulder LIVE PLC Questions And Answers 13 June 11th, 2003 10:10 AM


All times are GMT -5. The time now is 11:47 AM.


.