用Arduino做一个简单的音乐播放器
不多说,先上图:lol
static/image/hrline/line7.png
程序其实很简单,就是读数组里面的频率,然后就tone()播出去了。
#define LENGTH 16
int music_1 = {1318, 1318, 1397, 1568, 1568, 1397, 1318, 1175, 1046, 1046, 1175, 1318, 1318, 1318, 1175, 1175};
int music_2 = {1318, 1318, 1397, 1568, 1568, 1397, 1318, 1175, 1046, 1046, 1175, 1318, 1175, 1175, 1046, 1046};
int music_3 = {1175, 1175, 1318, 1046, 1175, 1318, 1397, 1318, 1046, 1175, 1318, 1397, 1318, 1175, 1046, 1175, 784, 784};
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
delay(5000);
Serial.println("");
while(true) {
Serial.println("Music Start.");
for(int i = 0; i < LENGTH; i++) {
digitalWrite(13, !digitalRead(13));
if((i == 14) || (i == 12)) {
tone(8, music_1, 500);
delay(600);
Serial.println(music_1);
i++;
continue;
}
tone(8, music_1, 200);
delay(300);
Serial.println(music_1);
}
for(int i = 0; i < LENGTH; i++) {
digitalWrite(13, !digitalRead(13));
if((i == 14) || (i == 12)) {
tone(8, music_2, 500);
delay(600);
Serial.println(music_2);
i++;
continue;
}
tone(8, music_2, 200);
delay(300);
Serial.println(music_2);
}
for(int i = 0; i < LENGTH + 2; i++) {
switch(i) {
case 16:
digitalWrite(13, !digitalRead(13));
tone(8, music_3, 500);
delay(600);
Serial.println(music_3);
i++;
continue;
break;
case 5:
case 6:
case 10:
case 11:
digitalWrite(13, !digitalRead(13));
tone(8, music_3, 100);
delay(150);
Serial.println(music_3);
break;
default:
digitalWrite(13, !digitalRead(13));
tone(8, music_3, 200);
delay(300);
Serial.println(music_3);
break;
}
}
for(int i = 0; i < LENGTH; i++) {
digitalWrite(13, !digitalRead(13));
if((i == 14) || (i == 12)) {
tone(8, music_2, 500);
delay(600);
Serial.println(music_2);
i++;
continue;
}
tone(8, music_2, 200);
delay(300);
Serial.println(music_2);
}
delay(1000);
Serial.println("Music End");
Serial.println("");
Serial.println("");
delay( 1000 );
}
}
最后当然是连线图啦
现在1有更人性化的版本了!一次上电只播放一次!
#define LENGTH 16
int music_1 = {1318, 1318, 1397, 1568, 1568, 1397, 1318, 1175, 1046, 1046, 1175, 1318, 1318, 1318, 1175, 1175};
int music_2 = {1318, 1318, 1397, 1568, 1568, 1397, 1318, 1175, 1046, 1046, 1175, 1318, 1175, 1175, 1046, 1046};
int music_3 = {1175, 1175, 1318, 1046, 1175, 1318, 1397, 1318, 1046, 1175, 1318, 1397, 1318, 1175, 1046, 1175, 784, 784};
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
delay(1000);
Serial.println("");
Serial.println("Music Start.");
for (int i = 0; i < LENGTH; i++) {
digitalWrite(13, !digitalRead(13));
if ((i == 14) || (i == 12)) {
tone(8, music_1, 500);
delay(600);
Serial.println(music_1);
i++;
continue;
}
tone(8, music_1, 200);
delay(300);
Serial.println(music_1);
}
for (int i = 0; i < LENGTH; i++) {
digitalWrite(13, !digitalRead(13));
if ((i == 14) || (i == 12)) {
tone(8, music_2, 500);
delay(600);
Serial.println(music_2);
i++;
continue;
}
tone(8, music_2, 200);
delay(300);
Serial.println(music_2);
}
for (int i = 0; i < LENGTH + 2; i++) {
switch (i) {
case 16:
digitalWrite(13, !digitalRead(13));
tone(8, music_3, 500);
delay(600);
Serial.println(music_3);
i++;
continue;
break;
case 5:
case 6:
case 10:
case 11:
digitalWrite(13, !digitalRead(13));
tone(8, music_3, 100);
delay(150);
Serial.println(music_3);
break;
default:
digitalWrite(13, !digitalRead(13));
tone(8, music_3, 200);
delay(300);
Serial.println(music_3);
break;
}
}
for (int i = 0; i < LENGTH; i++) {
digitalWrite(13, !digitalRead(13));
if ((i == 14) || (i == 12)) {
tone(8, music_2, 500);
delay(600);
Serial.println(music_2);
i++;
continue;
}
tone(8, music_2, 200);
delay(300);
Serial.println(music_2);
}
delay(1000);
Serial.println("Music End");
Serial.println("");
Serial.println("");
delay( 1000 );
while(true) {
}
} 来段mp3,听一下。。。。。 等有了硬件一定要试试你的代码 看看是不是真的那么神奇:D
页:
[1]