Go Down

Topic: Play a Melody using the tone() function (help) (Read 6288 times) previous topic - next topic

fmnotes


then change
Quote
int myspeed=map(analogRead(0),0,1023,10,500)

to
Code: [Select]
int myspeed=map(analogRead(0),0,1023,70,140)

..there are LOTS of examples to find around.. Start on the 'playgroud'


Thank you very much.

fmnotes

We Also in this scetch to add a button, and give us pause at our track.
If you know any example please upload it here.
Thank you.

fmnotes


We Also in this scetch to add a button, and give us pause at our track.
If you know any example please upload it here.
Thank you.

No one knows?

knut_ny

#18
Apr 04, 2014, 03:29 pm Last Edit: Apr 04, 2014, 03:31 pm by knut_ny Reason: 1
Use note  0 (for silence)  , and add a duration in parallel array
..small change for player
Code: [Select]
int melody[] = { // added 3rd note - silence - fullnote
 NOTE_C4, NOTE_G3,   0  ,  NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
 4, 8,   1  , 8, 4,4,4,4,4 };

void setup() {
 // iterate over the notes of the melody:
 for (int thisNote = 0; thisNote < 9; 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.
   int noteDuration = 1000/noteDurations[thisNote];
   if (melody[thisNote]>0)  
       tone(8, melody[thisNote],noteDuration);
  else
      delay(noteDuration);

... this code have 'lost' the 'speed-factor', but should be easy to fix
Ny

fmnotes

#19
Apr 04, 2014, 10:09 pm Last Edit: Apr 05, 2014, 09:53 am by fmnotes Reason: 1

Use note  0 (for silence)  , and add a duration in parallel array
..small change for player
Code: [Select]
int melody[] = { // added 3rd note - silence - fullnote
 NOTE_C4, NOTE_G3,   0  ,  NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
 4, 8,   1  , 8, 4,4,4,4,4 };

void setup() {
 // iterate over the notes of the melody:
 for (int thisNote = 0; thisNote < 9; 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.
   int noteDuration = 1000/noteDurations[thisNote];
   if (melody[thisNote]>0)  
       tone(8, melody[thisNote],noteDuration);
  else
      delay(noteDuration);

... this code have 'lost' the 'speed-factor', but should be easy to fix


Thank you again for the help,
But I would keep the speed controller with potentiometer, and add a button that with one click freezes the sound on this note, ie at the point you pressed the button..


knut_ny

The call to "tone"  starts the 'external' procedure that plays your tone for a given time, then control is returned.
While tone is playing you have no control  until it is finished.

U can easily add a line of code to 'replay' that last tone if a key is pressed/help down
You can rewrite ... omitting the 'noteduration' parameter and instead control playback time based on several parameters.

To replay the last tone, something simple like this can be added:
Code: [Select]
   if (melody[thisNote]>0) 
       do {  tone(8, melody[thisNote],noteDuration); } while (digitalRead(yourPin)==HIGH);
   else
       do {   delay(noteDuration);  } while (digitalRead(yourPin)==HIGH);
Ny

fmnotes


The call to "tone"  starts the 'external' procedure that plays your tone for a given time, then control is returned.
While tone is playing you have no control  until it is finished.

U can easily add a line of code to 'replay' that last tone if a key is pressed/help down
You can rewrite ... omitting the 'noteduration' parameter and instead control playback time based on several parameters.

To replay the last tone, something simple like this can be added:
Code: [Select]
   if (melody[thisNote]>0) 
       do {  tone(8, melody[thisNote],noteDuration); } while (digitalRead(yourPin)==HIGH);
   else
       do {   delay(noteDuration);  } while (digitalRead(yourPin)==HIGH);



So eventually it is difficult to do what I want.
Thank you very much.

knut_ny

I understand that you want a "freeze" at ANY point..
That is absolutly possible, but requires that you contall all timing  yourself ..(It would be wrong to call this difficult)
________
A pause between notes is easy

Play with Tone  , delay, and noTone
Take a look in the includefile .. you may find that tone_names may be shorte (less writing)
Ny

Go Up