千本櫻 聲音實作
程式碼
#define A0 1
#define A1 441
#define A2 495
#define A3 556
#define A4 589
#define A5 661
#define A6 742
#define AL1 221
#define AL2 248
#define AL3 278
#define AL4 294
#define AL5 330
#define AL6 371
#define AL7 416
#define AH1 882
#define AH2 990
#define AH3 1112
#define AH4 1178
#define AH5 1322
#define AH6 1484
#define AH7 1665
//以上部分是定义是把每个音符和频率值对应起来,其实不用打这么多,但是都打上了,后面可以随意编写A调的各种歌,我这里用A+数字表示音符,AH+数字表示上面有点的那种音符,AL+数字表示下面有点的那种音符。这样后面写起来比较好识别。
#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.25
#define SIXTEENTH 0.625
//这部分是用英文对应了拍子,这样后面也比较好看
int tune[] =
{
A0,A0,A0,A3,A5,
A6,A0,A0,A5,A6,A0,A0,A5,
A6,AH1,A5,A6,A3,A0,A3,A5,
A6,A0,A0,A5,A6,A0,A0,A5,
A6,AH3,AH1,AH2,A6,A0,A3,A5,
A6,A0,A0,A5,A6,A0,A0,A5,
A6,AH1,A5,A6,A3,A5,A1,A2,
A3,AH1,A6,AH3,
AH2,AH3,AH2,AH1,AH2,A6,A0,
A6,A6,A6,A6,AH1,AH2,AH3,
A6,A6,A6,A5,A5,A6,
A6,A6,A6,A6,AH1,AH2,AH3,
A6,A6,A6,AH4,AH4,AH3,
A6,A6,A6,A6,AH1,AH2,AH3,
A6,A6,A6,A5,A5,A6,
A6,A6,A6,A6,AH1,AH2,AH3,
AH6,A5,A5,A6,A6
};//这部分就是整首曲子的音符部分,用了一个序列定义为tune,整数
float duration[]=
{
1,1,1,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
1,1,1,1,
0.5,0.25,0.25,0.5,0.5,1,1,
1,1,1,0.25,0.25,0.25,0.25,
1,1,0.5,0.5,0.5,0.5,
1,1,1,0.25,0.25,0.25,0.25,
1,1,0.5,0.5,0.5,0.5,
1,1,1,0.25,0.25,0.25,0.25,
1,1,0.5,0.5,0.5,0.5,
1,1,1,0.25,0.25,0.25,0.25,
1+0.5,0.5,0.5,0.5,1
};//这部分是整首曲子的接拍部分,也定义个序列duration,浮点(数组的个数和前面音符的个数是一样的,一一对应么)
int length;//这里定义一个变量,后面用来表示共有多少个音符
int tonePin=4;//蜂鸣器的pin
void setup()
{
pinMode(tonePin,OUTPUT);
//设置蜂鸣器的pin为输出模式
length = sizeof(tune)/sizeof(tune[0]);
//这里用了一个sizeof函数,可以查出tone序列里有多少个音符
}
void loop()
{
for(int x=0;x<length;x++)//循环音符的次数
{
tone(tonePin,tune[x]);//此函数依次播放tune序列里的数组,即每个音符
delay(450*duration[x]);//每个音符持续的时间,即节拍duration,400是调整时间的越大,曲子速度越慢,越小曲子速度越快,自己掌握吧
noTone(tonePin);//停止当前音符,进入下一音符
}
delay(5000);//等待5秒后,循环重新开始
}
救護車聲音實作
程式碼
int buzzerPin=4;
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
ambulenceSiren(buzzerPin);
}
void ambulenceSiren(int pin) {
tone(pin, 400);
delay(500);
tone(pin, 800);
delay(500);
noTone(pin);
}
警車聲音實作
程式碼
int buzzerPin=4;
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
policeSiren(buzzerPin);
}
void policeSiren(int pin) {
for (int i=150; i<1800; i++) { //upward tone
tone(pin, i, 10);
delay(1);
}
for (int i=1800; i>150; i--) { //downward tone
tone(pin, i, 10);
delay(1);
}
}
消防車聲音實作
程式碼
int buzzerPin=4;
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
policeSiren(buzzerPin);
}
void policeSiren(int pin) {
for (int i=750; i<1450; i++) { //upward tone
tone(pin, i, 10);
delay(3.5);
}
for (int i=1450; i>750; i--) { //downward tone
tone(pin, i, 10);
delay(3.5);
}
}
兩隻老虎
#include "pitches.h"
#include <SimpleTimer.h>
SimpleTimer ABC;
#define sp D2
int melody_TwoTiger[] = {
_C6, _D6, _E6, _C6, _C6, _D6, _E6, _C6, _E6, _F6, _G6, _E6, _F6, _G6, _G6, _A6, _G6, _F6, _E6, _C6, _G6, _A6, _G6, _F6, _E6, _C6, _D6, _G5, _C6, 0, _D6, _G5, _C6, 0
};
byte noteDurations_TwoTiger[] = {
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 3, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
};
void PlayTwoTiger() {
PlayMusicV1(melody_TwoTiger,
noteDurations_TwoTiger,
sizeof(noteDurations_TwoTiger),
1);
}
void setup() {
Serial.begin(9600);
ABC.setInterval(200, PlayTwoTiger);
}
void loop() {
ABC.run();
}
void PlayMusicV1(int melody[], byte duration[], int melodyLen, float freMul) {
int static count = 0;
Serial.print("melodyLen count=");
Serial.print(count);
tone(sp, melody[count]*freMul);
byte static NextCount = 0;
if (NextCount < duration[count]) {
NextCount++;
}
else {
count = count + 1;
NextCount = 0;
}
if (count > melodyLen) count = 0;
}
小星星
#include "pitches.h"
#include <SimpleTimer.h>
SimpleTimer ABC;
#define sp D2
int melody_SmailStar[] = {
_C6, _C6, _G6, _G6, _A6, _A6, _G6, 0, _F6, _F6, _E6, _E6, _D6, _D6, _C6, 0, _G6, _G6, _F6, _F6, _E6, _E6, _D6, 0, _G6, _G6, _F6, _F6, _E6, _E6, _D6, 0, _C6, _C6, _G6, _G6, _A6, _A6, _G6, 0, _F6, _F6, _E6, _E6, _D6, _D6, _C6, 0
};
byte noteDurations_SmailStar [] = {
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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
void PlaySmailStar() {
PlayMusicV1(melody_SmailStar,
noteDurations_SmailStar,
sizeof(noteDurations_SmailStar),
1);
}
void setup() {
Serial.begin(9600);
ABC.setInterval(200, PlaySmailStar);
}
void loop() {
ABC.run();
}
void PlayMusicV1(int melody[], byte duration[], int melodyLen, float freMul) {
int static count = 0;
Serial.print("melodyLen count=");
Serial.print(count);
tone(sp, melody[count]*freMul);
byte static NextCount = 0;
if (NextCount < duration[count]) {
NextCount++;
}
else {
count = count + 1;
NextCount = 0;
}
if (count > melodyLen) count = 0;
}
我的小寶寶
#include "pitches.h"
#include <SimpleTimer.h>
SimpleTimer ABC;
#define sp D2
int melody_little [] = {
_C6, _C6, _D6, _E6, _E6, _D6, _C6, _D6, _C6, 0, _E6, _E6, _F6, _G6, _G6, _G6, _F6, _E6, _F6, _E6, 0, _G6, 0, _C6, 0, _G6, 0, _C6, 0, _E6, 0, _D6, 0, _C6 , 0, 0, 0
};
byte noteDurations_little [] = {
2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
};
void Playlittle baby () {
PlayMusicV1(melody_little ,
noteDurations_little ,
sizeof(noteDurations_little ),
1);
}
void setup() {
Serial.begin(9600);
ABC.setInterval(200, Playlittle );
}
void loop() {
ABC.run();
}
void PlayMusicV1(int melody[], byte duration[], int melodyLen, float freMul) {
int static count = 0;
Serial.print("melodyLen count=");
Serial.print(count);
tone(sp, melody[count]*freMul);
byte static NextCount = 0;
if (NextCount < duration[count]) {
NextCount++;
}
else {
count = count + 1;
NextCount = 0;
}
if (count > melodyLen) count = 0;
}
小毛驢
#include "pitches.h"
#include <SimpleTimer.h>
SimpleTimer ABC;
#define sp D2
int melody_Little [] = {
_C6, _C6, _C6, _E6, _G6, _G6, _G6, _G6, _A6, _A6, _A6, _C7, _G6, 0, _F6, _F6, _A6, _E6, _E6, _E6, _E6, _D6, _D6, _D6, _D6, _G6, _G6, _C6, _C6, _C6, _E6, _G6, _G6, _G6 , _G6, _A6, _A6, _A6, _C7, _G6, 0, _F6, _F6, _F6, _A6, _E6, _E6, _E6, _E6, _E6, _E6, _D6, _D6, _D6, _E6, _C6, 0
};
byte noteDurations_Little [] = {
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
4, 4, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 4, 4
};
void PlayLittle () {
PlayMusicV1(melody_Little ,
noteDurations_Little ,
sizeof(noteDurations_Little ),
1);
}
void setup() {
Serial.begin(9600);
ABC.setInterval(200, PlayLittle );
}
void loop() {
ABC.run();
}
void PlayMusicV1(int melody[], byte duration[], int melodyLen, float freMul) {
int static count = 0;
Serial.print("melodyLen count=");
Serial.print(count);
tone(sp, melody[count]*freMul);
byte static NextCount = 0;
if (NextCount < duration[count]) {
NextCount++;
}
else {
count = count + 1;
NextCount = 0;
}
if (count > melodyLen) count = 0;
}