Go Down

Topic: Upper limit to duration of tone(pin,freq,duration) (Read 1 time) previous topic - next topic

monica

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?

RuggedCircuits

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: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons

CrossRoads

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

monica

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!

CrossRoads

Well, you always play quarter notes in void loop over and over and ... :)
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.

mowcius

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.

Go Up