/*
全家便利商店來客報音
警報音
*/
byte btn1=33;
byte btn2=25;
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(btn1, INPUT_PULLUP);
  pinMode(btn2, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);
}
void loop() {
  if(!digitalRead(btn1)) {
    while(!digitalRead(btn1));
    sound1();
  }
  if(!digitalRead(btn2)) {
    while(!digitalRead(btn2));
    sound2();
  }
  delay(10); // this speeds up the simulation
}

//音樂播放
void sound1(){
  //音階 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); 
}
//救護車+複頻合音
void sound2(){
  tone(buzzer,C6,500);
  tone(buzzer,D5,500);
  delay(200);
  tone(buzzer,C6,500);
  tone(buzzer,D5,500);
  delay(200);
  tone(buzzer,C6,500);
  tone(buzzer,D5,500);
  delay(200);
  for(int j=0;j<3;j++){     //次數
    for(int i=1000;i>500;i-=20){  //降頻速率
      tone(buzzer,i,20);          //短音音長
      delay(10);                  //斷音效果
      tone(buzzer,i+50,20);       //複頻
      delay(10);                  //斷音效果
    }
    delay(20);              //每次間隔
  }  
 tone(buzzer,0,1000);       //靜音
}