使用ESP32-S3驱动蜂鸣器发出音乐

335 字
2 分钟
使用ESP32-S3驱动蜂鸣器发出音乐

使用工具#

硬件#

ESP32-S3开发板 2个以上无源蜂鸣器

软件#

midishow网站用于获取midi文件 FL Studio 进一步编辑midi文件 midi to arduino网站 用于将midi文件转换成arduino文件

开发过程#

先在midishow获取midi文件,再使用FL Studio进行分轨。(由于一个蜂鸣器只能发出一个音符,所以要把音轨分离,分多少轨取决于蜂鸣器数量,我这里有两个蜂鸣器,所以分了两个轨)把分轨的midi转换成arduino代码,修改成esp32所支持的格式。上传连接线路即可。

代码#

这里代码太长了(2000多行),所以仅展示部分。

int tonePin = 0;
int tonePin2 = 1;
void setup() {
// put your setup code here, to run once:
// call the song function with digital pin
Serial.begin(115200);
ledcSetup(0, 2000, 12);
ledcAttachPin(12, 0);
ledcSetup(1, 2000, 12);
ledcAttachPin(3, 1);
xTaskCreate(
taskOne, /* Task function. */
"TaskOne", /* String with name of task. */
60000, /* Stack size in bytes. */
NULL, /* Parameter passed as input of the task */
0, /* Priority of the task. */
NULL /* Task handle. */
);
xTaskCreate(
taskTwo, /* Task function. */
"TaskTwo", /* String with name of task. */
60000, /* Stack size in bytes. */
NULL, /* Parameter passed as input of the task */
0, /* Priority of the task. */
NULL /* Task handle. */
);
}
void taskOne(void *parameter){
ledcWriteTone(tonePin, 369);
delay(300.0);
ledcWriteTone(tonePin, 369);
delay(300.0);
ledcWriteTone(tonePin, 369);
delay(300.0);
...
}
void taskTwo(void *parameter){
delay(900.0);
ledcWriteTone(tonePin2, 493);
delay(600.0);
ledcWriteTone(tonePin2, 369);
delay(600.0);
...
}
void loop() {}

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

使用ESP32-S3驱动蜂鸣器发出音乐
https://blog.dotuoodo.top/posts/beeper-music/
作者
DOTUOODO
发布于
2023-02-24
许可协议
CC BY-NC-SA 4.0

评论

Profile Image of the Author
DOTUOODO
Nothing is immortal, but at least we can be extraordinary.
公告
欢迎来到我的博客!目前还在施工中...
分类
标签

文章目录