Connect 2 speakers to arduino uno

sketch1_bb_0.jpg (484270Bytes)

Hi everyone,

 

I'm sorry to bother with such a basic problem but I'm trying to connect 2 speakers ( a piezo and a 8 Ohm speaker ) to an arduino uno to play some tones. The fact is I can play tones with only one of them at a time.

 

 As a beginner, I don't know it's my code wich is wrong or the breadboard layout, so I uploaded here both of them hoping that someone can show me the light !

 

THX EVERYONE !

Here is the code:

int speakerPin = 8;

int speakerPin2 = 9;

 

int length = 295; 

char notes[] = "EE E CE G  g  C  g e  a b ia gEGA FG E CDb C  g e  a b ia gEGA FG E CDb  GNFR E uaC aCD GNFR E 1 11   GNFR E uaC aCD L  D C   CC C CD EC ag  CC C CDE  CC C CD EC ag  EE E CE G  g  C  g e  a b ia gEGA FG E CDb C  g e  a b ia gEGA FG E CDb EC g u aF Fa  bAAAGFEC ag  EC g u aF Fa  bF FFEDCe ec  "; 

float beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, //Page 1

                2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 4, //Page 2

                1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, //Page4

                1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, //Page 5

                1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1.3, 1.3, 1.3, 1.3, 1.3, 1.3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1.3, 1.3, 1.3, 1, 1, 1, 1, 1, 1, 2 }; //Page 6

 

int tempo = 100;

 

void playTone(int ton1, int duration) {

  for (long i = 0; i < duration * 1000L; i += ton1) {

    tone(speakerPin, ton1);

    tone(speakerPin2, ton1);

    delayMicroseconds(ton1);

  }

  noTone(speakerPin);

  noTone(speakerPin2);

}

 

void playNote(char note, int duration) {

//                        c    c#   d    d#   e    f    f#   g    g#   a    a#   b

  char names[] = { ' ',  '!', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', 'i', 'N', 'R', 'u',  '1', 'L', 'k'}; // [i = b flat] [N = G flat] [R = D#] [u = g#] [1 = C oct. 5] [L = E flat]

  int tones[] =  {   0, 1046, 138, 146, 155, 164, 174, 184, 195, 207, 220, 233, 246, 261, 293, 329, 349, 391, 440, 493, 523, 587, 659, 698, 783, 880, 987, 466, 740, 622, 415, 1046, 622u, 227};

 

  for (int i = 0; i < 34; i++) {

    if (names[i] == note) {

      playTone(tones[i], duration);

    }

  }

}

 

void setup() {

  pinMode(speakerPin, OUTPUT);

  pinMode(speakerPin2, OUTPUT);

}

 

void loop() {

  for (int i = 0; i < length; i++ ) {

    if (notes[i] == ' ') {

      delay(beats[i] * tempo); 

    } else {

      playNote(notes[i], beats[i] * tempo);

    }

 

    delay(tempo / 2); 

  }

}

 

I would suggest reading over the reference page for tone.

http://arduino.cc/en/Reference/Tone

There is mention of noTone().

">Only one tone can be

">Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency."

Ok,I will have to find another solution lol, thx for that Birdmun.