2.3. Programming¶
Download project Exercises.zip
2.3.2. SCL¶
2.3.2.1. If statement¶
Think about the if
statement as you think in daily life.
For example:
- If today is raining I take umbrella
- If it is cold I put a coat
- I you find orange then buy, otherwise buy apple.
2.3.2.2. Case statement¶
Case is like if, it check if the numerical value of the variable is present in the list, and execute the instruction corresponding to that value.
For example let create a variable day
of type int
. The first day of the week is one the last day is seven.
So If I want to make a decision tree, I list in the case
statement days from 1 to 7, and for every value I do something:
- If day is 1 (Monday), I go to work
- If day is 2, I do something else
- …
- If day is 6, I stay at home.
Remember that a case can be written also as an if.
The Case statement is more suitable in state machine
. In Siemens there is no enumeration
data type. In Tia portal siemens introduce CONSTANTS, so we can emulate an enumeration
. It is more clear to have name than numbers. For example, is more clear to say Monday than day 1. And if Day 1 for me is Sunday? So is better to create a set of CONSTANTS with unique value and use them.
int today;
const int MONDAY := 1;
const int TUESDAY := 2;
const int WEDNESDAY := 3;
const int THURSDAY := 4;
const int FRIDAY := 5;
const int SATURDAY := 6;
const int SUNDAY := 7;
CASE today OF
MONDAY:
I go to work;
SATURDAY:
I sleep more;
ELSE:
Error day is not recognized;
END_CASE;
2.3.2.3. Loop¶
Try to avoid for
and while
in PLC programming if you don’t know what are you doing. Infinite loops stop the plc.