Go Down

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

polonos13

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 resistor
int lightPin2 = 1;  //define a pin for Photo resistor
int lightPin3 = 2;  //define a pin for Photo resistor
int lightPin4 = 3;  //define a pin for Photo resistor
int lightPin5 = 4;  //define a pin for Photo resistor
int lightPin6 = 5;  //define a pin for Photo resistor

int reading;
// You can declare the tones as an array
Tone 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);
  }
 
       
}

marklar

The best way to chop this in 1/2 is to print out the values you are getting to the serial monitor.

Also - I would remove the if statements and just test them all with very simple code and see if that works.
Code: [Select]
void loop(void)
{
    aSpeaker1.play(130,400); // produce 130 Hz for 400 msecond to the first speaker.
    aSpeaker2.play(140,400);
    aSpeaker3.play(160,400);
    aSpeaker4.play(170,400);
    aSpeaker5.play(190,400);
    aSpeaker6.play(200,400);
       
}

Using this technique, you should be able to determine if the problem is a limit or code logic / values being read, etc.


polonos13


The best way to chop this in 1/2 is to print out the values you are getting to the serial monitor.

Also - I would remove the if statements and just test them all with very simple code and see if that works.
Code: [Select]
void loop(void)
{
    aSpeaker1.play(130,400); // produce 130 Hz for 400 msecond to the first speaker.
    aSpeaker2.play(140,400);
    aSpeaker3.play(160,400);
    aSpeaker4.play(170,400);
    aSpeaker5.play(190,400);
    aSpeaker6.play(200,400);
       
}

Using this technique, you should be able to determine if the problem is a limit or code logic / values being read, etc.




Nope same thing happens only the first three are playing

marklar

How about this .. do they play?

Code: [Select]
void loop(void)
{
    aSpeaker4.play(170,400);
    aSpeaker5.play(190,400);
    aSpeaker6.play(200,400);
       
}

polonos13

#4
Jan 27, 2011, 03:57 pm Last Edit: Jan 27, 2011, 04:02 pm by polonos13 Reason: 1

How about this .. do they play?

Code: [Select]
void loop(void)
{
   aSpeaker4.play(170,400);
   aSpeaker5.play(190,400);
   aSpeaker6.play(200,400);
     
}



Same thing again.

The problem is when i declare the pins for each speaker.
It uses the first three pins that i have them in Bold (pin 3 ,pin 4 and pin 5)

Quote
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                          
}


If i change the declaration of the pins to the following , again it uses the first three pins (pin 8 ,pin 7 amd pin 6)

Quote
void setup(void)
{
 Serial.begin(9600);

 aSpeaker1.begin(8 ); // first speaker at digital pin 8                          
 aSpeaker2.begin(7); // second speaker at digital pin 7                      
 aSpeaker3.begin(6); // third speaker at digital pin 6

 aSpeaker4.begin(5); // fourth speaker at digital pin 5                        
 aSpeaker5.begin(4); // fifth speaker at digital pin 4                          
 aSpeaker6.begin(3); // sixth speaker at digital pin 3                          
}


If i have three speakers connected to 8 ,7 and 6 pins they produce sound ,but if i take the cable from pin 7 and connect it to pin 4 or 3 or 5 only the speakers at pin 8 and 7 will continue playing

Go Up