#define buzzer 2 //定义buzzer I/O number is 7;
#define KEYO 3 //定义key I/O NUMBER IS 2;
#define KEYT 4 //定义key I/O NUMBER IS 2;
int KEY_NUM = 0; //按键变量赋初值
void BUZZER() {
for (int i = 0; i < 80; i++) { //输出一个频率的声音
digitalWrite(buzzer, HIGH); //发声音
delay(1);//延时1ms
digitalWrite(buzzer, LOW); //不发声音
delay(1);//延时1ms
}
}
void ScanKey() {
KEY_NUM = 0;
if (digitalRead(KEYO) == LOW) //有按键按下
{ delay(80);
if (digitalRead(KEYO) == LOW) {
KEY_NUM = 1; //变量设置为1
while (digitalRead(KEYO) == LOW); //等待按键松手
}
}
if (digitalRead(KEYT) == LOW) //有按键按下
{ delay(80);
if (digitalRead(KEYT) == LOW) {
KEY_NUM = 1; //变量设置为1
while (digitalRead(KEYT) == LOW); //等待按键松手
}
}
if (KEY_NUM == 1)
BUZZER();
}
void setup()
{
pinMode(KEYO, INPUT_PULLUP); //定义keyI/O is INPUT_PULLUP
pinMode(KEYT, INPUT_PULLUP); //定义keyI/O is INPUT_PULLUP
pinMode(buzzer, OUTPUT); //定义buzzer I/O is OUTPUT
Serial.begin(300);
}
void loop()
{
ScanKey(); //按键扫描程序,当按键按下时,该子程序会修改key-num的值
}