無源蜂鳴器+陣列
這顆有點吵,聲音不是很好聽,
純粹練習程式碼,以後要療癒情緒,可以考慮接個好一點的喇叭
將無源蜂鳴器接在D9,以下為電話鈴聲範例
int buzzerPin=9;
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
ringTone(buzzerPin);
}
void ringTone(int pin) {
for (int i=0; i<10; i++) { //repeat 10 times
tone(pin, 1000);
delay(50);
tone(pin, 500);
delay(50);
}
noTone(pin);
delay(2000);
}
使用函式
#define E5 659
#define C5 523
#define G5 784
int buzzerPin=9;
void setup() {
pinMode(buzzerPin, OUTPUT);
mario(buzzerPin);
}
void loop() {
}
void mario(int pin) {
tone(pin, E5, 150); //E5
delay(150); //pause for key transition
tone(pin, E5, 150); //E5
delay(150); //pause for key transition
tone(pin, E5, 150); //E5
delay(150); //pause for key transition
delay(150); //pause for a Quarter rest (1/4)
tone(pin, C5, 150); //C5
delay(150); //pause for key transition
tone(pin, E5, 150);
delay(150); //pause for key transition
delay(150); //pause for a Quarter rest (1/4)
tone(pin, G5, 150); //G5
delay(3000);
}
使用範例音樂,學習一維陣列,
注意用雙引號匯入, 內建函式庫是用角括號
上網找自己喜歡的曲子