Go Down

### Topic: Digital Pins Problem (Read 12789 times)previous topic - next topic

#### polonos13

##### Jan 27, 2011, 02:46 pmLast Edit: Jan 27, 2011, 02:50 pm by polonos13 Reason: 1
Hi !!!
I am building a laser harp project and at first i was using one and only speaker.My project has 6 laser strings so it has to play six notes. I decided to connect 6 speakers,one for each one of the lasers so that every speaker plays only one note.The problem is that i can't use more than three digital pins.How i understood that In the code below as you see i am using pins 3-8 one for each speaker.
when i run the program the only speakers that producing the tones are the first three (pin 3, pin 4 and pin 5).But if i change the code like the one that is on the right again only the first three pins are working (pin 3,pin 8 amd pin 7).Why this is happening ?

Code: [Select]
`void setup(void){  Serial.begin(9600);  aSpeaker1.begin(3); // first speaker at digital pin 3                           aSpeaker1.begin(3);  aSpeaker2.begin(4); // second speaker at digital pin 4                       aSpeaker2.begin(8);  aSpeaker3.begin(5); // third speaker at digital pin 5                          aSpeaker3.begin(7);  aSpeaker4.begin(6); // fourth speaker at digital pin 6                         aSpeaker4.begin(6);  aSpeaker5.begin(7); // fifth speaker at digital pin 7                            aSpeaker5.begin(5);  aSpeaker6.begin(8); // sixth speaker at digital pin 8                           aSpeaker6.begin(4);}`

Here is the full source code of my project

Code: [Select]
`#include <Tone.h>int notes[] = { NOTE_A3,                NOTE_B3,                NOTE_C4,                NOTE_D4,                NOTE_E4,                NOTE_F4,                NOTE_G4 };int lightPin1 = 0;  //define a pin for Photo resistorint lightPin2 = 1;  //define a pin for Photo resistorint lightPin3 = 2;  //define a pin for Photo resistorint lightPin4 = 3;  //define a pin for Photo resistorint lightPin5 = 4;  //define a pin for Photo resistorint lightPin6 = 5;  //define a pin for Photo resistorint reading;// You can declare the tones as an arrayTone aSpeaker1;Tone aSpeaker2;Tone aSpeaker3;Tone aSpeaker4;Tone aSpeaker5;Tone aSpeaker6;void setup(void){  Serial.begin(9600);  aSpeaker1.begin(3); // first speaker at digital pin 3  aSpeaker2.begin(4); // second speaker at digital pin 4  aSpeaker3.begin(5); // third speaker at digital pin 5  aSpeaker4.begin(6); // fourth speaker at digital pin 6  aSpeaker5.begin(7); // fifth speaker at digital pin 7  aSpeaker6.begin(8); // sixth speaker at digital pin 8}void loop(void){  reading = analogRead(lightPin1);  if(reading < 50) // when the laser hits the photoresistor the value of the photoresistor goes to 280 and when not hit goes to 20.  {                    // So if reading goes under 50 (that means that the laser beam is interupted)    aLaser1.play(130,400); // produce 130 Hz for 400 msecond to the first speaker.  }    reading = analogRead(lightPin2);  if(reading < 50)  {    aLaser2.play(140,400);  }    reading = analogRead(lightPin3);  if(reading < 50)  {    aLaser3.play(160,400);  }    reading = analogRead(lightPin4);  if(reading < 50)  {    aLaser4.play(170,400);  }    reading = analogRead(lightPin5);  if(reading < 50)  {    aLaser5.play(190,400);  }    reading = analogRead(lightPin6);  if(reading < 50)  {    aLaser6.play(200,400);  }         }`

#### PaulS

#1
##### Jan 27, 2011, 03:02 pm
Which version of the IDE are you using? Which Arduino?

According to Tone.cpp, only one pin can be used as a Tone pin at any given time.

#### polonos13

#2
##### Jan 27, 2011, 03:08 pmLast Edit: Jan 27, 2011, 03:31 pm by polonos13 Reason: 1

Which version of the IDE are you using? Which Arduino?

I am using Arduino Duemilanove.

Quote
According to Tone.cpp, only one pin can be used as a Tone pin at any given time.

According to this i could only play tones with one speaker but in my case i have three speakers working.
Can you tell a solution for my prblem ?

I've just read the changelog of the tone library and if you see at 0002 it says FIxed:Multiple pins.

Quote
\$Id: changelog.txt 120 2010-07-17 19:01:15Z bhagman@roguerobotics.com \$

Tone Library

------- ----------- -------- --------
0001    B Hagman    09/08/02 Initial coding
0002    B Hagman    09/08/18 Fixed: Multiple pins.
0003    B Hagman    09/08/18 Fixed: Moved initialization from constructor to
begin().
0004    B Hagman    09/09/26 Fixed: Problems with ATmega8.
0005    B Hagman    09/11/23 Fixed: Scanned prescalars for best fit on 8 bit
timers
09/11/25 Fixed: Pin toggle method to XOR.
09/11/25 Fixed: timer0 from being excluded.
B Hagman    10/07/17 Fixed: (more) problems with ATmega8 (thanks to Pete62)

#### TerryKing

#3
##### Jan 27, 2011, 03:34 pm
I believe there are only 3 hardware timers and so only 3 "PM-Like" functions at one time.. Perhaps that is the limitation you are experiencing...

#### polonos13

#4
##### Jan 27, 2011, 03:43 pm

I believe there are only 3 hardware timers and so only 3 "PM-Like" functions at one time.. Perhaps that is the limitation you are experiencing...

Is there any way to make it work with 6 speakers

#### keeper63@cox.net

#5
##### Jan 27, 2011, 04:16 pm
I recall there was a library or something (posted in the old forum) for performing PWM on many pins for control of an RGB LED array. Perhaps you could take that, build your own tone library over the top of it, and use that? Even so, it may not be fast enough for your purpose (that is, the tones won't have the range). I would suggest moving to the Mega, or alternatively, you could use two (or more) regular Arduinos, each sitting on an SPI or I2C bus, one master - each outputting 3 PWM channels. Finally, unless you need or want the effect - you don't need 3 (or 6) speakers; use a simple audio mixer circuit instead.
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

#### polonos13

#6
##### Jan 27, 2011, 04:43 pm

you don't need 3 (or 6) speakers; use a simple audio mixer circuit instead.

How does it work the audio mixer circuit
Is there anything in the forum about the audio mixer circuit that i can find ?

#### Grumpy_Mike

#7
##### Jan 27, 2011, 06:50 pm
First off don't cross post it really annoys people and wastes there time. Don't do it again.    :0

Quote
How does it work the audio mixer circuit ?

This is an external circuit that uses op amps and resistors to combine several signals, google it.
Quote
Is there anything in the forum about the audio mixer circuit

Probably not as this is more of your hi-fi audio circuit than your embedded processor circuit.

#### polonos13

#8
##### Jan 27, 2011, 09:11 pm
I ve figured how to make my laser harp playing a little better than before.You see my problem was that when i had one speaker for producing the notes,when i interrupt one and only laser beam the tone played well.But when i was interrupting two laser beams at the same time the notes for each interrupted beam was played at the same time from the one and only speaker and the mixing of the two tones was awfull. Now i 've conncted 3 speakers and i have 6 laser beams. The first laser goes with speaker 1 ,the second laser goes with speaker 2 ,the third laser goes with speaker 3, the fourth lase goes with speaker 1, the fifth laser goes with speaker 2 and the sixth laser goes with speaker 3.The only problem is when i hit at the same time the laser 1 and 4 together or the laser 2 and 5 together or the laser 3 and 6 together because these pairs are using the same speaker. So i've changed my source code a little and if one of the 3 combinations comes up a totally different sound is played by the speaker connected with the pair.

Code: [Select]
`#include <Tone.h>int lightPin1 = 0;  //define a pin for Photo resistorint lightPin2 = 1;  //define a pin for Photo resistorint lightPin3 = 2;  //define a pin for Photo resistorint lightPin4 = 3;  //define a pin for Photo resistorint lightPin5 = 4;  //define a pin for Photo resistorint lightPin6 = 5;  //define a pin for Photo resistorint reading;int reading2;// You can declare the tones as an arrayTone aLaser1;Tone aLaser2;Tone aLaser3;Tone aLaser4;Tone aLaser5;Tone aLaser6;void setup(void){  Serial.begin(9600);  aLaser1.begin(3);  aLaser2.begin(4);  aLaser3.begin(5);}void loop(void){  reading = analogRead(lightPin1);  if(reading < 50)   {    aLaser1.play(130,400);  }    reading = analogRead(lightPin2);  if(reading < 50)   {    aLaser2.play(140,400);  }    reading = analogRead(lightPin3);  if(reading < 50)   {    aLaser3.play(160,400);  }    reading = analogRead(lightPin4);  if(reading < 50)   {    aLaser1.play(170,400);  }    reading = analogRead(lightPin5);  if(reading < 50)   {    aLaser2.play(190,400);  }    reading = analogRead(lightPin6);  if(reading < 50)   {    aLaser3.play(200,400);  }      ////////////////////////////////////////////////////  //         This is the code for the combinations          //  ////////////////////////////////////////////////////   reading = analogRead(lightPin6);  reading2 = analogRead(lightPin3);  while(reading < 50 && reading2 < 50)   {    aLaser3.play(220,400);           reading = analogRead(lightPin6);       reading2 = analogRead(lightPin3);       if(reading >50 || reading2 >50)       aLaser3.stop();  }      reading = analogRead(lightPin5);  reading2 = analogRead(lightPin2);  while(reading < 50 && reading2 < 50)   {    aLaser2.play(240,400);           reading = analogRead(lightPin5);       reading2 = analogRead(lightPin2);       if(reading >50 || reading2 >50)       aLaser2.stop();  }      reading = analogRead(lightPin4);  reading2 = analogRead(lightPin1);  while(reading < 50 && reading2 < 50)   {    aLaser1.play(260,400);           reading = analogRead(lightPin4);       reading2 = analogRead(lightPin1);       if(reading >50 || reading2 >50)       aLaser1.stop();  }       }//void loop `