经过一晚的折腾程序成功上传,能够流畅的在Beetle ESP32-C3主板运行。在2023年国庆节来临之际,作品《伟大祖国国庆节献礼》献给亲爱的祖国。这借此机会作品也献给辛劳的蘑菇云团队和我一样热爱科教的广大基层科技教师。
设计思路:设置三个按键和三只LED灯,按下不同的按键,喇叭分别播放音乐《歌唱祖国》、《我的祖国》、《我和我的祖国》,三个LED灯伴随音乐闪烁。
器材:Beetle ESP32-C3主控板、3个按键、3个LED灯、面包板、3pin数据线、喇叭、面包板
编程软件:Arduino IDE 2.1.0
接线照片:
代码:
代码
#include "pitches.h"
int Led1=1;//定义LED 接口
int Led2=2;//定义LED 接口
int Led3=4;//定义LED 接口
int buttonpin1=5; //定义按键开关传感器接口
int buttonpin2=6; //定义按键开关传感器接口
int buttonpin3=7; //定义按键开关传感器接口
int val1;//定义数字变量val
int val2;
int val3;
int melody1[] = {
NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_F4,NOTE_E4,NOTE_D4,NOTE_C4,NOTE_G3,NOTE_G3,
NOTE_C4,NOTE_G3,NOTE_E4,NOTE_C4,NOTE_G4,NOTE_A4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_C5,NOTE_C5,NOTE_A4,NOTE_G4,NOTE_F4,NOTE_A4,
NOTE_G4,0,NOTE_G4,NOTE_G4,NOTE_A4,NOTE_A4,NOTE_D4,NOTE_D4,NOTE_D4,NOTE_G4,NOTE_F4,NOTE_E4,NOTE_G3,NOTE_G3,NOTE_G4,NOTE_G4,
NOTE_A4,NOTE_G4,NOTE_F4,NOTE_E4,NOTE_D4,NOTE_C4,0,NOTE_G4,NOTE_G4,NOTE_C5,NOTE_C5,NOTE_A4,NOTE_A4,NOTE_G4,NOTE_F4,NOTE_G4,
NOTE_A4,NOTE_D4,NOTE_D4,NOTE_G4,NOTE_G4,NOTE_A4,NOTE_G4,NOTE_F4,NOTE_E4,NOTE_D4,NOTE_C4,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0};
int noteDurations3[] = {
8,8,8,8,8,8,2.67,2.67,8,8,8,8,5.3,16,5.3,5.3,8,8,8,8,8,8,2.67,2.67,8,8,8,8,5.3,16,5.3,5.3,8,8,8,8,8,8,5.3,5.3,8,8,8,8,5.3,16,
5.3,5.3,8,8,8,2.67,8,8,8,2.67,4,8,4,8,5.3,5.3,8,8,8,8,8,8,8,5.3,16,5.3,5.3,8,8,8,8,8,8,8,5.3,16,5.3,5.3,8,8,8,2.67,8,8,8,2.67,
4,2.67,8,2.67,2.67,2.67,2.67,8,8,8,8,8,8,8,5.3,16,2.67,2.67,8,8,8,8,8,8,8,5.3,16,2.67,2.67,8,8,8,2.67,8,8,8,2.67,2.67,4,8,2.67};
void setup()
{
pinMode(Led1,OUTPUT);//定义LED 为输出接口
pinMode(Led2,OUTPUT);//定义LED 为输出接口
pinMode(Led3,OUTPUT);//定义LED 为输出接口
pinMode(buttonpin1,INPUT);//定义按键开关传感器为输出接口
pinMode(buttonpin2,INPUT);//定义按键开关传感器为输出接口
pinMode(buttonpin3,INPUT);//定义按键开关传感器为输出接口
}
void loop()
{
if(val1=digitalRead(buttonpin1)){
if(val1==HIGH)//当按键开关传感器检测有信号时,LED 闪烁
{
for (int thisNote1 = 0; thisNote1 < 147; thisNote1++) {
int noteDuration1 = 1000/noteDurations1[thisNote1];
tone(8, melody1[thisNote1],noteDuration1);
int pauseBetweenNotes1 = noteDuration1 * 1.30;
delay(pauseBetweenNotes1);
noTone(8);
digitalWrite(Led1,HIGH);
delay(pauseBetweenNotes1);
digitalWrite(Led1,LOW);
}
digitalWrite(Led1,LOW);
}
else
{
digitalWrite(Led1,LOW);
}
}
else if(val2=digitalRead(buttonpin2)){
if(val2==HIGH)//当按键开关传感器检测有信号时,LED 闪烁
{
for (int thisNote2 = 0; thisNote2 < 109; thisNote2++) {
int noteDuration2 = 2000/noteDurations2[thisNote2];
tone(8, melody2[thisNote2],noteDuration2);
int pauseBetweenNotes2 = noteDuration2 * 1.30;
delay(pauseBetweenNotes2);
noTone(8);
digitalWrite(Led2,HIGH);
delay(pauseBetweenNotes2);
digitalWrite(Led2,LOW);
}
digitalWrite(Led2,LOW);
}
else
{
digitalWrite(Led2,LOW);
}
}
else if(val3=digitalRead(buttonpin3)){
if(val3==HIGH)//当按键开关传感器检测有信号时,LED 闪烁
{
for (int thisNote3 = 0; thisNote3 < 133; thisNote3++) {
int noteDuration3 = 2000/noteDurations3[thisNote3];
tone(8, melody3[thisNote3],noteDuration3);
int pauseBetweenNotes3 = noteDuration3 * 1.30;
delay(pauseBetweenNotes3);
noTone(8);
digitalWrite(Led3,HIGH);
delay(pauseBetweenNotes3);
digitalWrite(Led3,LOW);
}
digitalWrite(Led3,LOW);
}
else
{
digitalWrite(Led3,LOW);
}
}
}
最后,音箱上有彩蛋,注意到了吗?感谢粒子小姐姐!
hacker_2023.09.28
666