Arduino Forum

Using Arduino => Audio => Topic started by: smithn123 on Apr 12, 2018, 10:19 pm

Title: Two buzzerpins playing simultaneously
Post by: smithn123 on Apr 12, 2018, 10:19 pm
I am trying to make two buzzers go at the same time with different notes, while having LED pins. The attached is the test code I've created, but I can't seem to make them play at the same time. It only plays one buzzer at a time. Individually, it recognizes and plays both buzzers, but not together.


CODE:

const int c = 261;
const int d = 293;
const int e = 329;
const int f = 349;
const int g = 392;
const int a = 440;
const int b = 493;
const int cH = 523;
const int dH = 587;
const int eH = 659;

 
const int buzzerPin = 8;
const int buzzerPin2 = 5;
const int ledPin1 = 12;
const int ledPin2 = 13;
 
int counter = 0;

void beep(int note, int duration)
{
  //Play tone on buzzerPin
  tone(buzzerPin, note, duration);
 
  //Play different LED depending on value of 'counter'
  if(counter % 2 == 0);
 
  }
 

  void beep2(int note2, int duration2)
{
  //Play tone on buzzerPin
 
  tone(buzzerPin2, note2, duration2);
 
  //Play different LED depending on value of 'counter'
  if(counter % 2 == 0);
 
   
  }


 
void setup()
{
  //Setup pin modes
  pinMode(buzzerPin, OUTPUT);
  pinMode(buzzerPin2, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}
 
void loop()
{
 beep(c, 250), beep2(e, 250);
 delay(500);
 beep(d, 250), beep2(f, 250);
 delay(500);
 beep(e, 250), beep2(g, 250);
 delay(500);
 beep(f, 250), beep2(a, 250);
 delay(500);
 beep(g, 250), beep2(b, 250);
 delay(500);
 beep(a, 250), beep2(cH, 250);
 delay(500);
 beep(b, 250), beep2(dH, 250);
 delay(500);
 beep(cH, 250), beep2(eH, 250);
  delay(500);

  beep2(b, 250);
  delay(500);
  beep2(a, 250);
  delay(500);
  beep2(f, 250);
  delay(500);
  beep2(e, 250);
  delay(500);
  beep2(d, 250);
  delay(500);
  beep2(c, 250);
  delay(500);
 

}
Title: Re: Two buzzerpins playing simultaneously
Post by: slipstick on Apr 13, 2018, 12:42 am
The Arduino Reference information is useful e.g. https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/

Particularly the part where it says:
"Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect."

Steve
Title: Re: Two buzzerpins playing simultaneously
Post by: Grumpy_Mike on Apr 13, 2018, 12:44 am
Quote
it recognizes and plays both buzzers, but not together.
Yes that is right, the tone function can only play one note on one pin at any one time. It says so in the instructions to that call.
https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/ (https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/)

You will have to find a polyphonic tone libiary, google for one and read the instructions about how to use it.
Title: Re: Two buzzerpins playing simultaneously
Post by: nathan_ramanathan on Apr 17, 2018, 05:02 am
Dude, its ur lucky day.

I just released a library yesterday that does exactly this!!

Here is a link to get started

https://github.com/nathanRamaNoodles/MusicWithoutDelay-LIbrary (https://github.com/nathanRamaNoodles/MusicWithoutDelay-LIbrary)

Hope u have fun!
If you have questions, feel free to ask.  Also, don't forget to star the project on Github and share it with friends.

This library is meant to be user-friendly.