When to use delay, and for how long?

p. 76 (06 Light Theremin): Why is there a 10 milliseconds delay after a 20 milliseconds tone?
p. 107 (10 Zoetrope): Why is there a 1 millisecond delay between onOffSwitchState and directionSwitchState?

What are you pulling those from?

CrossRoads:
What are you pulling those from?

The Starter Kit, I think (I am not sure I understand your question).

https://github.com/arduino/Arduino/blob/master/build/shared/examples/10.StarterKit/p06_LightTheremin/p06_LightTheremin.ino#L62

...probably just to give a bit of delay between notes. I might take it out and see what difference it makes! Good lesson here: "wait for a moment" is a lousy comment because it's obvious and doesn't tell us anything more than we can glean by reading the code. It would be better just left out to reduce clutter. Better still would be to explain why the delay is there, as you question so well points out!

https://github.com/arduino/Arduino/blob/master/build/shared/examples/10.StarterKit/p10_Zoetrope/p10_Zoetrope.ino#L57

...perhaps switch debouncing, but 1ms is pretty short for most switches, 20-25ms would be more like it. Again, I'd probably experiment and see what effect it has. Try 10, 20, 50, 100.

That was indeed what I meant. Not many of us have the starter kit. You could post the code & hardware design being used, that would help a lot.

Thanks Jack! I don’t hear the difference with delay(0) and as I increase the delay the sound gets more scattered. The projects book says ‘call a delay() for 10 milliseconds to give the sound some time to play’, but your explanation seems to be the correct one. Given my findings and given that the delay between pulses is already defined by the frequency of the tone(), could I conclude that delay is unnecessary in this case?

No noticeable difference, even with delay(0).

Dansercoer:
Thanks Jack! I don’t hear the difference with delay(0) and as I increase the delay the sound gets more scattered. The projects book says ‘call a delay() for 10 milliseconds to give the sound some time to play’, but your explanation seems to be the correct one. Given my findings and given that the delay between pulses is already defined by the frequency of the tone(), could I conclude that delay is unnecessary in this case?

I had another look at the code and I think that it is necessary. Reason being, as soon as tone is called again, next time through the loop, the new note will immediately replace the old. Notice that tone plays the note for 20ms. So the delay should be something a little less than that, because the rest of the code in loop takes time to execute. Changing the 20ms delay in the tone call would be interesting to try as well, that should make the notes change more slowly.

On project 10, even calling delay(0) introduces a significant delay, so that may be enough to debounce the switch. I always use 20-25ms though. Too little and you'll get two or more presses for the price of one :wink:

So delay between the start of the tone() functions rather than between notes? What is the disadvantage of the new note (or tone) immediately replacing the old? Also, increasing the tone() milliseconds does not seem to make the notes change more slowly, which makes sense if the duration of the notes is limited by the time to execute the rest of the code in loop, no?

I am confused. :slight_smile:

Dansercoer:

[quote author=Jack Christensen link=topic=247024.msg1765946#msg1765946 date=1402795020]probably just to give a bit of delay between notes

So delay between the start of the tone() functions rather than between notes?
[/quote]

Yes, that is a better way to put it.

What is the disadvantage of the new note (or tone) immediately replacing the old?

The old note would not be heard then.

Also, increasing the tone() milliseconds does not seem to make the notes change more slowly, which makes sense if the duration of the notes is limited by the time to execute the rest of the code in loop, no?

Correct, therefore both the ms given in the call to tone() and the delay() have to be increased. Once tone() is started, it continues for the specified duration, and the rest of the code can continue and do other things. After the specified duration, the tone is turned off via an interrupt. But if tone is called again before the duration has ended, the new tone will replace the old at that time.

[quote author=Jack Christensen link=topic=247024.msg1769259#msg1769259 date=1403007299]

What is the disadvantage of the new note (or tone) immediately replacing the old?

The old note would not be heard then.[/quote]

Which means I was hearing the new note in my experiment, is that not an advantage?