Arduino Forum

Using Arduino => Audio => Topic started by: monica on Mar 11, 2011, 06:02 am

Title: Upper limit to duration of tone(pin,freq,duration)
Post by: monica on Mar 11, 2011, 06:02 am
For the tone function the duration is simply listed as an unsigned int. Does anyone know if the limit to the duration is only limited to 2^23-1 milliseconds? That would be about 50 days. Could I really have a tone running for days?
Title: Re: Upper limit to duration of tone(pin,freq,duration)
Post by: RuggedCircuits on Mar 11, 2011, 06:08 am
It depends on the frequency. The constraint is that 2*frequency*duration has to be less than 2**31=2147483648. For example at a frequency of 440 Hz the duration has to be less than 2440322 seconds, or 28 days.

--
The Gadget Shield (http://ruggedcircuits.com/html/gadget_shield.html): accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons
Title: Re: Upper limit to duration of tone(pin,freq,duration)
Post by: CrossRoads on Mar 11, 2011, 06:18 am
It compiles & uploads, so it would seem so - haven't tried connecting a speaker yet to hear it.

Code: [Select]

// info on alarm sound
#include "pitches.h"

// notes in the melody:
int thisNote = 0;
int noteDuration = 0;
int pauseBetweenNotes = 0;
int melody[] = {
 NOTE_C6, NOTE_A5, NOTE_C6, NOTE_A5, NOTE_C6, NOTE_A5, NOTE_C6};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
 0xffffffff,12,12,12,12,12,4};
void setup(){}
void loop(){
 for (thisNote = 0; thisNote < 8; thisNote++)
 {
   // to calculate the note duration, take one second
   // divided by the note type.
   //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
   // noteDuration = 1000/noteDurations[thisNote];
   noteDuration =noteDurations[thisNote];
   noTone(6);       //apparent known bug - need this for the tone to play next.
   tone(6, melody[thisNote],noteDuration);
   // to distinguish the notes, set a minimum time between them.
   // using the note's duration + 10%:
   pauseBetweenNotes = noteDuration * 1.10;
   delay(pauseBetweenNotes);
   // stop the tone playing:
   //  noTone(6);
 }
}
Title: Re: Upper limit to duration of tone(pin,freq,duration)
Post by: monica on Mar 11, 2011, 06:29 am
Thanks guys for your fast responses. In case any of you are wondering why I would care about this, it's because I need to drive a vibrating motor (no giggling!) at approximately 200Hz for about five days. It looks like it will work!
Title: Re: Upper limit to duration of tone(pin,freq,duration)
Post by: CrossRoads on Mar 11, 2011, 06:37 am
Well, you always play quarter notes in void loop over and over and ... :)
Title: Re: Upper limit to duration of tone(pin,freq,duration)
Post by: mowcius on Mar 11, 2011, 06:13 pm
I need to drive a vibrating motor for about five days

giggety :D

But yeah - as CrossRoads said, you can just loop a short tone - the delay between them would basically be nothing.