第1篇:Arduino与ESP32开发板的安装方法
第2篇:ESP32 helloword第一个程序示范点亮板载LED
第3篇:vscode搭建esp32 arduino开发环境
第4篇:vscode+platformio搭建esp32 arduino开发环境
第5篇:doit_esp32_devkit_v1使用pmw呼吸灯实验
D5连接喇叭正极,GND连接喇叭负极
一、乐理准备
十二平均律(12-equal temperament)又称十二等程律。
一个八度分成12份,八个八度的十二平均音率对应的频率如下(从33Hz到7902Hz)。
相邻两个音之间的倍数是2^(1/12),约等于1.0594630943592952645618252949463)。这样一来,高八度的do频率是低八度do的两倍。
频率与钢琴上的按键是对应的:
我们把频率440赫兹作为中央C ,C调的歌就是把频率440赫兹当作do,乐谱中写作:1=C。
相邻的两个音之间的差一个半音,12345671之间的间隔分别是“全全半全全全半”。
D调的歌就是把从440后面第二个音494赫兹当作do,乐谱中写作1=D。
————————————————
示范代码:
#include <Arduino.h>
// put function declarations here:
//定义蜂鸣器控制管脚
#define beep_pin 5 //连无源喇叭
// float k=1.0594631;
struct YINFU{ //定义音符
int freq;
float Duration;
};
//1 2 3 4 5 6 7 1 2 3 4 5 6 7
//C D E F G A A# B C1 D1, E1, F1, G1, A1, B1
//440,494,554,587,659,740,784 831,880,988,1109, 1175,1319,1480,1661
struct YINFU YUEPU[128]={
{831,0.55}, {740,0.15}, {622,0.55}, {740,0.15}, {988,0.3}, {831,0.15}, {740,0.15}, {831,0.8}, {0,0.55}, {0,0.3},
{622,0.3}, {740,0.3}, {831,0.3}, {740,0.3}, {622,0.3}, {494,0.15}, {440,0.15}, {740,0.15}, {622,0.15}, {554,0.4}, {0,0.3},
{554,0.3}, {622,0.3}, {740,0.3}, {740,0.3}, {831,0.3}, {622,0.15}, {554,0.15}, {494,0.4}, {0,0.3}, {0,0.3},
{740,0.3}, {622,0.3}, {554,0.15}, {494,0.15}, {440,0.15}, {494,0.15}, {392,0.5}, {0,0.3}, {0,0.3},
};
//涛声依旧
struct YINFU YUEPU_TSYJ[2048]={
//6 0 //6 3 5 6 5 5
{1480,0.55}, {0,0.55},{1480,0.55}, {1109,0.55}, {1319,0.55}, {1480,0.3}, {1319,0.15},{1,0.8},{1319,0.55},
//1 2 3 3 3 5 6 5 3 2 3
{880,0.35}, {988,0.35}, {1109,0.55},{0,0.3},{1109,0.55},{0,0.15},{0,0.55},{1109,0.15},{1319,0.55},{1480,0.2},{1319,0.2},{1109,0.2},{988,0.2},{1109,0.85}, {1,0.8},
//6 5 3 5 1 6 5 5
{1480,0.55}, {1319,0.55},{1109,0.15},{1319,0.15},{880,0.55},{740,0.15},{659,0.55},{0,0.3},{659,0.85},
//6 1 2 2
{740,0.15}, {880,0.35}, {988,0.55}, {0,0.3},{988,0.55},
//2 6 3 5 6 5 5
{988,0.15},{1480,0.35}, {1109,0.55},{1319,0.15},{1480,0.35},{1319,0.55}, {1319,0.8}, {1319,0.8},{0,0.8},{0,0.8},{0,0.8},
};
void setup() {
pinMode(beep_pin, OUTPUT); //设置引脚为输出模式
}
void loop(){
for(int k=0;k<128;k++){ //顺序演奏128音符
float d=YUEPU_TSYJ[k].Duration;
for(int D=0;D<int(d*YUEPU_TSYJ[k].freq);D++){ //演奏YUEPU[k].freq的音符,时长=d
if(YUEPU_TSYJ[k].freq != 0){ //演奏音符
//频率转化为时间间隔,1秒=500000微秒*2。一半高电平,一半低电平,所以用500000/音符频率。
int freq_to_us=int(500000/YUEPU_TSYJ[k].freq);
digitalWrite(beep_pin,HIGH); //一半高电平
delayMicroseconds(freq_to_us);
digitalWrite(beep_pin,LOW); //一半低电平
delayMicroseconds(freq_to_us);
}
else{ //停顿
digitalWrite(beep_pin,LOW); //低电平,不发声
// delayMicroseconds(1000);
delay(1000);
}
}
}
delay(1000);
}
vscode esp32 platformio编译方法参考:
第4篇:vscode+platformio搭建esp32 arduino开发环境