/*
來客觸動開關,報音
*/
#define btn 33
#define buzzer 22
//中音音階-------------------
#define C5 523
#define D5 587
#define E5 659
#define F5 698
#define G5 784
#define A5 880
#define B5 988
//高音音階-------------------
#define C6 1047
#define D6 1175
#define E6 1319
#define F6 1397
#define G6 1568
#define A6 1760
#define B6 1976
//-------------------
void setup() {
Serial.begin(115200);
pinMode(btn, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
}
void loop() {
if(!digitalRead(btn)){ //開門碰觸微動
while(!digitalRead(btn)); //防彈跳
sound(); // 播放音樂
}
delay(10); // 偵測間距
}
// 音樂播放
void sound(){
//音階 E6 C6 G5 C6 D6 G6 D6 E6 D6 G5 C6
//音長 1 1 1 1 1 2 1 1 1 1 2
int t = 300; //基本節拍
tone(buzzer, E6, t); Serial.println("Mi_E6");
tone(buzzer, C6, t); Serial.println("Do_C6");
tone(buzzer, G5 , t); Serial.println("So_G5");
tone(buzzer, C6, t); Serial.println("Do_C6");
tone(buzzer, D6, t); Serial.println("Re_D6");
tone(buzzer, G6, 2*t); Serial.println("So_G6");
tone(buzzer, D6, t); Serial.println("Re_D6");
tone(buzzer, E6, t); Serial.println("Mi_E6");
tone(buzzer, D6, t); Serial.println("Re_D6");
tone(buzzer, G5 , t); Serial.println("So_G5");
tone(buzzer, C6, 2*t); Serial.println("Do_C6");
}