<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>A non blocking delay</title><link>/products/arduino/arduino-tutorials/b/blog/posts/a-non-blocking-delay</link><description>The &amp;quot;hello world&amp;quot; of the Arduino is the blink sketch. This demonstrates that your board is working by blinking an LED, typically the on-board LED attached to pin13 but it has a problem.The code uses the &amp;quot;delay&amp;quot; function which waits the desired number</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: A non blocking delay</title><link>https://community.element14.com/products/arduino/arduino-tutorials/b/blog/posts/a-non-blocking-delay</link><pubDate>Tue, 28 Jun 2016 01:14:04 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:6b0a5fc0-36ab-49d4-aebc-efea05d8ceff</guid><dc:creator>celem</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;I altered &lt;span&gt;Andy Clark&amp;#39;s solution to better suit my needs. While I kept the Timeout member, I am not using it. I modified Delay() so that it stands alone and only executes the code after the timer expires. Here is what I am using. Note, all three files are concatenated together for this comment.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:3ef06e6b-9b7d-4651-b12d-db8be7f5e991:type=c_cpp&amp;amp;text=%2F**%0A+*+nonblockdelay2.ino%0A+*+%0A+*+example+%22blink%22+program%0A+*%0A+*%2F%0A%0A%0A%23include+%22Delay.h%22%0A%0A%0Aint+led+%3D+13%3B%0Aint+ledstate+%3D+HIGH%3B%0Astatic+NonBlockDelay+d%3B%0A%0A%0Avoid+setup%28%29%0A%7B++++++++++++++++%0A++pinMode%28led%2C+OUTPUT%29%3B%0A%7D%0A%0A%0A%2F%2F+the+loop+routine+runs+over+and+over+again+forever%3A%0Avoid+loop%28%29%0A%7B%0A++if%28d.Delay%281000%29%29%0A++%7B%0A++++ledstate+%3D+%28ledstate+%3D%3D+LOW+%3F+HIGH+%3A+LOW%29%3B+%2F%2Ftoggle+state%0A++++digitalWrite%28led%2C+ledstate%29%3B%0A++%7D%0A++%2F%2FCode+here+runs+frequently%0A%7D%0A%0A%0A%2F%2F---------------------%0A%2F**%0A+*+Delay.cpp%0A+*%0A+*%2F%0A%0A%23include+%22Delay.h%22%0A%0A%0A%2F%2F+Set+iTimeout+to+current+millis+plus+milliseconds+to+wait+for%0A%2F**%0A+*+Called+with+milliseconds+to+delay.%0A+*+Return+true+if+timer+expired%0A+*+%0A+*%2F%0Abool+NonBlockDelay%3A%3ADelay+%28unsigned+long+t%29%0A%7B%0A++if%28TimingActive%29%0A++%7B%0A++++if%28%28millis%28%29+%3EiTimeout%29%29%7B%0A++++++TimingActive+%3D+0%3B%0A++++++return%281%29%3B%0A++++%7D%0A++++return%280%29%3B%0A++%7D%0A++iTimeout+%3D+millis%28%29+%2B+t%3B%0A++TimingActive+%3D+1%3B%0A++return%280%29%3B%0A%7D%3B%0A%0A%0A%0A%0A%2F%2F+returns+true+if+timer+expired%0Abool+NonBlockDelay%3A%3ATimeout+%28void%29%0A%7B%0A++if%28TimingActive%29%7B%0A++++if%28%28millis%28%29+%3EiTimeout%29%29%7B%0A++++++TimingActive+%3D+0%3B%0A++++++iTimeout+%3D+0%3B%0A++++++return%281%29%3B%0A++++%7D%0A++%7D%0A++return%28false%29%3B%0A%7D%0A%0A%0A%2F%2F+Returns+the+current+timeout+value+in+milliseconds%0Aunsigned+long+NonBlockDelay%3A%3ATime%28void%29%0A+%7B%0A+++return+iTimeout%3B%0A+%7D%0A%0A%0A%2F%2F------------------------------%0A%2F**%0A+*+Delay.h%0A+*%0A+*%2F%0A%0A%23if+defined%28ARDUINO%29+%26%26+ARDUINO+%3E%3D+100%0A%23include+%3CArduino.h%3E%0A%23else%0A%23include+%3CWProgram.h%3E%0A%23endif%0Aclass+NonBlockDelay+%7B%0A++++unsigned+long+iTimeout%3B%0A++++bool+TimingActive+%3D+0%3B%0A++public%3A%0A++++bool+Delay+%28unsigned+long%29%3B%0A++++bool+Timeout+%28void%29%3B%0A++++unsigned+long+Time%28void%29%3B%0A%7D%3B%0A%2F%2F-------------------------]&lt;/p&gt;&lt;div&gt;&lt;/div&gt;&lt;img src="https://community.element14.com/aggbug?PostID=17844&amp;AppID=148&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: A non blocking delay</title><link>https://community.element14.com/products/arduino/arduino-tutorials/b/blog/posts/a-non-blocking-delay</link><pubDate>Mon, 27 Jun 2016 20:34:11 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:6b0a5fc0-36ab-49d4-aebc-efea05d8ceff</guid><dc:creator>celem</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt; Andy Clark&amp;#39;s solution is concise and makes for readable source - but behaves as a do {..code..}while loop where the &amp;#39;code&amp;#39; will execute one and the delay only applies to subsequent executions of Timeout(). Using the example code:&lt;/p&gt;&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:3ef06e6b-9b7d-4651-b12d-db8be7f5e991:type=c_cpp&amp;amp;text=void+loop%28%29+%7B%0A++if+%28d.Timeout%28%29%29+%7B++++%2F%2F+always+true+on+first+call%0A++++digitalWrite%28led%2C+ledstate%29%3B%0A++++ledstate+%3D+ledstate+%3D%3D+LOW+%3F+HIGH+%3A+LOW%3B+%2F%2Ftoggle+state%0A++++d.Delay%281000%29%3B%0A++%7D]&lt;/p&gt;&lt;div&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;d.Timeout returns (iTimeout &amp;lt; millis()). On the first invocation iTimeout is indeterminate but, unless millis() has turned over, probably less than millis() returns, so &amp;#39;true&amp;#39; will be returned. Once d.Delay is called iTimeout will be set to millis() + 1000 so subsequent calls to d.Timeout will return &amp;#39;false&amp;#39; until millis() increments above iTimeout.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;This is useful where a do {..code..}while loop is called for, such as blinking the LED but not useful in a delay before executing situation, which is more like a blocking delay() would do. I haven&amp;#39;t figured out how to do that yet - meanwhile Andy Clark&amp;#39;s solution works for some situations.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=17844&amp;AppID=148&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: A non blocking delay</title><link>https://community.element14.com/products/arduino/arduino-tutorials/b/blog/posts/a-non-blocking-delay</link><pubDate>Tue, 10 Jun 2014 06:35:42 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:6b0a5fc0-36ab-49d4-aebc-efea05d8ceff</guid><dc:creator>mcb1</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;What about simply using the Millis() timer.&lt;/p&gt;&lt;p&gt;The example BlinkWithoutDelay (stupid name IMO) shows how to do it.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Use an unsigned long variable declaration and always subtract the lower number from the larger and the rollover works fine.&lt;/p&gt;&lt;p&gt;An example of its use as a debounce on a button&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:3ef06e6b-9b7d-4651-b12d-db8be7f5e991:type=c_cpp&amp;amp;text=++%2F%2F+Button+handling+variables%0A++boolean+ButtonState+%3D+HIGH%3B+++++++++++%2F%2F+records+the+Button+State%0A++unsigned+long+LastButtonCheck+%3D+0%3B++++%2F%2F+Time+the+Buttons+were+last+checked.%0A++unsigned+long+ButtonPressTime+%3D+0%3B++++%2F%2F+Time+the+last+button+was+pressed.%0A++int+ButtonCount+%3D+0%3B++++++++++++++++++%2F%2F+Button+counter]&lt;/p&gt;&lt;div&gt;&lt;/div&gt;&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:3ef06e6b-9b7d-4651-b12d-db8be7f5e991:type=c_cpp&amp;amp;text=if+%28millis%28%29+-+LastButtonCheck+%3E+5%29++++++++++++++++++++%2F%2F+Reads+button+state+every+5mS+and+then+updates+button+counts%0A++++++%7B+++++++%0A++++++++ButtonState+%3D+digitalRead%28Pushbutton%29%3B%0A++++++++LastButtonCheck+%3D+millis%28%29%3B+%0A%0A++++++++if+%28ButtonState+%3D%3D+LOW%29%0A++++++++%7B%0A++++++++++ButtonCount+%2B%2B%3B+++++++++++++++++++++++++++++++++++++%2F%2F+Increment+the+Count+by+1%0A++++++++++if+%28ButtonCount+%3E+5%29+++++++++++++++++++++++++++++++%2F%2F+the+button+should+be+LOW+for+5x5mS+%3D+25mS%0A++++++++++%7B%0A++++++++++++%2F%2F+Button+has+been+pressed+for+longer+than+25mS+so+its+valid%0A++++++++++++ButtonPressTime+%3D+millis%28%29%3B+++++++++++++++++++++++%2F%2F+set+the+Button+press+timer%0A++++++++++++ButtonCount+%3D+0%3B%0A++++++++++++ProcessButton%28%29%3B++++++++++++++++++++++++++++++++++%2F%2F+Do+something+with+the+valid+button+press%0A++++++++++%7D%0A++++++++%7D%0A++++++++else++++++++++++++++++++++++++++++++++++++++++++++++++%2F%2F+Button+is+HIGH%0A++++++++%7B%0A++++++++++ButtonCount+%3D0%3B+++++++++++++++++++++++++++++++++++++%2F%2F+Reset+the+counter+as+the+button+has+been+released%0A++++++++%7D%0A++++++%7D]&lt;/p&gt;&lt;div&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;mark&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;edit ... change the 100mS to 25mS in the comment on line 11&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=17844&amp;AppID=148&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: A non blocking delay</title><link>https://community.element14.com/products/arduino/arduino-tutorials/b/blog/posts/a-non-blocking-delay</link><pubDate>Mon, 09 Jun 2014 21:33:40 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:6b0a5fc0-36ab-49d4-aebc-efea05d8ceff</guid><dc:creator>Former Member</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;Hi,&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;This is a great class, thanks!&amp;nbsp; I&amp;#39;ve got it working with a few examples, but am having trouble getting it working while iterating through a loop.&amp;nbsp; Do you see any trouble with the syntax of my for-loop here?&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:3ef06e6b-9b7d-4651-b12d-db8be7f5e991:type=c_cpp&amp;amp;text=NonBlockDelay+ramp%3B%0A%0Avoid+loop%28%29+%7B%0A++++%2F%2F+The+goal+is+to+delay+writing+new+i+values+to+myPin%0A++++for+%28i%3D0%3B+i%3C256%3B+i%2B%2B%29+%7B%0A++++++++if+%28ramp.Timeout%28%29%29+%7B%0A++++++++++++analogWrite%28myPin%2C+i%29%3B%0A++++++++++++%2F%2FSerial.println%28i%29%3B%0A++++++++++++ramp.Delay%281000%29%3B%0A++++++++%7D%0A++++%7D%0A%7D]&lt;/p&gt;&lt;div&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Thanks for any insights -- this is really puzzling because, oddly enough, if I uncomment line 8, the delay &lt;em&gt;somehow&lt;/em&gt; works.&amp;nbsp; I&amp;#39;m at a loss.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=17844&amp;AppID=148&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item></channel></rss>