How do I run two buzzers at the same time?

Hi-

I wanted to play a song on the arduino using buzzers. I have a couple of melodies overlapping and I need two buzzers to be playing different melodies at the same time for the song to sound "right". Unfortunately, I learned that there is only one internal timer the Arduino uses to count the duration for tones in the tone() function. I was looking for ways to solve this.

A friend suggested I use a bunch of 555 or 556 timers set to different pulsations and wire those to the buzzers so that, when powered, the buzzers would produce noise at the correct frequencies. I could then control just control these timers with the digitalWrite() function.

What I wanted to ask was: Is this a feasible solution? And is there any easier (and preferably cheaper) alternative to run two buzzers at the same time?

There is a third-party Tone library that can play multiple simultaneous tones.

http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation

There are a few projects outputting four tones, you can go higher, but with 8-bit sound its a bit like mixing colours - too much and it all turns brown.

Four channels - not a library but easy to mod if you have a set sequence -

Duane B

@johnwasser:

this sounds like it will work for me. They said that the play() function is non-blocking without the duration variable, but to play the proper notes at the right time, I will need to use the duration variable. Is there any way around this?

Also, I found a couple other sources to play multiple notes: bassdll – an Arduino Piezo music library | Sealed Abstract

and

If y'all could tell me if these are better or worse, that would be really great.

Thanks!

wherewolfe:
They said that the play() function is non-blocking without the duration variable, but to play the proper notes at the right time, I will need to use the duration variable. Is there any way around this?

You can either stop the playing on demand or wait until the durations have expires:

tone1.play(NOTE_C4);
tone2.play(NOTE_A4);
delay(noteDuration);
tone1.stop();
tone2.stop();
tone1.play(NOTE_C4, noteDuration);
tone2.play(NOTE_A4, noteDuration);
while (tone1.isPlaying() || tone2.isPlaying())
   /* Do nothing else while the tones are playing */ ;

Note: From the documentation it appears that .play() is NON BLOCKING even if duration is specified. The function returns immediately but the tone will continue until the duration expires.

Ok, this should work for my purposes. Thanks johnwasser!

Problem: :~

When I tried to compile my program with the tone library included, I got a bunch of errors stating that several different variables/functions were "not declared in this scope". I tried opening the Tone.cpp file using a C++ IDE, and when I tried to compile it there, I got the same messages. The library was last edited in April 2011, so maybe something changed, I dunno. Any help would be much appreciated.

Here are the error messages I got:

C:\Documents and Settings\home\My Documents\Arduino\libraries\Tone\Tone.cpp: In member function 'void Tone::begin(uint8_t)':
C:\Documents and Settings\home\My Documents\Arduino\libraries\Tone\Tone.cpp:121: error: 'bitWrite' was not declared in this scope
C:\Documents and Settings\home\My Documents\Arduino\libraries\Tone\Tone.cpp:123: error: 'digitalPinToPort' was not declared in this scope
C:\Documents and Settings\home\My Documents\Arduino\libraries\Tone\Tone.cpp:123: error: 'portOutputRegister' was not declared in this scope
C:\Documents and Settings\home\My Documents\Arduino\libraries\Tone\Tone.cpp:124: error: 'digitalPinToBitMask' was not declared in this scope
C:\Documents and Settings\home\My Documents\Arduino\libraries\Tone\Tone.cpp: In member function 'void Tone::play(uint16_t, uint32_t)':
C:\Documents and Settings\home\My Documents\Arduino\libraries\Tone\Tone.cpp:198: error: 'OUTPUT' was not declared in this scope
C:\Documents and Settings\home\My Documents\Arduino\libraries\Tone\Tone.cpp:198: error: 'pinMode' was not declared in this scope
C:\Documents and Settings\home\My Documents\Arduino\libraries\Tone\Tone.cpp:294: error: 'bitWrite' was not declared in this scope
C:\Documents and Settings\home\My Documents\Arduino\libraries\Tone\Tone.cpp: In member function 'void Tone::stop()':
C:\Documents and Settings\home\My Documents\Arduino\libraries\Tone\Tone.cpp:361: error: 'digitalWrite' was not declared in this scope

Replace WProgram.h or Wiring.h with Arduino.h in Tone.cpp.

Thanks johnwasser!

OK- my song is coming along, but I have come to another problem. I am not able to play 3 notes simultaneously. It says in the library description that the number of playable notes does depend on the kind of processor included, but my uno r3 has an ATmega328, which supposedly can play a maximum of three notes. Unfortunately, I can't get the third one to work. Halp! :disappointed_relieved:

Thanks!

P.S: should this thread be moved to another category? It's become more of a software thing...

wherewolfe:
P.S: should this thread be moved to another category? It's become more of a software thing...

Might be time for a new post under Project Guidance. Be sure to include (ALL of) your code and a detailed description of your circuitry.

Got it.