Pages: [1]   Go Down
Author Topic: How do I run two buzzers at the same time?  (Read 1252 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 27
// no comment
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 -
http://rcarduino.blogspot.com/2012/08/the-must-build-arduino-project-illutron.html

Duane B


Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 27
// no comment
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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: http://sealedabstract.com/code/bassdll-an-arduino-piezo-music-library/

and

http://www.jeremyblum.com/2010/09/05/driving-5-speakers-simultaneously-with-an-arduino/

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

Thanks!
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
tone1.play(NOTE_C4);
tone2.play(NOTE_A4);
delay(noteDuration);
tone1.stop();
tone2.stop();

Code:
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.
« Last Edit: January 13, 2013, 01:46:41 pm by johnwasser » Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 27
// no comment
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 27
// no comment
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Problem:  smiley-confuse

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:

Code:
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
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 27
// no comment
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks johnwasser!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
// no comment
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!  smiley-sad-blue

Thanks!

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

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 27
// no comment
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Got it.
Logged

Pages: [1]   Go Up
Jump to: