Play a Melody using the tone() function (help)

Hello.

Please can you help me?
I try many days (without result) this scetch add a potentiometer to change the rhythm of the sound.
Ie how fast the slowly using a potentiometer.
Please can you tell me what should I do?
Thank you very much.

/*
  Melody
 
 Plays a melody 
 
 circuit:
 * 8-ohm speaker on digital pin 8
 
 created 21 Jan 2010
 modified 30 Aug 2011
 by Tom Igoe 

This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/Tone
 
 */
 #include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

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

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

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.
}

Unfortunately since you've not posted your code in code-tags its corrupted with smileys - please re-edit the posting// using code tags so it can be easily copy/pasted for testing.

The .h file is needed too to re-create your problem.

MarkT: Unfortunately since you've not posted your code in code-tags its corrupted with smileys - please re-edit the posting// using code tags so it can be easily copy/pasted for testing.

The .h file is needed too to re-create your problem.

Please some example?

Read the sticky thread about posting on this forum again, it explains how to do all this.

MarkT: Read the sticky thread about posting on this forum again, it explains how to do all this.

I received no help but thanks.

You should: 1) Read the first two posts on this forum, which are called "sticky" threads or posts. 2) Place your cursor in the Arduino IDE source code window and press Ctrl-T to reformat the code using stand C formatting 3) Post all of your code by copying it from the source window to the space between the code tags, which are created using the '#' posting icon.

econjack: You should: 1) Read the first two posts on this forum, which are called "sticky" threads or posts. 2) Place your cursor in the Arduino IDE source code window and press Ctrl-T to reformat the code using stand C formatting 3) Post all of your code by copying it from the source window to the space between the code tags, which are created using the '#' posting icon.

I'm new, I do not know much about programming, but it is my dream to learn. I believe we will try to make it. Thank you very much for your reply.

fmnotes:

econjack: You should: 1) Read the first two posts on this forum, which are called "sticky" threads or posts. 2) Place your cursor in the Arduino IDE source code window and press Ctrl-T to reformat the code using stand C formatting 3) Post all of your code by copying it from the source window to the space between the code tags, which are created using the '#' posting icon.

I can not find these issues.(called "sticky" threads or posts) Where are they?

The first 2 topics above

http://forum.arduino.cc/index.php?topic=149014.0 http://forum.arduino.cc/index.php?topic=97455.0

please if anyone knows something more to help.

Thanks

Please help yourself by posting the code correctly in code tags. Look at the code in your original post. Does it really contain

 noTone(smiley-cool;

As it stands nobody but you knows what your code really looks like.

UKHeliBob:
Please help yourself by posting the code correctly in code tags. Look at the code in your original post. Does it really contain

 noTone(smiley-cool;

As it stands nobody but you knows what your code really looks like.

This is known code and want to add a potentiometer to control the speed of the melody. That I can make the music track playing what quickly what late.
If anyone knows the code to add, please upload an example. Thank.

/*
  Melody
 
 Plays a melody 
 
 circuit:
 * 8-ohm speaker on digital pin 8
 
 created 21 Jan 2010
 modified 30 Aug 2011
 by Tom Igoe 

This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/Tone
 
 */
 #include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

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

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

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.
}

This is known code and want to add a potentiometer to control the speed of the melody. That I can make the music track playing what quickly what late.
If anyone knows the code to add, please upload an example. Thank.

… add a potmeter. center tap to A0, one end to 5V other end to GND (potmeter >=1k)
You will have to change ‘noteduration’…
e.g:

void setup() {
   //**** NEW LINE: read analog value
   int myspeed=map(analogRead(0),0,1023,10,500) ;  // get a factor  10%..500% depening on pots position
  // iterate over the notes of the melody:
  for (int 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.
    int noteDuration = 1000/noteDurations[thisNote];
   // NOW recalc this numer based on YOUR number from analog reading
   // Thin can be done in many different ways. Here is one:
     noteDuration=noteDuration * (myspeed/100.0);
    tone(8, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30 * (myspeed/100.0)  ; // ADDED factor
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(smiley-cool;
  }
}

knut_ny:
… add a potmeter. center tap to A0, one end to 5V other end to GND (potmeter >=1k)
You will have to change ‘noteduration’…
e.g:

void setup() {

//**** NEW LINE: read analog value
   int myspeed=map(analogRead(0),0,1023,10,500) ;  // get a factor  10%…500% depening on pots position
  // iterate over the notes of the melody:
  for (int 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.
    int noteDuration = 1000/noteDurations[thisNote];
   // NOW recalc this numer based on YOUR number from analog reading
   // Thin can be done in many different ways. Here is one:
     noteDuration=noteDuration * (myspeed/100.0);
    tone(8, melody[thisNote],noteDuration);

// to distinguish the notes, set a minimum time between them.
    // the note’s duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30 * (myspeed/100.0)  ; // ADDED factor
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(smiley-cool;
  }
}

Thank you very much for your help.
It works well overmuch.
One question that I can learn these commands,
Looking here at the site and I’m a little confused.

then change

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

toint myspeed=map(analogRead(0),0,1023,70,140)

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

knut_ny: then change

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

toint myspeed=map(analogRead(0),0,1023,70,140)

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

Thank you very much.

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?

Use note 0 (for silence) , and add a duration in parallel array
…small change for player

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

knut_ny:
Use note 0 (for silence) , and add a duration in parallel array
…small change for player

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…