Simultaneous PWM to multiple output devices

My project is a musical instrument that generates a tone when the divided voltage reading through a photo-resistive cell dips below a threshold. There are multiple sensors, each with its own speaker. The problem I am having is that when one tone initiates on one speaker, no other tone will play on the other speakers. I believe there should be no problem generating 2 or more distinct PWM signals to the speakers. I have looked at manipulating the PWM frequencies more directly, but I believe I am limited by the discrete set of divisors.

This is a stub of the code to illustrate the scheme, and perhaps highlight any causes of my problem:

void setup() {}
void loop() {
if (sensor1 < threshold1) tone(outputPin1, frequency1, duration1)
if (sensor2 < threshold2) tone(outputPin2, frequency2, duration2)
}

If I trigger the sensors simultaneously, the first tone to play will continue to do so until the trigger action is disengaged, while the other remains silent. My desire is to get any speaker to play at any time, regardless of other speakers playing.

I believe the tone library deals with this issue of different simultaneous outputs by assigning free timers with each instance of the class, so a mega board having more timers can play more tones at the same time then a uno board can. Or something like that. :wink:

Lefty

Hi,

You are not using Arduino PWM there, you are using the Tone library.

Try using Arduino PWM, by outputting using analogWrite to multiple pins...