Go Down

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

fmnotes

Mar 27, 2014, 01:39 am Last Edit: Apr 02, 2014, 06:52 pm by fmnotes Reason: 1
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.




Code: [Select]
/*
  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.
}

MarkT

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

The .h file is needed too to re-create your problem.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

fmnotes


Unfortunately since you've not posted your code in code-tags its corrupted with
smileys - please re-edit the posting
Code: [Select]
// 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?

MarkT

Read the sticky thread about posting on this forum again, it explains how to do all this.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

fmnotes


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


I received no help but thanks.

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.

fmnotes


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



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?

UKHeliBob

The first 2 topics above

http://forum.arduino.cc/index.php?topic=149014.0
http://forum.arduino.cc/index.php?topic=97455.0
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

fmnotes

please if anyone knows something more to help.

Thanks

UKHeliBob

Please help yourself by posting the code correctly in code tags.  Look at the code in your original post.  Does it really contain
Code: [Select]
noTone(smiley-cool;
As it stands nobody but you knows what your code really looks like.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

fmnotes

#11
Apr 02, 2014, 06:45 pm Last Edit: Apr 02, 2014, 06:51 pm by fmnotes Reason: 1

Please help yourself by posting the code correctly in code tags.  Look at the code in your original post.  Does it really contain
Code: [Select]
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.

Code: [Select]
/*
  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.

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:
Code: [Select]
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;
  }
}

Ny

fmnotes


.. 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:
Code: [Select]
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.

knut_ny

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'
Ny

Go Up