/*
全家便利商店來客報音
*/
byte btn=33;
byte 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); // this speeds up the simulation
}
//音樂播放
void sound(){
//音階 E6 C6 G5 C6 D6 G6 0 D6 E6 D6 G5 C6
//音長 1 1 1 1 1 2.5 0.5 1 1 1 1 2
int t =200 ; //基本節拍
tone(buzzer,C5,t);
tone(buzzer,D5,t);
tone(buzzer,E5,t);
tone(buzzer,F5,t);
tone(buzzer,G5,t);
tone(buzzer,A5,t);
tone(buzzer,B5,t);
//-----------------
tone(buzzer,C6,t);
tone(buzzer,D6,t);
tone(buzzer,E6,t);
tone(buzzer,F6,t);
tone(buzzer,G6,t);
tone(buzzer,A6,t);
tone(buzzer,B6,t);
}