arduino 嗡鸣器 音乐_[求助]怎样实现红外遥控蜂鸣器播放音乐,求大神指正

本文介绍了一种方法,通过arduino配合红外接收模块实现遥控控制蜂鸣器播放预设的音乐。代码中定义了不同音符的频率,并在接收到特定红外信号时播放对应的音乐节奏。
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#define NOTE_0 -1#define NOTE_A1 441

#define NOTE_A2 495

#define NOTE_A3 556

#define NOTE_A4 589

#define NOTE_A5 661

#define NOTE_A6 742

#define NOTE_A7 833#define NOTE_AL1 221

#define NOTE_AL2 248

#define NOTE_AL3 278

#define NOTE_AL4 294

#define NOTE_AL5 330

#define NOTE_AL6 371

#define NOTE_AL7 416#define NOTE_AH1 882

#define NOTE_AH2 990

#define NOTE_AH3 1112

#define NOTE_AH4 1178

#define NOTE_AH5 1322

#define NOTE_AH6 1484

#define NOTE_AH7 1665#define NOTE_E1 330

#define NOTE_E2 350#define NOTE_E3 393

#define NOTE_E4 441

#define NOTE_E5 495

#define NOTE_E6 556

#define NOTE_E7 624#define NOTE_EL1 165

#define NOTE_EL2 175

#define NOTE_EL3 196

#define NOTE_EL4 221

#define NOTE_EL5 248

#define NOTE_EL6 278

#define NOTE_EL7 312#define NOTE_EH1 661

#define NOTE_EH2 700

#define NOTE_EH3 786

#define NOTE_EH4 882

#define NOTE_EH5 990

#define NOTE_EH6 1112

#define NOTE_EH7 1248#define WHOLE 1

#define HALF 0.5

#define QUARTER 0.25

#define EIGHTH 0.25

#define SIXTEENTH 0.625

int t1[] =

{

NOTE_E1,NOTE_E2,NOTE_E3,NOTE_E1,

NOTE_E1,NOTE_E2,NOTE_E3,NOTE_E1,

NOTE_E3,NOTE_E4,NOTE_E5,

NOTE_E3,NOTE_E4,NOTE_E5,

NOTE_E5,NOTE_E6,NOTE_E5,NOTE_E4,NOTE_E3,NOTE_E1,

NOTE_E2,NOTE_EL5,NOTE_E1,NOTE_0

NOTE_E2,NOTE_EL5,NOTE_E1,NOTE_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,1,0.5,0.5,0.5,0.5,0.5,

0.5,1,0.5,0.5,0.5,1,1

};

int length1;

int length2;

int tonepin=0;

int RECV_PIN = 8;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup() {

Serial.begin(9600);

irrecv.enableIRIn();

pinMode(tonepin,OUTPUT);

length1=sizeof(t1)/sizeof(t1[0]);

length2=sizeof(t2)/sizeof(t2[0]);

}

void loop() {

if (irrecv.decode(&results)) {

if (results.value == 0xF08F7) {//1

for(int x=0;x

{

newtone(tonepin,t1[x]);

delay(500*d1[x]);

noTone(tonepin);

}

delay(2000);

}

irrecv.resume();

}

if (irrecv.decode(&results)) {

if (results.value == 0xFD8877) {//2

for(int x=0;x

{

newtone(tonepin,t2[x]);

delay(500*d2[x]);

noTone(tonepin);

}

delay(2000);

}

irrecv.resume();

}

}void newtone(byte tonePin, int frequency, int duration) {

int period = 1000000L / frequency;

int pulse = period / 2;

for (long i = 0; i < duration * 1000L; i += period) {

digitalWrite(tonePin, HIGH);

delayMicroseconds(pulse);

digitalWrite(tonePin, LOW);

delayMicroseconds(pulse);

}

}

以下是一个使用红外遥控控制蜂鸣器播放音乐Arduino代码示例: ``` #include <IRremote.h> //引入红外遥控库 int RECV_PIN = 11; //定义红外遥控接收引脚 IRrecv irrecv(RECV_PIN); //初始化红外遥控接收器 decode_results results; //定义红外遥控解码结果 int buzzerPin = 10; //定义蜂鸣器引脚 //定义播放的音符频率 int notes[] = {262, 294, 330, 349, 392, 440, 494, 523}; void setup() { irrecv.enableIRIn(); //开启红外遥控接收 pinMode(buzzerPin, OUTPUT); //设置蜂鸣器引脚为输出模式 } void loop() { if (irrecv.decode(&results)) //如果有红外遥控信号被接收 { switch (results.value) //根据红外遥控编码值做出相应操作 { case 0xFF6897: //播放C大调音阶 playScale(); break; case 0xFF9867: //播放Happy Birthday playHappyBirthday(); break; case 0xFFB04F: //播放小星星 playLittleStar(); break; } irrecv.resume(); //继续接收红外遥控信号 } } void playScale() { for (int i = 0; i < 8; i++) //循环播放8个音符 { tone(buzzerPin, notes[i]); //使用tone函数播放音符 delay(500); //每个音符播放500毫秒 noTone(buzzerPin); //停止播放音符 } } void playHappyBirthday() { int melody[] = {262, 262, 294, 262, 349, 330, 262, 262, 294, 262, 392, 349, 262, 262, 523, 440, 349, 330, 294, 466, 466, 440, 349, 392, 349}; int noteDurations[] = {4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 2}; for (int i = 0; i < 24; i++) //循环播放24个音符 { int duration = 1000 / noteDurations[i]; //计算每个音符的持续时间 tone(buzzerPin, melody[i], duration); //使用tone函数播放音符 int pauseBetweenNotes = duration * 1.30; //计算每个音符之间的暂停时间 delay(pauseBetweenNotes); //暂停 noTone(buzzerPin); //停止播放音符 } } void playLittleStar() { int melody[] = {262, 262, 294, 294, 330, 330, 294, 262, 262, 294, 294, 330, 330, 294, 262, 294, 262, 220, 220, 262, 262, 294, 294, 262, 220, 262, 262, 294, 294, 330, 330, 294, 262, 262, 294, 294, 330, 330, 294, 262}; int noteDurations[] = {4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4}; for (int i = 0; i < 38; i++) //循环播放38个音符 { int duration = 1000 / noteDurations[i]; //计算每个音符的持续时间 tone(buzzerPin, melody[i], duration); //使用tone函数播放音符 int pauseBetweenNotes = duration * 1.30; //计算每个音符之间的暂停时间 delay(pauseBetweenNotes); //暂停 noTone(buzzerPin); //停止播放音符 } } ``` 在上面的代码中,我们引入了IRremote库来实现红外遥控功能,使用了tone函数来播放音符,同时定义了三个播放不同音乐的函数playScale、playHappyBirthday和playLittleStar,通过红外遥控信号来控制不同的函数的执行。你可以根据自己的需要来修改这个代码,播放不同的音乐
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值