<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to How can I put a delay in a st code with a for cycle?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/e3836baa66/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/e3836baa66/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/e3836baa66/</id><updated>2020-02-27T14:09:51.669000Z</updated><subtitle>Recent posts to How can I put a delay in a st code with a for cycle?</subtitle><entry><title>How can I put a delay in a st code with a for cycle?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/e3836baa66/?limit=25#dddc" rel="alternate"/><published>2020-02-27T14:09:51.669000Z</published><updated>2020-02-27T14:09:51.669000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com3eaa1b5717c917dd4b3adac59b1423747492477f</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; scott_cunningham&lt;/p&gt;
&lt;p&gt;You cannot hang the program with a for loop as a pause. This is PLC code, which means the program must run a complete execution within a certain amount of time, then the PLC does some IO scanning and then call your code again. If you hang it, then you will get an execution error. &lt;/p&gt;
&lt;p&gt;Instead, increment a variable every time your code executed and simply use an if statement to trigger a call to your send routine and reset your counter. &lt;/p&gt;
&lt;p&gt;Count := Count + 1;&lt;br/&gt;
IF Count &amp;gt; MyTrig THEN&lt;br/&gt;
   SendMsg();&lt;br/&gt;
   Count := 0;&lt;br/&gt;
END_IF&lt;/p&gt;
&lt;p&gt;You can also look at a state machine:&lt;/p&gt;
&lt;p&gt;CASE State OF&lt;/p&gt;
&lt;p&gt;0:&lt;br/&gt;
   Count := Count + 1;&lt;br/&gt;
   IF Count &amp;gt; MyTrig THEN&lt;br/&gt;
      Count := 0;&lt;br/&gt;
      State := State + 1;&lt;br/&gt;
   END_IF&lt;/p&gt;
&lt;p&gt;1:&lt;br/&gt;
   SendMsg();&lt;br/&gt;
   State := State + 1;&lt;/p&gt;
&lt;p&gt;2:&lt;br/&gt;
   Ans = RecAck();&lt;br/&gt;
   IF Ans THEN&lt;br/&gt;
      State := 0;&lt;/p&gt;
&lt;p&gt;END_CASE&lt;/p&gt;
&lt;p&gt;It is better to use an Enumeration instead of just numbers for the case states in real life, like (WAIT_TO_SEND, SEND, GET_ACK)&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>How can I put a delay in a st code with a for cycle?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/e3836baa66/?limit=25#9aaf" rel="alternate"/><published>2020-02-27T14:09:51.283000Z</published><updated>2020-02-27T14:09:51.283000Z</updated><author><name>Carlos Barría</name><uri>https://forge.codesys.com/u/carlos-barria/</uri></author><id>https://forge.codesys.com8fdeec81ce1b30511798dc9a12046b0040a62c2d</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;I need to change the time interval with which I send messages. Is it possible to do using a counter within the cycle for?&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>