之前学习esp32时发现很多人都在说无源蜂鸣器无法关闭,于是我想出了一个好方法,即没有关闭无源蜂鸣器,又让他一点声音都没有,众所周知,声音是由一个物品的震动发出,物品震动的频率就是声音的频率,所以我们让蜂鸣器发出0赫兹的声音,等于真正的让他不发出声音。无源蜂鸣器接gpio22
#define bp 22
#define chanel 0
typedef struct data {
note_t note; //音符
uint8_t level; //音阶 4 为我们平常时所听到的那个8度
uint16_t interval; //持续时间
int x; //音符播放后的空时间
} note;
note tones[]={
{NOTE_C,4,500,0},
{NOTE_C,4,500,0},
{NOTE_G,4,500,0},
{NOTE_G,4,500,0},
{NOTE_A,4,500,0},
{NOTE_A,4,500,0},
{NOTE_G,4,725,250},
{NOTE_F,4,500,0},
{NOTE_F,4,500,0},
{NOTE_E,4,500,0},
{NOTE_E,4,500,0},
{NOTE_D,4,500,0},
{NOTE_D,4,500,0},
{NOTE_C,4,725,250},
{NOTE_G,4,500,0},
{NOTE_G,4,500,0},
{NOTE_F,4,500,0},
{NOTE_F,4,500,0},
{NOTE_E,4,500,0},
{NOTE_E,4,500,0},
{NOTE_D,4,725,250},
{NOTE_G,4,500,0},
{NOTE_G,4,500,0},
{NOTE_F,4,500,0},
{NOTE_F,4,500,0},
{NOTE_E,4,500,0},
{NOTE_E,4,500,0},
{NOTE_D,4,725,250},
{NOTE_C,4,500,0},
{NOTE_C,4,500,0},
{NOTE_G,4,500,0},
{NOTE_G,4,500,0},
{NOTE_A,4,500,0},
{NOTE_A,4,500,0},
{NOTE_G,4,725,250},
{NOTE_F,4,500,0},
{NOTE_F,4,500,0},
{NOTE_E,4,500,0},
{NOTE_E,4,500,0},
{NOTE_D,4,500,0},
{NOTE_D,4,500,0},
{NOTE_C,4,725,250},
}; //曲子包含所有的音符
//NOTE_C do
//NOTE_Cs 升do
//NOTE_D re
//NOTE_Eb 降mi
//NOTE_E mi
//NOTE_F fa
//NOTE_Fs 升fa
//NOTE_G sol
//NOTE_Gs 升sol
//NOTE_A la
//NOTE_Bb 降ti
//NOTE_B ti
byte tonesSize=sizeof(tones)/sizeof(note);
void xiuzhi(uint8_t channel,unsigned long long deley)
{
ledcWriteTone(channel,0);
delay(deley);
}
void setup()
{
// put your setup code here, to run once:
ledcSetup(chanel,20000,10);
ledcAttachPin(bp,0);
}
void loop()
{
// put your main code here, to run repeatedly:
// ledcWriteTone(0,200);
// delay(200);
// ledcWriteTone(0,500);
// delay(200);
// ledcWriteNote(0,NOTE_C,4);
// delay(500);
for(int i=0;i
#define bp 22
#define chanel 0
typedef struct data {
note_t note; //音符
uint8_t level; //音阶 4 为我们平常时所听到的那个8度
uint16_t interval; //持续时间
int x; //音符播放后的空时间
} note;
note tones[]={
{NOTE_C,4,500,0},
{NOTE_C,4,500,0},
{NOTE_G,4,500,0},
{NOTE_G,4,500,0},
{NOTE_A,4,500,0},
{NOTE_A,4,500,0},
{NOTE_G,4,725,250},
{NOTE_F,4,500,0},
{NOTE_F,4,500,0},
{NOTE_E,4,500,0},
{NOTE_E,4,500,0},
{NOTE_D,4,500,0},
{NOTE_D,4,500,0},
{NOTE_C,4,725,250},
{NOTE_G,4,500,0},
{NOTE_G,4,500,0},
{NOTE_F,4,500,0},
{NOTE_F,4,500,0},
{NOTE_E,4,500,0},
{NOTE_E,4,500,0},
{NOTE_D,4,725,250},
{NOTE_G,4,500,0},
{NOTE_G,4,500,0},
{NOTE_F,4,500,0},
{NOTE_F,4,500,0},
{NOTE_E,4,500,0},
{NOTE_E,4,500,0},
{NOTE_D,4,725,250},
{NOTE_C,4,500,0},
{NOTE_C,4,500,0},
{NOTE_G,4,500,0},
{NOTE_G,4,500,0},
{NOTE_A,4,500,0},
{NOTE_A,4,500,0},
{NOTE_G,4,725,250},
{NOTE_F,4,500,0},
{NOTE_F,4,500,0},
{NOTE_E,4,500,0},
{NOTE_E,4,500,0},
{NOTE_D,4,500,0},
{NOTE_D,4,500,0},
{NOTE_C,4,725,250},
}; //曲子包含所有的音符
//NOTE_C do
//NOTE_Cs 升do
//NOTE_D re
//NOTE_Eb 降mi
//NOTE_E mi
//NOTE_F fa
//NOTE_Fs 升fa
//NOTE_G sol
//NOTE_Gs 升sol
//NOTE_A la
//NOTE_Bb 降ti
//NOTE_B ti
byte tonesSize=sizeof(tones)/sizeof(note);
void xiuzhi(uint8_t channel,unsigned long long deley)
{
ledcWriteTone(channel,0);
delay(deley);
}
void setup()
{
// put your setup code here, to run once:
ledcSetup(chanel,20000,10);
ledcAttachPin(bp,0);
}
void loop()
{
// put your main code here, to run repeatedly:
// ledcWriteTone(0,200);
// delay(200);
// ledcWriteTone(0,500);
// delay(200);
// ledcWriteNote(0,NOTE_C,4);
// delay(500);
for(int i=0;i { ledcWriteNote(chanel,tones[i].note,tones[i].level); delay(tones[i].interval); xiuzhi(chanel,tones[i].x); } }