Playing two buzzers at once?

Hello everyone. I am working on a project that plays music with piezo buzzers, but some parts of the song have overlapping notes. I can't seem to figure out how to play two notes on two different buzzers at once.

I'm working with a modified version of this code for playing music on a piezo buzzer that is found on the arduino site: https://www.arduino.cc/en/tutorial/melody

In order to play sharp notes, I converted the sequence of notes into a list of strings. The approach that I am taking in order to play multiple notes at once is creating a multidimensional array where each row has a different set of notes, which will be played on separate buzzers. Here is my code:

int speaker1=12;
int speaker2=11;
int speaker3=10;

int led=8;
int length=11;
int sets=2;

String gg="gg";
String aas="aas";
String b="b";
String c="c";
String cs="cs";
String d="d";
String ds="ds";
String e="e";
String f="f";
String fs="fs";
String g="g";
String gs="gs";
String a="a";
String as="as";
String B="B";
String C="C";
String D="D";
String DS="DS";
String CS="CS";
String E="E";
String s=" ";

String notes1[]={f, a, c, s, a, s, f, d, d, d, s};
String notes2[]={d, f, a, s, f, s, d, gg, gg, gg, s};
int beats[]={2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5};
int tempo=200;
String allNotes[2][11]={notes1, notes2};

void playTone(int tone, int duration){
  for (long i=0; i<duration*1000L; i+=tone*2){
    digitalWrite(speaker1, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speaker1, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(String note, int duration){
  String names[]={"gg", "aas", "b", "c", "cs", "d", "ds", "e", "f", "fs", "g", "gs", "a", "as", "B", "C", "CS", "D", "DS", "E"};
  int tones[]={2551, 2146, 2024, 1915, 1805, 1700, 1608, 1515, 1433, 1351, 1276, 1205, 1136, 1073, 1012, 956, 903, 852, 804, 759};
  for (int i=0; i<20; i++){
    if (names[i]==note){
      playTone(tones[i], duration);
    }
  }
}

void setup(){
  pinMode(speaker1, OUTPUT);
  pinMode(led, OUTPUT);
}

void loop(){
  for (int j=0; j<length; j++){
      for (int i=0; i<sets; i++){
        if (allNotes[i][j]==" "){
          delay(beats[j]*tempo);
        }
        else{
          playNote(allNotes[i][j], beats[j]*tempo);
        }
        delay(tempo/4);
    }

  }
}

Please note that I am a beginner and I haven't had a lot of time to refine my code. It may be a bit messy.

Do you have any suggestions of how to approach this?

I don't believe that the tone() function allows simultaneous use of multiple pins: tone() - Arduino Reference

You may be able to do something yourself with low level manipulation pins with timers.

Hi,

cwduffy01:
Hello everyone. I am working on a project that plays music with piezo buzzers, but some parts of the song have overlapping notes. I can't seem to figure out how to play two notes on two different buzzers at once.

I'm working with a modified version of this code for playing music on a piezo buzzer that is found on the arduino site: https://www.arduino.cc/en/tutorial/melody

Also have a look at:
Play a Melody using the tone() function

The code you currently have is playing a tone by writing speakerPin HIGH and LOW in the playTone()

The tone() function can play a tone and leave it playing while your code does something else.

However, the tone() function will have to use a different pin from your playTone()

You could try putting this at the end of setup():

  pinMode( speaker2, OUTPUT );
  tone(speaker2, 1000, 10000);      // 1kHz tone for 10 seconds on speaker2 pin

You should then hear this tone on speaker2 while your melody plays on speaker1.

Yours,
TonyWilk

=

TonyWilk:
You could try putting this at the end of setup():

  pinMode( speaker2, OUTPUT );

tone(speaker2, 1000, 10000);      // 1kHz tone for 10 seconds on speaker2 pin



You should then hear this tone on speaker2 while your melody plays on speaker1.

Ah, so you can put an argument at the beginning of tone to chose which pin it plays? Is that right? What do the other two arguments do?

Allow me to suggest that you try to remember the reference pages in future. Just look at the top of every forum page, select Resources then Reference.

Not only does that stop you being the 97th person today asking for information they could easily have found themselves, it also has the advantage that you get your answer much quicker because you don't have to wait for someone to notice your question in the forum and decide to answer it.

Steve