Go Down

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

#### fmnotes

#15
##### Apr 02, 2014, 11:23 pm

then change
Quote

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

#16
##### Apr 03, 2014, 10:42 pm
We Also in this scetch to add a button, and give us pause at our track.
Thank you.

#### fmnotes

#17
##### Apr 04, 2014, 03:27 pm

We Also in this scetch to add a button, and give us pause at our track.
Thank you.

No one knows?

#### knut_ny

#18
##### Apr 04, 2014, 03:29 pmLast 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 pmLast 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..

#20
No one knows?

#### knut_ny

#21
##### Apr 05, 2014, 06:30 pm
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

#22
##### Apr 05, 2014, 06:45 pm

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

#23
##### Apr 05, 2014, 08:03 pm
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