user31599: I've seen some code where the developers add delay(0), it doesnt make sense, what does it do? Delay with zero milliseconds, why?
bool DFRobotDFPlayerMini::waitAvailable(unsigned long duration){
  unsigned long timer = millis();
  if (!duration) {
    duration = _timeOutDuration;
  }
  while (!available()){
    if (millis() - timer > duration) {
      return false;
    }
    delay(0);
  }
  return true;
}


user4143: Delay (traditionally) has two functions that it performs:

Wait for a period
Yield processing to other threads through the yield() weak symbol.

By using a delay(0) the author thinks they are saying "I don't want to delay here, but if anything is using the yield() function it can run now."
However, that is not correct.
The code for delay() in the AVR core looks like this:
void delay(unsigned long ms) 
{
    uint32_t start = micros();

    while (ms > 0) {
        yield();
        while ( ms > 0 && (micros() - start) >= 1000) {
            ms--;
            start += 1000;
        }
    }   
}

You can see that, as well as delaying for a period, yield() is regularly called. However that only ever happens if ms is greater than 0. If you pass 0 as the delay length then the delay() function essentially does nothing at all.
Now it could be that an earlier version of the core had yield() always being called even with ms==0, but the current version doesn't. Maybe this is a hangup from an old coding style.  But for the current core if you want to yield you must explicitly call yield() or delay for at least one millisecond.

31599: <p>I've seen some code where the developers add <code>delay(0)</code>, it doesnt make sense, what does it do? Delay with zero milliseconds, why?</p>
<pre><code>bool DFRobotDFPlayerMini::waitAvailable(unsigned long duration){
  unsigned long timer = millis();
  if (!duration) {
    duration = _timeOutDuration;
  }
  while (!available()){
    if (millis() - timer &gt; duration) {
      return false;
    }
    delay(0);
  }
  return true;
}
</code></pre>
 4143: <p>Delay (traditionally) has two functions that it performs:</p>
<ol>
<li>Wait for a period</li>
<li>Yield processing to other threads through the <code>yield()</code> weak symbol.</li>
</ol>
<p>By using a <code>delay(0)</code> the author thinks they are saying &quot;I don't want to delay here, but if anything is using the <code>yield()</code> function it can run now.&quot;</p>
<p>However, that is <em>not</em> correct.</p>
<p>The code for <code>delay()</code> in the AVR core looks like this:</p>
<pre class="lang-cpp prettyprint-override"><code>void delay(unsigned long ms) 
{
    uint32_t start = micros();

    while (ms &gt; 0) {
        yield();
        while ( ms &gt; 0 &amp;&amp; (micros() - start) &gt;= 1000) {
            ms--;
            start += 1000;
        }
    }   
}
</code></pre>
<p>You can see that, as well as delaying for a period, <code>yield()</code> is regularly called. However that only ever happens if <code>ms</code> is greater than 0. If you pass <code>0</code> as the delay length then the <code>delay()</code> function essentially does nothing at all.</p>
<p>Now it <em>could</em> be that an earlier version of the core had <code>yield()</code> always being called even with <code>ms==0</code>, but the current version doesn't. Maybe this is a hangup from an old coding style.  But for the current core if you want to yield you must explicitly call <code>yield()</code> or delay for <em>at least one millisecond</em>.</p>
