multiple speaker problems

I am making a “piano glove” where each finger has a switch that emits a different tone. I am running each finger to a different speaker so that you can play multiple tones at once and get a chord. One speakers code looks like this:

digitalWrite(speakerOut, HIGH);
delayMicroseconds(956);
digitalWrite(speakerOut, LOW);
delayMicroseconds(956);

The problem is that when I set up the loop to play multiple speakers, the sounds distort because each speaker has to wait for the other speakers to play. Also, i can’t run them without the delay because i need microseconds and the arduino doesn’t check itself fast enough.

Does anyone have any advice to fix this problem? Is there a way to have the speakers all running simultaneously without looping them? I am new to the arduino so it may be something simple I don’t know. Any help would be greatly appreciated. Thanks

My son asked me a very similar question the other night except he was turning LEDs on and off and wanted to control them independently rather than sequentially. My suggestion was to use counter variables and a minimal delay which would control the amount of time any particular led was on or off.

For example, instead of using a delay of 300 ms all at one shot, put a 1 millisend delay at the end of the main loop and then set up counters at the beginning of the main loop that you increment with “var++”. Each time through the loop is 1 millisecond. 300 times through the loop would be 300 milliseconds (roughly). Use “if” (or “switch”) statements to check your counters if enough time has passed to turn the led (or sound) on and also if enough time has passed to turn it off again. Don’t forget to reset your variables back to 0 when your timers have expired.

If you need help with the actual code I can give you an example. I used 3 variables for each led I wanted to turn on and off. The first is the counter and incremented at the top of the loop (ledcnt1++). The second held the amount of time the led should be on (ledon1). The third held the amount of time the led should be off (ledoff1). When the “ledcnt1” reaches the “ledoff1” time it would turn the led on, when the counter reached the “ledoff1 + ledon1” time the led would be turned off and “ledcnt1” reset to 0. End of the loop delays 1 ms. Initialize the counters to 0 and initialize your on and off variables in the setup function.

This is a very basic way to do it. A more elegant way to do it would be to set an alarm though I am not sure at this point if that is supported on avr/Arduino. Here is a little info on setting up a timer:

http://www.gnu.org/software/libc/manual/html_node/Setting-an-Alarm.html

In fact there is probably an even better basic way than I describe but it is the first thing that came to mind and it worked for us.