If (Digital_2 or Input_1) THEN Light_1 := TRUE; Light_2 := FALSE; Delay(IN:=FALSE); Light_2 := TRUE; Light_1 := FALSE; Delay(IN:=FALSE); ELSE Light_1 := FALSE; Digital_3:= False; END_IF; I’ll go through the code step by step 1. Input_1 is wired to a button. When I press it the if statement should start. 2. Light_1 (blue light) turns on, Light_2 (red light) will stay off 3. 5 seconds pass with blue light on 4.After 5 seconds red light turns on and blue light stays off When I run this program and press the button the red light comes on for 5 seconds and then shuts off. How come this keeps happening? I thought that the delay timers would stop the logic and give it enough time to switch booleans. How does PLCNext read this statement? If this is wrong how can I break my logic up to do what I intended?
I assume that the “Delay” function is a timer, eg a TP timer? Or is it your own custom function? It looks like that Delay function might set/reset the value of variable Digital_2, which (I guess) keeps the IF condition TRUE after Input_1 goes off? Without seeing the rest of the code, these are just guesses. In any case, in IEC 61131 program instances, the code is executed all the way through once every time the Task is executed. In particular, IEC timers don’t behave like the sleep() function in C++ does (for example). In C++, sleep() blocks the execution of the program for the specified number of seconds, but in IEC 61131 nothing is allowed to block the task from finishing within the Watchdog time, or before the task is next scheduled to run. So timers like TON and TOF simply increment the ET variable, if necessary, each time they are called, but they do not block the execution of the task. That seems to be what you are expecting to happen in this case. So how to do what you want? In IEC code, sequences like the one you are trying to implement are typically done using either SFC or a step sequencer pattern in ST. Step timers should be called unconditionally outside the sequencer. The timer inputs are usually based on the step number, and the timer output used to move the sequencer to the next step. Finally, outputs are usually set at the end of the sequence based on the step number. Someone else might be able to give an implementation of a sequencer like this for your example, if not I’ll try to add one tomorrow.
Here is a step sequencer in ST that implements something like you intend (I think). The Delay timers are On-Delay (TON) timers. // Step sequencer // Step is a UINT CASE Step OF 0: // Idle. Waiting for button press. IF Input_1 THEN Step := Step + 1; END_IF; 1: // Waiting for first delay timer. IF Delay_1.Q THEN Step := Step + 1; END_IF; 2: // Waiting for second delay timer. IF Delay_2.Q THEN Step := 0; // Reset sequence. END_IF; ELSE // We should never reach here. Step := 0; END_CASE; // Step timer processing Delay_1 (IN:=(Step=1), PT:=T#5S); // TON Delay_2 (IN:=(Step=2), PT:=T#5S); // TON // Output assignment Light_1 := (Step=1); // Blue lamp Light_2 := (Step=2); // Red lamp
Than you for the reply! That code fits perfectly and its what I needed it to do. Unfortunately, when I nest this in an if statement I run into the same problem. I attempted to use a function but wasn’t able to wrap my head around them because I can’t seem to get the return variable to output a true or false. Am I not able to return a variable with a boolean? I’ve decided to look into C++, and before I start, I wanted to know if C++ would still read the same like PLCNext, or if it would read line by line? When I use C++ will its syntax have to be written similar in a way to PLCNext?
[quote]when I nest this in an if statement I run into the same problem.[/quote]If you surround the entire code block with an IF statement, then when the IF condition is false the timers will not increment, the outputs won’t be assigned, and the step sequencer won’t progress. Everything will be frozen until the IF condition goes true again. If this is not what you want, then you will need to find another way, which should be possible if you understand how IEC tasks are executed. [quote]Am I not able to return a variable with a boolean?[/quote]Yes, this is possible. In a new Function, set the Signature to return a BOOL, and then assign the return value to the “variable” with the same name as the function - e.g. if the name of your function is “MyFunction”, then the assignment MyFunction := TRUE in the function will return TRUE from that function, and copy that value to whatever boolean variable is connected to the function output. Obviously you will want to return a variable boolean value from your function instead. [quote]I wanted to know if C++ would still read the same like PLCNext, or if it would read line by line?[/quote]Both IEC code and C++ code are “read” the same way - they are both executed line by line. IEC code cannot include any blocking functions, like endless (or very long) loops. If you are writing a C++ program that will run on the ESM, then you have to follow the same rules - any statements or function calls in the C++ “Execute” function that block program execution - like sleep(), or endless loops - are not allowed. Once you understand how IEC tasks are executed, this becomes a lot clearer.
Thank you, this has helped a lot. I was wondering if there are any PLCNext resources that I could use other than the help function in PLCNext. Something along the lines of a learning academy or a video series that covers the fundamentals of PLCNext.
Hello Keith, next to the FORUM menu in the PLCnext-Community you can find the Knowledge Base. There you can find links to alot of Tutorials Elearnings, Info Center etc… hope that helps. Getting Started with PLCnext Technology https://www.plcnext.help/ kind regards, Oliver
Thank you for the help! Feel free to close this thread
For anyone looking for general IEC 61131 training: Realpars have some good basic PLC programming videos on YouTube (using Ladder Logic, but the same principles can be applied to all IEC 61131 languages). Here is the link: https://youtu.be/y2eWdLk0-Ho Realpars also has a basic course on the fundamentals of PLC programming in their course library (again in LAD). Here is the link: https://learn.realpars.com/getting-started-with-plc-programming These courses give a solid foundation in the principles of PLC programming, which can also be applied to programming in PLCnext Engineer.