Go Down

Topic: Play Melody (Read 3205 times) previous topic - next topic

Grumpy_Mike

Post your code so far then we can have a look at it. Their have been a lot of changes since your last posted it.

SarahBethC

Heres what I have.

Grumpy_Mike

Do you know that an iPad and many other mobile devices can not handle a .ino file format?
You have posted code before, you know how to do it.

SarahBethC

/*
  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://www.arduino.cc/en/Tutorial/Tone

 
 */

  #define Note_c4 262
  #define Note_d4 294
  #define Note_e4 330
  #define Note_f4 349
  #define Note_g4 392
  #define Note_a4 440
  #define Note_b4 494
  #define Note_c5 523
  #define Note_f5 699
  #define Note_c6 1047
  #define Note_d5 587
  #define Note_eb5 622
  #define Note_g5 784
  #define Note_a5 880
  #define Note_bb5 932
  #define Note_b5 988
  #define Note_d6 1175
  #define Note_e6 1319
  #define Note_e5 659
  #define Note_eb4 311
  #define Note_bb4 466
  #define Note_d3 147
  #define Note_f3 175
  #define Note_b2 123
  #define Note_e3 162
  #define Note_g3 196
  #define Note_a3 220
  #define Note_bb3 233
  #define Note_eb3 156
  #define Note_c3 131
  #define Note_bb2 117
  #define Note_c2 65
  // Define a special note, 'R', to represent a rest
  #define R 0
 
// notes in the melody:
 int melody[] = {
    Note_g4, Note_e4, Note_f4, Note_g4, Note_a4, Note_c5, Note_b4, Note_f5, Note_c6, Note_c5, Note_d5, Note_eb5, Note_f5, Note_g5, Note_a5, Note_bb5, Note_a5, Note_g5, Note_f5, Note_e5, Note_d5, Note_c5, Note_b4, Note_a4, Note_g5, Note_a5, Note_b5, Note_c6,  Note_b5, Note_a5, Note_g5, Note_b5, Note_c6, Note_d6, Note_e6, Note_d6, Note_c6, Note_b5, R, Note_d6, Note_f5, Note_d6, Note_a5, Note_d6, Note_a5, Note_c6, Note_d5, Note_g5, Note_f5, Note_e5, Note_d5, Note_e5, Note_d5, Note_e5, Note_d5, Note_c6, Note_c4, R
 };
  int melody2[] = {
    Note_e4, Note_d4, Note_e4, Note_f4, Note_g4, Note_a4, R, Note_c5, Note_c5, Note_c5, R, Note_eb4, Note_bb4, R, Note_g5, Note_d5, R, Note_bb4, R, Note_d4, R, Note_a4, Note_g4, Note_a4, Note_b4, Note_c5, Note_b4, Note_a4, Note_b4, Note_g4, Note_b4, Note_c5, Note_d5, Note_e5, Note_d5, Note_c5, Note_b4, R, Note_e5, Note_f5, Note_g5, R, Note_g5, R, Note_e5, R, Note_d4, R, Note_e4, R, Note_a4, R, Note_c5, R, Note_g4, Note_f4, Note_e4, Note_d4, Note_d3, Note_c4, Note_c5, Note_f3, R
  }; 
  int melody3[] = {
    Note_c4, Note_d4, R, R, R, Note_f3, R, R, Note_b2, R, R, R, Note_eb3, Note_f3, Note_g3, Note_f3, Note_g3, Note_g5, R, R, R, R, Note_f5, Note_c6, R, R, R, Note_a4, R
   };
   int melody4[] = {
    Note_f3, Note_g3, Note_a3, Note_bb3, Note_a3, Note_bb3, Note_c4, Note_d4, Note_c4, Note_bb3, Note_a3, Note_g3, Note_f3, Note_eb3, Note_d3, Note_c3, Note_f3, Note_c3, Note_f3, Note_eb3, Note_f3, Note_a3, Note_f3, Note_g3, Note_f3, Note_c3, Note_g3, Note_eb3, Note_bb2, Note_d3, Note_a3, Note_bb3, Note_f3, Note_eb3, R, R
   };
    int melody5[] = {
      R, Note_d3, R, R, Note_a3, Note_bb3, Note_bb3, R, R, Note_d3, R, Note_d3, R, Note_d3, R, Note_bb2, R, Note_bb2, R, Note_bb2, Note_c3, R, R, Note_e3, Note_g3, Note_f3, R, R, R, Note_d3, R, R, Note_c3, R, Note_c2, R
  };
// note durations: 4 = quarter note, 8 = eighth note, etc.:
  int noteDurations[] = {
      4, 4, 4, 4, 4, 4, 4, 4, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 8, 8, 8, 8, 6, 6, 6, 8, 8, 8, 8, 6, 6, 6, 8, 16, 16, 16, 16, 16, 16, 4, 4, 4, 4, 3, 8, 4, 4, 2, 4, 2
  };
  int noteDurations2[] = {
    4, 4, 4, 4, 4, 4, 4, 4, 16, 16, 16, 16, 16, 8, 16, 16, 8, 16, 16, 16, 8, 16, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 4, 4, 4, 4, 2, 2, 2, 4, 4
  };
  int noteDurations3[] = {
    4, 8, 8, 2, 2, 8, 8, 4, 2, 2, 2, 4, 6, 6, 6, 2, 2, 16, 16, 8, 8, 16, 16, 16, 6, 16, 16, 16, 1
  };
  int noteDurations4[] = {
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1
  };
  int noteDurations5[] = {
  8, 16, 16, 8, 16, 16, 16, 16, 16, 16, 4, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 1, 1, 2, 16, 16, 16, 16, 4, 2, 2, 2, 2, 2, 4, 4
};

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 63; 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.75;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  }

Grumpy_Mike

See that next to last line? noTone( cool smiley ?
Did you write that? No, that is the forum mangling code. This is why we ask people to post code correctly in code tags. Please read this:-
How to use this forum

So you started this thread by asking
Quote
how to code 2 separate melodies to play from the same board (UNO) at once??
You seem to have ignored all the answers you got.
You can not use the tone function to play more than one note at the same time.

You can google the words:-
Arduino Polyphonic libiary
To find a replacement for your tone function

DocStein99

Or, buy Mike's book and read that, which explains how to make your own polyphonic midi synthesizers.

Go Up