Andrea Ascani

sequences programming in ladder

10 posts in this topic

Hi guys, I'm new in the forum and I'm quite new also in Sysmac programming, in ladder programming at all, to be honest.


I have some years of experience in programming CNC machines using C, on a owner platform. most of the functions I was using there were developed using "step sequences order".

I mean, I had functions continuosly running where I was able to control the steps,  using the switch/case contstruct, and pass to a step to another just changing the reference variable.

To be more clear, something like this:


functionStep = 0;

switch (functionStep)

  case 0:

       if !(somethingToDoHereIsDone())  


      functionStep = 10;

  case 10:


and so on.


In the Sysmac programs I was able to see, something like this is possible to perform using register and AryShiftReg function.

but I find it's quite complicated to manage if I want to insert intermediate steps in a second moment and most of all let an external part of the program to know in which "logical step" I am in my function.


Can you suggest me, in your experience, how can a sequencial function can be defined in Sysmac?


thanks a lot for your help.







Share this post

Link to post
Share on other sites

There are a number of ways to do this and many people have differing opinions. In my experience I find a state engine to be best. This essentially uses a variable to keep track of the state (sequence number) you are currently on. When the state is finished you change the state variable to the next state. Outputs are controlled by the state value. I use this in C and ladder and structured text often. It works well in any language. The state variable can be used to communicate the state to other parts of the program and control the PLCs actions. Since you're familiar with C type languages it would look something like this:

State 0 waits for retracted press, State 1 extends press and waits for confirmation, state 2 waits for 5 seconds to pass


// Global variables

UINT State = 0;

BOOLEAN OUT1; // Press Activate Output

BOOLEAN IN1; // Press Extended Sensor Input

void LOOP{

if(State==0 & IN1==FALSE){State = 1;} //Press is retracted, move to state 1

if(State==1 & IN1==TRUE){State = 2;}//Press is extended, move to state 2

if(State==2 & Elapsed_Time==5){State=0;}// Press has been down for 5 seconds, move to state 0


//Output Section

if(State==0){OUT1 = FALSE;}

if(State==1){OUT1 = TRUE;}

if(State==2){OUT1 = TRUE;}

//Add additional states here

} //End void Loop


Will you be using Ladder or ST ?

Share this post

Link to post
Share on other sites

Thanks a lot.

ST as possible, but especially in this first months I have to integrate existing code, that is mostly in ladder (actually this functionality is purely in ladder, that's my question to restructure this trying to keep the other part of the software). 


The state is a good idea. Another question : in my previous job, in C, I used to use defines to name the steps:




In this way, I can keep the code more readable and most of all insert new phases just changing the defines. 

And other things, I can create functions to understand where I am looking if the state is > or < of the "name" of the define. 


There is something similar that I can use for? I found the Enum, but don't know if is the best option 

Share this post

Link to post
Share on other sites

The only 2 ways I can think of doing this would be an Enum like you said or a String array. Neither are complete solutions..

With Enum you need to extract a numerical value from the Enum datatype and use conventional comparisons but changing the Enum value would fit what you are trying to achieve. This a sort of a halfway there option.

With a dynamically addressed string array you can use string compare instructions and have the states clearly displayed in your logic but this causes a lot of processing overhead compared to a simple numeric comparison. States would still be changed numerically though.

I guess you could even use a single String variable and change the string to progress through the stages. Lots of overhead processing again.

In my experience a numeric variable is always used and the states are just commented. A multi-dimensional string array is a good way to have the state description readily available for your HMI/SCADA/DCS but I've never seen it used as a core for a state engine.



Share this post

Link to post
Share on other sites

Another way some people do state engines is setting and resetting bits instead of a single numeric variable. The bits can have the name of the state as their tag name and this would get you a lot closer. This a controversial method as it is possible to be in multiple states at once if not programmed correctly. Troubleshooting a situation where this happens can be a headache since they can jump around faster than you can see. I'm all out of ideas, hopefully that helps.

Share this post

Link to post
Share on other sites

it is a simple state machine, if you don't like bit shifting (or any other bit manipulation), then stick with integer as a step number. it is all the same... steps do not need to be in order or consecutive. you can have state machine that only uses step 0, 1, 27, 444, 2019, 33 if that is the way you like it.



Share this post

Link to post
Share on other sites

Posted (edited)

Good morning guys, thanks a lot for your replies.


All those methods are interesting, but have a look at the simple state machine here represented from panic mode: If somewhere else I have a check on the state variable to identify the actual state of this logical part of the program, I will have something like IF STEP = 2 THEN, or IF STEP > 3 THEN... etc etc.

How can I manage if I have to modify the state machine inserting a new state beetween step 1 and 2? the new step will become the number 2, and I have to shift all the others (2 to 3, and so on) and most of all SEARCH for all the references in the program to the old 2 state and replace with the new.. terrible to mantain.

At the same time if I add the new state to an high value, I can't use anymore any control regarding the state > or < of a specific reference. I can define the status as 100, 200, etc and insert eventual middle states as 110, 120, 121, 122... but in long terms it could be always a problem.

At the same time I could set some variable to identify a logical state indipendently of the state (ie, if I need to check if the state is <5, I can set a variable = 1 at the first state and = 0 at state 5, and then check the variable instead of the state. this solution is not bad, but basically why I have to introduce new variable (or variables, is possible that I have to introduce al lot of them) when I can use the state?


Edited by Andrea Ascani

Share this post

Link to post
Share on other sites

I used the same layout as described by panic mode. I typically space out the step numbers into groups by 100's and each minor step by 10. Normally this leaves enough room to insert new intermediate steps if necessary. I have also made sub-sequences that the main sequence can call and wait to finish. This can also give some room to add steps and break up the main sequence into smaller sequences to make the code more manageable. 

Share this post

Link to post
Share on other sites

How can I manage if I have to modify the state machine inserting a new state between step 1 and 2?

if you don't care that steps are not sequential, simply add new step... like 19470... and your sequence becomes


this means very little change to existing code.

if you do want to make them ordered, you will either need to edit code for all following steps or you will need to plan in advance and leave some spares. in the old days of BASIC it was common to use line numbers that are multiples of 10 (10, 20, 30, ...) so that if you need to insert something, there are few (nine to be exact) spare values for any unexpected situations.


Share this post

Link to post
Share on other sites

Thanks a lot guys, I probably found a good compromise beetween your suggestion and my needing.

I created an enumerated structured, naming the steps as "start(0)", "verifyingConditions(10)", "preparingOperations(20)", and so on.

Then I created a structure where I move a variable of this enumerated type from a state to another according to the specific conditions.

In this way I can use the name of the enumrated structure to keep the code, and change just the enumerated definition if I want to add some intermediate steps (adding the name and shifting the enum values (ie add prePreparingOperations(20) and shift the others of 10 -> preparingOperations(30)).

With this solution I can also create some FB to verify if the step is > or < of a state of the enumerated list (calling by name). That was for me the fundamental part.

I just found that is not possible to use comparison functions (except for = and "<>") for enum variables (even if they are INT, after all), but I solved it using EnumToNum function.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now