建立旋律程式
一個簡單的蜂鳴器可以連線到 ESP32 控制器來發出聲音。它也可以用來產生旋律。本文使用兩個不同的例子,在 Arduino 上使用 C 程式碼讓蜂鳴器播放旋律。在第一個例子“一閃一閃亮晶晶”中,播放詩歌的音符;在第二個例子中,使用另一首著名詩歌“鈴兒響叮噹”來播放音符,同時讓兩個彩色 LED 燈開關來創造一個燈光與音樂的表演。
示例 1:使用蜂鳴器和 ESP32 播放詩歌音符
電路是使用 Arduino 軟體製作的,程式也是用它編寫的。這裡給出的 C 語言程式定義了不同鋼琴鍵的音樂音符頻率。playNotes() 函式用於根據音符的持續時間和播放時間使蜂鳴器引腳變高或變低。
電路設計步驟和編碼
步驟 1 − 將 ESP32 微控制器連線到麵包板。
步驟 2 − 將蜂鳴器也放在麵包板上。
步驟 3 − 將蜂鳴器的正極連線到 ESP32 的 D26 引腳。將蜂鳴器的負極連線到 ESP32 的 GND 引腳。
步驟 4 − 使用 Arduino 為播放旋律編寫 C 程式。
步驟 5 − 使用 USB 資料線將 ESP32 連線到電腦。
步驟 6 − 編譯並將程式碼傳輸到 ESP32,然後檢查結果。
程式碼
// set piano notes (frequencies)
#define c 3830
#define d 3400
#define e 3038
#define f 2864
#define g 2550
#define a 2272
#define b 2028
#define C 1912
#define R 0
// buzzer pin is D26 on ESP32
int buzzOut = 26;
int startFlag = 1;
int flagPlay=1;
// this rhythm_measure is the tempo of the song
long rhythm_measure = 10000;
int pauseTime = 500;
int do_nothing = 2;
int audioTone = 0;
int beat1 = 0;
long timedur = 0;
void setup() {
// set the buzzer as output component for melody
pinMode(buzzOut, OUTPUT);
if (startFlag) {
// specify the baud rate
Serial.begin(9600);
}
}
// these are the music notes for Twinkle twinkle little star poem
double songNotes[] = {
c,c,g,g,a,a,g,
f,f,e,e,d,d,c,
g,g,f,f,e,e,d,
g,g,f,f,e,e,d,
c,c,g,g,a,a,g,
f,f,e,e,d,d,c
};
int MAX_COUNT = sizeof(songNotes) / 2;
// this is the function which makes the buzzer high and low while playing the poem notes
void playNotes() {
long passed_time = 0;
if (audioTone > 0) {
while (passed_time < timedur) {
digitalWrite(buzzOut,HIGH);
delayMicroseconds(audioTone / 2);
digitalWrite(buzzOut, LOW);
delayMicroseconds(audioTone / 2);
passed_time += (audioTone);
}
}
else {
for (int n = 0; n < do_nothing; n++) {
delayMicroseconds(timedur);
}
}
}
void loop() {
for (int m=0; m<MAX_COUNT; m++) {
audioTone = songNotes[m];
beat1 = 50;
timedur = beat1 * rhythm_measure;
if( flagPlay != 0) {
playNotes();
}
delayMicroseconds(pauseTime);
}
delay(5000);
}
檢視結果
程式碼編譯並傳輸/上傳到 ESP32 後,即可看到結果。
示例 2:使用 LED、蜂鳴器和 ESP32 播放詩歌音符並製作跳舞的燈光
給出了 C 語言程式。playNotes() 函式用於根據音符的持續時間和播放時間使蜂鳴器引腳變高或變低。loop() 函式中的程式碼還使紅色和綠色 LED 燈隨著音樂播放而變高和變低。
電路設計步驟和編碼
步驟 1 − 將 ESP32 放置在麵包板上。將 GND 連線到負軌。
步驟 2 − 連線蜂鳴器。將其正極連線到 ESP32 的 D26 引腳,將其負極連線到麵包板的負軌。
步驟 3 − 將紅色 LED 連線到麵包板上。將其較小的引腳連線到負軌,較大的引腳(正極)透過電阻連線到 ESP32 的 D12 引腳。
步驟 4 − 將綠色 LED 連線到麵包板上。將其較小的引腳連線到負軌,較大的引腳(正極)透過電阻連線到 ESP32 的 D13 引腳。
步驟 5 − 使用 Arduino 編寫 C 程式來播放旋律並使兩個 LED 燈交替開關。
步驟 6 − 使用 USB 資料線將 ESP32 連線到電腦。
步驟 7 − 編譯並將程式碼傳輸到 ESP32,然後檢查結果。
程式碼
// set piano notes (frequencies)
#define R 0
#define cnote 3830
#define d 3400
#define e 3038
#define f 2864
#define g 2550
#define b 2028
#define C 1912
// buzzer pin is D26 on ESP32
int buzzOut = 26;
// Green color LED pin is D13 on ESP32
int LED_green = 13;
// Red color LED pin is D12 on ESP32
int LED_red = 12;
int LED_flag = 0;
int flag1 = 0;
// this rhythm_measure is the tempo of the song
long rhythm_measure = 15000;
int pauseDur = 2000;
int doNothing = 200;
int audioTone = 0;
int beat1 = 0;
long timedur = 0;
//These are the music notes for the Jingle bells poem
int songNotes[] = { e, e, e,
e, e, e,
e, g, cnote, d, e,
f, f, f,
f, f, e, e,
e, e, e,
d, d, e, d, g, R
};
int MaxNo = sizeof(songNotes) / 2;
int songBeats[] = { 16, 16, 32,
16, 16, 32,
16, 16, 16, 8, 48,
16, 16, 16,
16, 16, 8, 8,
16, 16, 16,
16, 16, 16, 16, 8, 48
};
void changeFlag() {
if (LED_flag == 0){
LED_flag = 1;
} else {
LED_flag = 0;
}
}
void playNote() {
long passed_time = 0;
if (audioTone > 0) {
while (passed_time < timedur) {
digitalWrite(buzzOut,HIGH);
delayMicroseconds(audioTone / 2);
digitalWrite(buzzOut, LOW);
delayMicroseconds(audioTone / 2);
passed_time += (audioTone);
}
}
else {
for (int m = 0; m < doNothing; m++) {
delayMicroseconds(timedur);
}
}
}
void setup() {
// set the buzzer as output component for melody
pinMode(buzzOut, OUTPUT);
// set the LEDs as output component for dancing lights
pinMode(LED_green, OUTPUT);
pinMode(LED_red, OUTPUT);
if (flag1) {
Serial.begin(9600); // Set serial out if we want flag1ging
}
}
void loop() {
for (int n=0; n<MaxNo; n++) {
audioTone = songNotes[n];
beat1 = songBeats[n];
changeFlag();
if (LED_flag == 1){
// make the green and red lights on and off one after the other
digitalWrite(LED_green,HIGH);
digitalWrite(LED_red,LOW);
} else {
digitalWrite(LED_green,LOW);
digitalWrite(LED_red,HIGH);
}
timedur = beat1 * rhythm_measure;
playNote();
delayMicroseconds(1000);
if (LED_flag == 1){
digitalWrite(LED_green,LOW);
digitalWrite(LED_red,LOW);
} else {
digitalWrite(LED_green,LOW);
digitalWrite(LED_red,LOW);
}
if (flag1) {
Serial.print(n);
Serial.print(":");
Serial.print(beat1);
Serial.print(" ");
Serial.print(audioTone);
Serial.print(" ");
Serial.println(timedur);
}
}
}
檢視結果 - 示例 2
圖:顯示播放旋律並使 LED 燈開關的電路。
本文使用兩個不同的例子,介紹了使用 ESP32 和蜂鳴器播放旋律的方法。首先介紹了播放詩歌旋律的方法,在第二個例子中,播放另一個旋律的同時,LED 燈交替開關,製作燈光秀。
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP