Pages: [1]   Go Down
Author Topic: When to use delay, and for how long?  (Read 1004 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 548
Posts: 27372
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What are you pulling those from?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What are you pulling those from?

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

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 14, 2014, 08:19:31 pm by Jack Christensen » Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 548
Posts: 27372
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


* Light Theremin.jpg (307.89 KB, 710x557 - viewed 14 times.)

* Zoetrope.jpg (369.1 KB, 834x540 - viewed 13 times.)
« Last Edit: June 16, 2014, 04:50:37 pm by Dansercoer » Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-wink
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Pages: [1]   Go Up
Jump to: