Pages: [1]   Go Down
Author Topic: Run two speakers simultaneously  (Read 551 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

Im trying to make some kind of simple portable karaoke machine. Therefor I want three speakers to make sound at the same time. (I know I named it two speakers, but I guess when you know how to build two, the third one should not be that hard, right?)   
I tried it with an if statement, that the speakers have the same statements, but it will first run the first one, and when it is done the second one.
Is there some kind of code which will say "do this and this simultaneously"?

My code at this point is (including the if statements & setup for LCD)

Code:
/*
int ledPin2;
int ledPin1;
int buzzerPin = 9;
int buzzerPin1 = 8;
int counter = 0;

const int fs = 46;
const int cs = 69;
const int e = 82;
const int fs1 = 93;
const int c = 65;
const int d = 73;
const int b = 62;
const int eh = 330;
const int fsh = 185;
const int fsh1 = 370;
long time;

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


const int beep(int note, int duration)
  {
  //Play tone on buzzerPin
  tone(buzzerPin, note, duration);
if(counter % 2 == 0)
  {
    digitalWrite(ledPin1, HIGH);
    delay(duration);
    digitalWrite(ledPin1, LOW);
  }
  else
  {
    digitalWrite(ledPin2, HIGH);
    delay(duration);
    digitalWrite(ledPin2, LOW);
  }
  //Stop tone on buzzerPin
  noTone(buzzerPin);

  delay(50);
    counter++;

  }
 
 
const int beep1(int note, int duration)
  {
  //Play tone on buzzerPin
  tone(buzzerPin1, note, duration);
if(counter % 2 == 0)
  {
    digitalWrite(ledPin1, HIGH);
    delay(duration);
    digitalWrite(ledPin1, LOW);
  }
  else
  {
    digitalWrite(ledPin2, HIGH);
    delay(duration);
    digitalWrite(ledPin2, LOW);
  }
  //Stop tone on buzzerPin
  noTone(buzzerPin1);

  delay(50);
    counter++;

  }


void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
   pinMode(buzzerPin, OUTPUT);
   time=millis();
 
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 0);
  lcd.print("She was more like");
    lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("A beauty queen");
 
 
 
  if (millis()>10){
    //BASE
    beep(fs, 250);   beep(cs, 250);   beep(e, 250);   beep(fs1, 250);   beep(e, 250);  beep(cs, 250);   beep(b, 250);   beep(cs, 250);
  }
  if (millis()>10){
    //chords
     beep1(fsh, 300); delay(400); beep1(eh, 1000); delay(400); beep1(fsh1, 500); delay(300); beep1(eh, 1000); delay(400);
  }
   
}

I already found a code that makes the speakers work simultaneously, but I don't really know how it works. Also I tried the timing with this code, and that was also harder..
Anyways this is the code which I found:

Code:
int speakerOut = 9;
int speakerOut1 = 7;
int speakerOut2 = 8;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'}; 
int tones[] = {110, 69, 73, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;

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

void loop() {
  analogWrite(speakerOut, 0);   
   analogWrite(speakerOut1, 1);
    analogWrite(speakerOut2, 2);
  for (count = 0; count < MAX_COUNT; count++) {
    for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody[count*2 + 1]) {     
          analogWrite(speakerOut,500);
          analogWrite(speakerOut1,500);analogWrite(speakerOut2,500);
          delayMicroseconds(tones[count2]);
          analogWrite(speakerOut, 0);
          analogWrite(speakerOut1, 0);
          analogWrite(speakerOut2,500);
          delayMicroseconds(tones[count2]);   
     }
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          analogWrite(speakerOut, 0);
          delayMicroseconds(500);
        }
      }
    }
  }
}

Thanks in advance!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Run two speakers simultaneously

...

Quote
Therefor I want three speakers to make sound at the same time ...

I like it!

Quote
Code:
  if (millis()>10){

This would be true most of the time wouldn't it?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11197
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want three speakers to make sound at the same time.

Do you mean you want to superimpose some beeps etc over the audio track?

Do you actually have two (or three) audio channels? (If so - why? Do you have three microphones?)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do you actually have two (or three) audio channels? (If so - why? Do you have three microphones?)
Microphones? Did you mean speakers?
I want to work with the beeps the arduino can make. So there will be no "real audio" track (just the beeps like in the expample of play melody arduino has http://www.arduino.cc/en/Tutorial/PlayMelody). To make it sound like actual music I want to make a base line, a singing line and maybe some chords or something, make it recognizable.
For instance that it would play an C E G all at the same time from different speakers...
So there will be three outputs, I'm not really sure it they are then audio channels? (Do I use an audio channel?)

Quote
  if (millis()>10){

This would be true most of the time wouldn't it?

Haha yes! I know, but I was hoping that my arduino would think "hey, these are the same, I should play them simultaneously". That did not work out..
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you will find it hard to get sufficient frequency accuracy for more than one channel at a time on the Uno, because the Uno has only one 16-bit counter/timer. Also you'll be limited to square wave output. Why not use the Arduino to control a synthesiser chip? Something like http://hackaday.com/2011/12/17/midi-synth-arduino-shield/.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4031
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not just feed 3 powered speakers the same 1 signal?

Logged

Examples can be found in your IDE.

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11197
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Microphones? Did you mean speakers?

No, I meant microphones.

The Arduino output is going to be a nasty harsh square wave and if that's all the speakers are producing then I can't imagine this system sounding at all pleasant - I guessed you were going to overlay these beeps over an audio track as some sort of prompt or timing hint. Given that you mentioned three speakers, I guessed that you would have three independent output channels. If they aren't going to be overlaid on separate channels, I couldn't see any point in producing separate outputs from the Arduino. I don't really understand why you would want more than one microphone and one output channel for karaoke but then I don't know how these things work and I guessed you might be trying to have multiple people singing at the same time or something like that.

So, what's the plan - are the three speakers actually playing different sounds?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: