Go Down

Topic: How do I run two buzzers at the same time? (Read 1 time) previous topic - next topic

wherewolfe

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?

johnwasser

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

DuaneB

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



wherewolfe

@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!

johnwasser

#4
Jan 12, 2013, 08:43 pm Last Edit: Jan 13, 2013, 07:46 pm by johnwasser Reason: 1

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: [Select]

tone1.play(NOTE_C4);
tone2.play(NOTE_A4);
delay(noteDuration);
tone1.stop();
tone2.stop();


Code: [Select]

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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

wherewolfe

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

wherewolfe

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:

Code: [Select]

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

johnwasser

Replace WProgram.h or Wiring.h with Arduino.h in Tone.cpp.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

wherewolfe


wherewolfe

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


johnwasser


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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e


Go Up