利用Arduino及配套的蜂鸣器等,可以实现音乐的演奏。由于本人水平所限,该视频仅仅截取了原曲的一部分,而且音调有偏差的地方,还请多多包涵!
Arduino-The Truth that You Leave
https://github.com/johnrickman/LiquidCrystal_I2C
#include
#define Do0 262
#define Re0 294
#define Mi0 330
#define Fa0 349
#define Sol0 395
#define La0 440
#define Si0 494
#define Do1 523
#define Re1 587
#define Mi1 659
#define Fa1 698
#define Sol1 784
#define La1 880
#define Si1 988
#define Do2 1046
#define Re2 1175
#define Mi2 1318
#define Fa2 1397
#define Sol2 1568
#define La2 1760
#define Si2 1976
//各个声调的频率
LiquidCrystal_I2C lcd(0x27, 16, 2);//初始化LCD
int pin=3; //自行选择作为输出的接口
int scale[]={
Do0,Re0,Mi0,Fa0,Sol0,La0,Si0,Do1,Re1,Mi1,Fa1,Sol1,La1,Si1,Do2,Re2,Mi2,Fa2,Sol2,La2,Si2};
//定义音调数组
int song[]={
12,6,8,12,0,6,8,12,0,6,8,12,0,6,0,8,0,8,0,8,0,8,8,0,8,0,9,0,10,0,11,0,13,19,13,15,19,0,
13,15,19,0,13,17,18,0,19,0,19,0,9,0,9,0,12,12,12,12,9,9,9,0,12,12,12,12,9,10,11,0,10,0,0,10,0,0,0,12,12,12,12,9,9,9,0,12,12,12,12,9,10,11,0,12,0,0,9,10,11,0,0
,6,7,8,12,0,0,11,10,9,0,0,10,11,10,0,9,0,8,0,0,0,12,12,12,12,9,9,9,0,12,12,12,12,9,10,11,0,10,0,10,13,14,15,0,0,0,19,19,19,19,16,16,16,0,19,19,19,19,16,17,18,0,
17,0,10,13,13,14,15,14,13,10,8,9,0,19,19,19,19,16,16,16,0,19,19,19,19,16,17,18,0,19,19,19,0,17,0,18,17,13,14,15,19,19,0,0,18,17,16,0,16,0,0,21,0,17,16,16,17,16,0,15,15,0,19,19,19,19,
16,16,16,0,19,19,19,19,16,17,19,0,20,0,8,10,13,15,17,20,0,0,0,0,0,10,9,8,7,8,0,0,8,8,9,0,0,10,9,8,7,8,0,0,8,8,3,0,0,10,9,8,7,8,0,0,8,9,0,12,0,10,0,12,0,0,0,10,9,8,7,8,0,0,8,0,9,0,0,10,9,8,7,8,0,0,8,0,3,0,0,10,9,8,7,8,0,0,8,0,12,0,10,0,12,0,0,0,0,0,
12,12,0,8,8,0,12,12,0,9,9,0,8,9,10,10,10,0,9,8,10,12,12,0,0,12,12,0,8,8,0,12,12,0,9,9,0,7,8,9,10,10,10,15,14,12,0,0,12,12,0,8,8,0,12,12,0,9,9,8,9,8,0,0,9,12,12,0,8,8,0,12,12,0,9,9,8,9,8,0,0,0,0,0,0};
//以数组的形式存放歌曲音调
void setup()
{
pinMode(pin,OUTPUT);
lcd.init();//初始化LCD
lcd.backlight(); //点亮LCD背光灯
lcd.setCursor(1, 0);//调整光标位置为第0行(即物理上的第1行)第1列(下同)
lcd.print("the truth that");//输出相应的文字
lcd.setCursor(4, 1);
lcd.print("you leave");
delay(5000);
lcd.clear();//清屏
lcd.setCursor(3, 0);
lcd.print("by Pianoboy");
delay(5000);
lcd.clear();
lcd.print("Give me a like!");
}
void loop()
{
for(int i=0;i<425;i++)
{
if(song[i]!=0)
{
tone(pin,scale[song[i]-1]); //播放对应的音调,原型为tone(输出管脚,频率(Hz))
}
else
{
noTone(pin);//终止播放
}
delay(120);
noTone(pin);
delay(60);
}
Display();
delay(5000);
}
void Display()
{
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Thank you!");
delay(5000);
lcd.clear();
}