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)
/*
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:
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!