- 無源蜂鳴器
Code(單音節)
#include pitches.h
// notes in the melody:
int melody[] = {
NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C6};
int duration = 500; // 500 miliseconds
void setup() {
}
void loop() {
for (int thisNote = 0; thisNote < 8; thisNote++) {
// 在 pin8 上輸出聲音,每個音階響 0.5 秒
tone(8, melody[thisNote], duration);
// 間隔一段時間後再播放下一個音階
delay(1000);
}
// 兩秒後重新播放
delay(2000);
}
Code(超級馬力)#include pitches.h
// notes in the melody : super mario
int melodyMario[] = {
NOTE_E4, NOTE_E4, NOTE_E4, NOTE_C4, NOTE_E4, NOTE_G4, NOTE_G3,
NOTE_C4, NOTE_G3, NOTE_E3, NOTE_A3, NOTE_B3, NOTE_AS3, NOTE_A3, NOTE_G3, NOTE_E4, NOTE_G4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_E4, NOTE_C4, NOTE_D4, NOTE_B3,
NOTE_C4, NOTE_G3, NOTE_E3, NOTE_A3, NOTE_B3, NOTE_AS3, NOTE_A3, NOTE_G3, NOTE_E4, NOTE_G4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_E4, NOTE_C4, NOTE_D4, NOTE_B3,
NOTE_G4, NOTE_FS4, NOTE_E4, NOTE_DS4, NOTE_E4, NOTE_GS3, NOTE_A3, NOTE_C4, NOTE_A3, NOTE_C4, NOTE_D4, NOTE_G4, NOTE_FS4, NOTE_E4, NOTE_DS4, NOTE_E4, NOTE_C5, NOTE_C5, NOTE_C5,
NOTE_G4, NOTE_FS4, NOTE_E4, NOTE_DS4, NOTE_E4, NOTE_GS3, NOTE_A3, NOTE_C4, NOTE_A3, NOTE_C4, NOTE_D4, NOTE_DS4, NOTE_D4, NOTE_C4,
NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_C4, NOTE_A3, NOTE_G3, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_D4, NOTE_E4,
NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_C4, NOTE_A3, NOTE_G3
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurationsMario[] = {
8,4,4,8,4,2,2,
3,3,3,4,4,8,4,8,8,8,4,8,4,3,8,8,3,
3,3,3,4,4,8,4,8,8,8,4,8,4,3,8,8,2,
8,8,8,4,4,8,8,4,8,8,3,8,8,8,4,4,4,8,2,
8,8,8,4,4,8,8,4,8,8,3,3,3,1,
8,4,4,8,4,8,4,8,2,8,4,4,8,4,1,
8,4,4,8,4,8,4,8,2
};
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 98; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurationsMario[thisNote];
tone(8, melodyMario[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
}
tone()函數說明:
tone(pin, frequency)
tone(pin, frequency, duration)
pin 訊號接口, frequency,頻率,duration間距
| 無源蜂鳴器接口完成圖 |
- 有源蜂鳴器
- 透過藍芽控制LED和蜂鳴器
Code
#include pitches.h
SoftwareSerial BTSerial(10, 11); // RX | TX
int Buzzerpin = 3;
void setup()
{
Serial.begin(57600);
//pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
pinMode(Buzzerpin,OUTPUT);
digitalWrite(Buzzerpin,LOW); //蜂鳴器初始為不鳴叫
BTSerial.begin(57600); // HC-06 current bound rate (default 9600)
}
void loop()
{
// 讀出第 1 個字元
unsigned char charreceived = BTSerial.read();
switch(charreceived){
case '1':
//digitalWrite(13, HIGH);
digitalWrite(8, HIGH); //啟動LDE
digitalWrite(Buzzerpin,HIGH); //啟動蜂鳴器
Serial.println(" Arduino Led On");
// Alarm();
break;
case '0':
// digitalWrite(13, LOW);
digitalWrite(8, LOW); //關閉LED
Serial.println(" Arduino Led Off");
digitalWrite(Buzzerpin,LOW); ////關閉蜂鳴器
break;
default:
break;
}
Serial.flush();
delay(10);
}
參考資料
- http://yehnan.blogspot.tw/2012/02/arduinoloudspeaker.html
- http://coopermaa2nd.blogspot.tw/2010/12/arduino-lab6.html
- http://blog.csdn.net/bitezijie/article/details/24815203
沒有留言:
張貼留言