Arduino Forum

Using Arduino => Audio => Topic started by: SarahBethC on Nov 28, 2016, 05:20 am

Title: Play Melody
Post by: SarahBethC on Nov 28, 2016, 05:20 am
Does anyone what suggestions on how to hook up 2 speakers to one arduino as well as how to code 2 separate melodies to play from the same board (UNO) at once?? I have 1 strain of note set to play, but I have 5 separate strains that need to play at once....I can't figure out how to write/hookup the program/board to work correctly.....
Title: Re: Play Melody
Post by: Grumpy_Mike on Nov 28, 2016, 06:39 am
There is no need for two speakers, just one will do but connect the output to an amplifier.

The code is complex and might be found on line but I cover this in my book http://www.apress.com/gb/book/9781484217207 (http://www.apress.com/gb/book/9781484217207)

Unfortunately due to my deal with the publishers I can't post it here.
Title: Re: Play Melody
Post by: SarahBethC on Nov 28, 2016, 07:15 pm
If I hook it up to a speaker will I have to rewrite my whole code??
Title: Re: Play Melody
Post by: Grumpy_Mike on Nov 28, 2016, 09:06 pm
No, why do you ask? The sound waveform still comes out of the same pin does it not?
Title: Re: Play Melody
Post by: SarahBethC on Nov 28, 2016, 10:06 pm
Yes. I may have been going about it wrong. I was thinking I'd have to use multiple pins for mulitple speakers, but you're saying 1 pin attached to 1 speaker that will play multiple sounds at once??
Title: Re: Play Melody
Post by: Grumpy_Mike on Nov 28, 2016, 10:13 pm
Quote
but you're saying 1 pin attached to 1 speaker that will play multiple sounds at once??
Only if your code is written to mix the two waveforms BEFORE they are outputted to the pin.

If you are producing a separate waveform on each pin then connect a resistor between the pin and the input to the amplifier, and another resistor from the input of the amplifier to ground. All 1K ones should do.
Title: Re: Play Melody
Post by: CrossRoads on Nov 28, 2016, 10:18 pm
You can "cheat"! Use two microcontrollers, one for note creation, one for melody playing.
My project here used a 1284P processor to allow up to 13 notes to be created from 13 buttons.
You can do the same, but use a Uno/Nano etc to make the button presses.
'1284P cards:
http://www.crossroadsfencing.com/BobuinoRev17/ (http://www.crossroadsfencing.com/BobuinoRev17/)
"piano" project topic
http://forum.arduino.cc/index.php?topic=179761.0 (http://forum.arduino.cc/index.php?topic=179761.0)
Youtube video
https://www.youtube.com/watch?v=4c8idXN4Pg0 (https://www.youtube.com/watch?v=4c8idXN4Pg0)
The outputs are mixed together in the analog world instead of the digital world.
Title: Re: Play Melody
Post by: CrossRoads on Nov 28, 2016, 10:22 pm
I suppose you could do even more outputs, 2 octaves worth, and send the notes to play in serially. Start and stop info for each.  Like a basic form of MIDI? Might have to put a limit of how many notes can be played at once, I don't know how many notes can actually be created - I only did 8 because that was all the buttons I had.
Title: Re: Play Melody
Post by: DocStein99 on Nov 29, 2016, 10:38 am
You can "cheat"! Use two microcontrollers, one for note creation, one for melody playing.
I was curious about playing tones, and holding up the processor, for handling serial events from orher devices.  Couldnt figure out a way to multi-thread the main loop while playing a tone.

So i guess a seperate board, or external dedicated synth is about the only way to play tones, without jamming the main loop.
Title: Re: Play Melody
Post by: Grumpy_Mike on Nov 29, 2016, 10:47 am
No, you simply have a timer generating an interrupt that increments a tick. The ISR then just looks at this tick count and decides what pins to toggle depending on what time out has been set for each tone. You can have as many tones as you want. The more tones the more processor load. In my book, see link above, I have an example of generating a two tone telephone dialling output.
Title: Re: Play Melody
Post by: DocStein99 on Nov 29, 2016, 11:14 am
I just bought the book.  Thanks for pointing that out.  I look forward to figuring out how that works.
Title: Re: Play Melody
Post by: Grumpy_Mike on Nov 29, 2016, 11:26 am
Quote
I just bought the book.
Thanks  :)
Title: Re: Play Melody
Post by: SarahBethC on Nov 29, 2016, 08:51 pm
I am creating a program for a piano piece I wrote. 5 strains of notes over 4 octaves (I believe). I've been programming the speaker to play each note based on frequenties. There are 30+ notes (accidentals included). I've been using the sample play melody off the Arduino website.
Title: Re: Play Melody
Post by: DocStein99 on Nov 29, 2016, 09:31 pm
The piano song sounds interesting.  I made a few programs so far, using arduino uno.  They use speaker play tone.  Ran out of memory and forced me to learn how to maximize code to store notes and tones.

Knowing how to manipulate waveforms to create sounds is always something that interests me.  I like to programatically write sounds, just like they had to do for 80s Atari games.
Title: Re: Play Melody
Post by: Grumpy_Mike on Nov 29, 2016, 10:12 pm
Sarah - That sounds like an interesting project. I know it is not an Arduino but it is something that you can do in the Pure Data ( PD ) language. It is rather like MAX but free.

Currently I am working with Drake Music Labs - North, we design accessible instruments for disabled people. I have been working with a girl who is teaching autistic people and we have developed a drum that mimics and plays back what ever rhythm you tap into it.

Some unusual things I have designed and are in the book are:-
SpoonDuino:-
http://vimeo.com/38466551  (http://vimeo.com/38466551)
Harp Player
http://www.vimeo.com/21024841 (http://www.vimeo.com/21024841)note this is a early version with only 5 of the 10 strings working.
yes - no Arduino sample playing without hardware
https://youtu.be/WShVFcrFpwU (https://youtu.be/WShVFcrFpwU)
Arduinocaster - keytar
http://www.youtube.com/watch?v=ehy9xgl4YCs (http://www.youtube.com/watch?v=ehy9xgl4YCs)
Title: Re: Play Melody
Post by: SarahBethC on Nov 30, 2016, 10:03 pm
I switched to the Arduino "Melody" example code and I'm getting an error that says

 
error: expected declaration before '}' token

 };

I've changed my code 30 times and nothing is working....do any of you know what this means and how to fix it??? Its olny highlighting:

  #define Note_e4 329.63

There are 35ish more notes exactly like that with different frequencies and a different name (instead of being Note_e4 it'll be Note_f3, etc.). I don't know what is wrong with my code that only that part is messed up....
Title: Re: Play Melody
Post by: CrossRoads on Nov 30, 2016, 10:09 pm
Well, you could post/attach the code for us to look at and identify where the missing ; needs to go.

Are you sure Melody works with non-whole #s like 329.63?
Title: Re: Play Melody
Post by: SarahBethC on Nov 30, 2016, 10:10 pm
so , did you made it or not in the end?
Still working on it.
Title: Re: Play Melody
Post by: Grumpy_Mike on Nov 30, 2016, 11:32 pm
Quote
error: expected declaration before '}' token
Normally means a ; or a } misplaced.

If you click on a { then the matching } is highlighted, they must be matched.
Title: Re: Play Melody
Post by: SarahBethC on Dec 01, 2016, 12:15 am
Here is the program I have written. I haven't tried it with whole #s because I don't want the pitches to be off.....
Title: Re: Play Melody
Post by: uxomm on Dec 01, 2016, 02:10 am
The commas are missing in the melody array.

If you add them like this
Code: [Select]
// 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
};

the code will compile.
Title: Re: Play Melody
Post by: SarahBethC on Dec 01, 2016, 03:00 am
The commas are missing in the melody array.

If you add them like this
Code: [Select]
// 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
};

the code will compile.
I tried it that way, but it didn't work....
Title: Re: Play Melody
Post by: CrossRoads on Dec 01, 2016, 04:33 am
Code: [Select]
int melody[] = {
 R,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, Note_d6, Note_f5, Note_d6, Note_a5, Note_d6, Note_a5, Note_c6, Note_d5,
};

Compiles ok for me with IDE 1.6.9
R is in a different position, I thought it was a typo when I was adding commas, I don't know from where so I stuck it at the beginning.
Title: Re: Play Melody
Post by: SarahBethC on Dec 02, 2016, 03:01 pm
Code: [Select]
int melody[] = {
 R,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, Note_d6, Note_f5, Note_d6, Note_a5, Note_d6, Note_a5, Note_c6, Note_d5,
};

Compiles ok for me with IDE 1.6.9
R is in a different position, I thought it was a typo when I was adding commas, I don't know from where so I stuck it at the beginning.
R is representing a rest and it's in the right spot. It corrolates with the rest in the piano piece.
Title: Re: Play Melody
Post by: SarahBethC on Dec 05, 2016, 03:03 pm
I got it to work, but it's not playing all of the notes. Still working.
Title: Re: Play Melody
Post by: SarahBethC on Dec 05, 2016, 03:43 pm
Ladies and Gents, it is now officially working!!!!
Title: Re: Play Melody
Post by: DocStein99 on Dec 05, 2016, 09:33 pm
Congratulations.
Title: Re: Play Melody
Post by: SarahBethC on Dec 06, 2016, 07:16 pm
Now, I am trying to add in anbother strain of notes, however, I cannot imput int melody[] twice....any suggestions???
Title: Re: Play Melody
Post by: DocStein99 on Dec 06, 2016, 08:40 pm
Call it melody2[]
Title: Re: Play Melody
Post by: SarahBethC on Dec 07, 2016, 09:59 pm
Call it melody2[]
I did that but I need them to play atthe same time....
Title: Re: Play Melody
Post by: Grumpy_Mike on Dec 07, 2016, 10:32 pm
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.
Title: Re: Play Melody
Post by: SarahBethC on Dec 08, 2016, 04:23 am
Heres what I have.
Title: Re: Play Melody
Post by: Grumpy_Mike on Dec 08, 2016, 06:05 am
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.
Title: Re: Play Melody
Post by: SarahBethC on Dec 09, 2016, 03:21 pm
/*
  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() {
  }
Title: Re: Play Melody
Post by: Grumpy_Mike on Dec 09, 2016, 03:49 pm
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 (http://forum.arduino.cc/index.php/topic,148850.0.html)

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
Title: Re: Play Melody
Post by: DocStein99 on Dec 09, 2016, 07:27 pm
Or, buy Mike's book and read that, which explains how to make your own polyphonic midi synthesizers.