接线
按钮——————D3
火焰传感器———D2
蜂鸣器—————D7
led灯—————D13
编程思路及目的
利用火焰传感器检测火焰,然后播放音乐,如果中途有人按下按钮,则停止音乐。
主程序
#include "Arduino.h"
int EXIT0 = 0; //定义按键开关调用外部中断0,默认接D2端口
int zhuantai = 0; //设置状态位
/*************蜂鸣器的音乐的**********************/
int note[21][7] = { /*标准频率对照表*/
/*A-G大调降音*/
{221, 248, 278, 294, 330, 371, 416}, //A大调
{248, 278, 294, 330, 371, 416, 467}, //B大调
{131, 147, 165, 175, 196, 221, 248}, //C大调
{147, 165, 175, 196, 221, 248, 278}, //D大调
{165, 175, 196, 221, 248, 278, 294}, //E大调
{175, 196, 221, 248, 278, 294, 330}, //F大调
{196, 221, 248, 278, 294, 330, 371}, //G大调
/*A-G大调标准音*/
{441, 495, 556, 624, 661, 742, 833},
{495, 556, 624, 661, 742, 833, 935},
{262, 294, 330, 350, 393, 441, 495},
{294, 330, 350, 393, 441, 495, 556},
{330, 350, 393, 441, 495, 556, 624},
{350, 393, 441, 495, 556, 624, 661},
{393, 441, 495, 556, 624, 661, 742},
/*A-G大调升音*/
{882, 990, 1112, 1178, 1322, 1484, 1665},
{990, 1112, 1178, 1322, 1484, 1665, 1859},
{525, 589, 661, 700, 786, 882, 990},
{589, 661, 700, 786, 882, 990, 1112},
{661, 700, 786, 882, 990, 1112, 1178},
{700, 786, 882, 990, 1112, 1178, 1322},
{786, 882, 990, 1112, 1178, 1322, 1484},
};
/*************以下变量依据实际修改****************/
int buzzerPin = 7; /*蜂鸣器引脚位置*/
int BPM = 400; /*一拍的时间*/
int Scale = 4; /*设置大调,A-Z分别对应1-7*/
float rate = 0.1; /*发声时间占一拍时间的比率*/
/***** 蜂鸣器唱歌 类似调节编译 可以手动选择音乐*****/
/*
int pitch[] =
{
6, 7, 1, 7, 1, 3, 7, 3, 6, 5, 6, 1, 5, 3, 4, 3, 4, 1, 3, 1, 7, 4, 4, 7, 7, 6, 7, 1, 7, 1, 3, 7, 3, 3, 6, 5, 6, 1, 5, 3, 4, 1, 7, 1, 2, 3, 1, 1, 1, 7, 6, 7, 5, 6, 6, 6, 1, 2, 3, 2, 3, 5, 2, 2, 2, 5, 1, 7, 1, 2, 3, 3, 3, 3, 3, 6, 7, 1, 7, 1, 2, 1, 5, 5, 5, 4, 3, 2, 1, 3, 3, 3, 3, 6, 6, 5, 5, 3, 2, 1, 1, 1, 2, 1, 2, 5, 3, 3, 3, 3, 6, 6, 5, 5, 3, 2, 1, 1, 1, 2, 1, 2, 7, 6, 6, 6, 0,
};
int rise[] =
{
0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
};
float mlength[] =
{
0.500, 0.500, 1.500, 0.500, 1.000, 1.000, 3.000, 1.000, 0.500, 1.000, 1.000, 1.000, 3.000, 1.000, 1.500, 0.500, 0.500, 1.500, 3.000, 1.000, 1.500, 0.500, 0.500, 1.000, 3.000, 0.500, 0.500, 1.500, 0.500, 1.000, 1.000, 3.000, 0.500, 0.500, 1.500, 0.500, 1.000, 1.000, 3.000, 1.000, 1.000, 0.500, 1.500, 1.000, 1.000, 0.500, 0.500, 2.000, 0.500, 0.500, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 0.500, 0.500, 1.500, 0.500, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.500, 0.500, 1.000, 0.500, 0.500, 1.000, 1.000, 1.000, 1.000, 0.500, 0.500, 1.000, 0.500, 0.500, 1.000, 1.500, 0.500, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.500, 0.500, 1.500, 0.500, 0.500, 0.500, 1.000, 1.000, 1.000, 1.500, 0.500, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.500, 0.500, 1.500, 0.500, 0.500, 0.500, 1.000, 1.000, 1.000, 1.500, 0.500, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000,
};*/
//两只老虎
int pitch[] =
{
1, 2, 3, 1, 1, 2, 3, 1, 3, 4, 5, 3, 4, 5, 5, 6, 5, 4, 3, 1, 0, 0, 1, 2, 3, 1, 1, 2, 3, 1, 3, 4, 5, 3, 4, 5,
};
int rise[] =
{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
float mlength[] =
{
1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 2.000, 1.000, 1.000, 2.000, 0.500, 0.500, 0.500, 0.500, 1.000, 1.000, 1.000, 3.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 2.000, 1.000, 1.000, 2.000,
};
/*追光者*/
/*int pitch[] =
{
0, 0, 0, 0, 6, 7, 1, 7, 3, 3, 5, 5, 1, 2, 1, 2, 2, 5, 3, 6, 7, 1, 7, 3, 3, 5, 1, 5, 1, 2, 3, 1, 7, 6, 7, 1, 1, 3, 2, 7, 6, 5, 6, 7, 7, 2, 1, 0, 1, 7, 1, 0, 6, 6, 7, 1, 7, 6, 5, 0, 1, 2,
};
int rise[] =
{
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1, 2, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2,
};*/
//因为arduino引脚少 就少用条件编译了
void setup() {
Serial.begin(9600); //串口调试,9600波特率
attachInterrupt(digitalPinToInterrupt(2), stateChange, RISING); //触发中断进入函数stateChange();RISING(上升的)
attachInterrupt(digitalPinToInterrupt(3), wbzd, FALLING); //触发中断进入函数wbzd();FALLING(下降沿的)
pinMode(3, INPUT); //中断引脚
pinMode(2, INPUT); //中断引脚
pinMode(6, INPUT); //历史预留
pinMode(13, OUTPUT); //灯光引脚
pinMode(7, OUTPUT); //蜂鸣器输出
digitalWrite(7, 1); //让蜂鸣器别吵
}
void loop() {
// put your main code here, to run repeatedly:
// Serial.println(digitalRead(6));
if (zhuantai == 1) {
digitalWrite(13, 1);
music();
} else {
digitalWrite(13, 0);
digitalWrite(7, 1);
}
}
void stateChange() //中断回调函数
{
delay(50);
if (digitalRead(2) == 1) {
Serial.print("D2:");
Serial.println(digitalRead(2));
zhuantai = 1;
}
}
//中断回调函数2
void wbzd() //外部中断2,3号引脚
{
if (digitalRead(3) == 0)
{
delay(50);
if (digitalRead(3) == 0)
{
zhuantai = 0;
Serial.print("D3:");
Serial.println(digitalRead(3));
}
}
}
void music() //播放音乐 涉及到音阶和音调
{
int i;
int Frequency;
int toneTime, notoneTime;
for (i = 0; i < sizeof(note) / sizeof(int); i++)
{
if (pitch[i] != 0)
{
Frequency = note[Scale + rise[i] * 7 ][pitch[i]];
toneTime = mlength[i] * BPM * rate;
notoneTime = mlength[i] * BPM - toneTime;
tone(buzzerPin, Frequency);
delay(toneTime);
delay(notoneTime);
noTone(buzzerPin);
if (zhuantai == 0)return 0;
}
else {
delay(BPM * mlength[i]);
if (zhuantai == 0)return 0;
}
}
if (zhuantai == 0)return 0;
}
效果
arduino火焰传感器控制蜂鸣器放歌