Quick question about Tone library

Okay, just a quick question:

When using the tone library, can I change the pin a tone object is outputted on by calling begin() within loop()? This won't mess up timers or anything, and the tone will no longer play on the previous pin once begin()'d on another pin, right?

The reason I ask is because I want to be able to change the volume level of the tone between multiple levels by using different resistors on different pins. e.g, if I want a louder tone, I would output the tone on a pin connected to a speaker through a 100R resistor, then for a quieter tone on a pin through a 200R resistor.

And, taking this a little further, is there a simple way I can modify the tone library to output the same thing on 2 pins at the same time? So I could get 4 different volume levels if used with the resistor setup mentioned above??

Thanks!
:slight_smile:

modify the tone library to output the same thing on 2 pins at the same time?

Looking at the Tone.cpp that came with Arduino-0018, I find the line

timer2_pin_mask = digitalPinToBitMask(_pin);

as a simple test, you could try to change that into

timer2_pin_mask = digitalPinToBitMask(8) | digitalPinToBitMask(9);

(i.e. hardcoding two pin numbers (which must be pins that appear on the same port), more lines need to be changed to make it more flexible)

Okay, so after fiddling around with the tone library, I have made a modification that allows the tone to be played on two pins at the same time with the same timer (it still works using only one pin, too).

Thanks for pointing out that line, drhex!! That's what did it.

Code on pastebin:
(credit goes to whoever made the tone library, I just tweaked it a bit)
.cpp file
.h file
Keywords file (exact same as tone library)

To use it, just use begin(), but now, it works with two parameters for two pins! (it is still usable with only one parameter)
WARNING!!! The pins must be in the same port! (I dunno what would happen if they weren't)