Go Down

Topic: When to use delay, and for how long? (Read 1 time) previous topic - next topic

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?

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


What are you pulling those from?


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

Jack Christensen

#3
Jun 15, 2014, 03:17 am Last Edit: Jun 15, 2014, 03:19 am by Jack Christensen Reason: 1
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.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#5
Jun 16, 2014, 11:45 pm Last Edit: Jun 16, 2014, 11:50 pm by Dansercoer Reason: 1
...probably just to give a bit of delay between notes. I might take it out and see what difference it makes!


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?

Try 10, 20, 50, 100.


No noticeable difference, even with delay(0).

Jack Christensen


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  ;)
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

probably just to give a bit of delay between notes


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.  :)

Jack Christensen


probably just to give a bit of delay between notes


So delay between the start of the tone() functions rather than between notes?


Yes, that is a better way to put it.

Quote

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


The old note would not be heard then.

Quote

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.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

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


The old note would not be heard then.


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

Go Up