/*
程序名称:
描述:
1. 所需硬件:
- Arduino Uno开发板 *1
- 面包板 *1
- 杜邦线 *若干
2. 接线方式(具体请查看接线图):
-
3. 本程序需要安装如下库(括号里是库的作者):
- 无 ( by 无 ) V1.0.0
作者:ChantionLAB·开物室
联系方式:
*/
/*--------------------------------------------------------------------------1--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------本程序所引用库--------------------------------------------------------------------*/
// 无
/*--------------------------------------------------------------------------2--------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------声明定义-----------------------------------------------------------------------*/
/*****************************< 常量声明 >*****************************/
// 无
/*****************************< 引脚定义 >*****************************/
const int Speaker_Pin = 9;
const int Button_Pins[] = { 8, 7, 6, 5, 4, 3, 2 };
const int Led_Pins[] = { A2, A1, A0, 13, 12, 11, 10 };
/*****************************< 变量定义 >*****************************/
const int Button_Tones[] = { 262, 294, 330, 349, 392, 440, 494};
const int Num_Tones = sizeof(Button_Pins) / sizeof(Button_Pins[0]);
/*****************************< 对象声明 >*****************************/
// 无
/*****************************< 函数声明 >*****************************/
// 无
/*--------------------------------------------------------------------------3--------------------------------------------------------------------------*/
/*-------------------------------------------------------------------setup()程序初始化------------------------------------------------------------------*/
void setup() {
/******************************< 启动串口通讯 >******************************/
Serial.begin(9600); // 初始化串口通信,并设置波特率为9600
/******************************< 设置引脚模式 >******************************/
for (int i = 0; i < Num_Tones; i++) {
pinMode(Button_Pins[i], INPUT_PULLUP);
pinMode(Led_Pins[i], OUTPUT);
}
pinMode(Speaker_Pin, OUTPUT);
}
/*--------------------------------------------------------------------------4--------------------------------------------------------------------------*/
/*-------------------------------------------------------------------loop()基础循环体-------------------------------------------------------------------*/
void loop() {
int Freq = 0;
for (uint8_t i = 0; i < Num_Tones; i++) {
if (digitalRead(Button_Pins[i]) == LOW) {
Freq = Button_Tones[i];
digitalWrite(Led_Pins[i],HIGH);
}
}
if (Freq) {
tone(Speaker_Pin, Freq,500);
delay(500);
} else {
noTone(Speaker_Pin);
}
for (int i = 0; i < Num_Tones; i++) {
digitalWrite(Led_Pins[i],LOW);
}
}
/*--------------------------------------------------------------------------5--------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------函数定义-----------------------------------------------------------------------*/
/*
函数:name()
作用:
参数:
- 无
返回值:
- 无
*/