ブザーモジュール
私が購入したGROVEスターターキットV3に入っていたブザーモジュールは、Buzzer V1.2というモジュールです。スイッチサイエンスさんの単体販売ページに掲載されているものと同じです。


ブザーモジュールの動作
ブザーモジュールはデジタル出力モジュールです。1(HIGH)を送るとブザーが鳴り、0(LOW)を送るとブザーが止まります。音量の調整はできません。かなり大きな音が出るので、静かな部屋だとびっくりします。
プログラム
1秒間音を鳴らした後1秒間音を止めるという動作を繰り返すプログラムです。
// Nefry BT無印の場合: D2, A0, A2 (注:D0は使えません)
// Nefry BT R2の場合: D0, D2, D5, A1 (注:A0を使うときはA1を記載します。)
#define PIN D2
void setup() {
pinMode(PIN, OUTPUT);
Serial.print("PIN = ");
Serial.println(PIN);
}
void loop() {
digitalWrite(PIN, HIGH);
Serial.println(HIGH);
delay(1000);
digitalWrite(PIN, LOW);
Serial.println(LOW);
delay(1000);
}
setup( )関数内のpinMode(PIN, OUTPUT)で、GPIOピンを出力に設定しています。loop( )関数内では、まずdigitalWrite(PIN, HIGH)でHIGHを出力して音を鳴らし、delay(1000)で1秒間待ちます。次にdigitalWrite(PIN, LOW)でLOWを出力して音を止め、delay(1000)で1秒間待ちます。
メロディーを演奏するプログラム
Seed Studio社のGrove Wikiページにはブザーでメロディーを演奏するプログラムが掲載されています。
下記のリストでは、Nefry BTで動作するように、19行目のBUZZER_PINの設定をコメントアウトし、24行目でBUZZER_PINの値を変更してあります。
/*
Buzzer
The example use a buzzer to play melodies. It sends a square wave of the
appropriate frequency to the buzzer, generating the corresponding tone.
The circuit:
* Buzzer attached to pin39 (J14 plug on Grove Base BoosterPack)
* one side pin (either one) to ground
* the other side pin to VCC
* Note:
This example code is in the public domain.
http://www.seeedstudio.com/wiki/index.php?title=GROVE_-_Starter_Kit_v1.1b#Grove_-_Buzzer
*/
/* Macro Define */
//#define BUZZER_PIN 39 /* sig pin of the buzzer */
// Nefry BT用にPIN定義を変更する
// Nefry BT無印の場合: D2, A0, A2 (注:D0は使えません)
// Nefry BT R2の場合: D0, D2, D5, A1 (注:A0を使うときはA1を記載します。)
#define BUZZER_PIN D2
int length = 15; /* the number of notes */
char notes[] = "ccggaagffeeddc ";
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void setup()
{
/* set buzzer pin as output */
pinMode(BUZZER_PIN, OUTPUT);
}
void loop()
{
for(int i = 0; i < length; i++) {
if(notes[i] == ' ') {
delay(beats[i] * tempo);
} else {
playNote(notes[i], beats[i] * tempo);
}
delay(tempo / 2); /* delay between notes */
}
}
/* play tone */
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(BUZZER_PIN, HIGH);
delayMicroseconds(tone);
digitalWrite(BUZZER_PIN, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
60-70行目のplayNote(char note, int duration )が、指定された高さの音を鳴らす関数です。引数noteはnames[ ]配列に入っている’c’, ‘d’,・・・で音の高さを指定します。’c’は’ド’, ‘d’は’レ’, ・・・です。引数durationはその音を鳴らす時間をmsで指定します。tones[ ]配列は、音の矩形波がHight/LOWになっている時間をmicro second単位で覚えています。
51-58行目のplayTone(int tone, int duration)が実際に音を鳴らしている関数です。digitalWrite(BUZZER_PIN, HIGH)でブザーを鳴らしdelayMicroseconds(tone)でtoneで指定されたmicro secondだけ待ちます。次にdigitalWrite(BUZZER_PIN, LOW)でブザーを止めdelayMicroseconds(tone)でtoneで指定されたmicro secondだけ待ちます。toneで指定されるmicro secondが非常に短い時間なので、このOn/Offの繰り返しで音の高さを調整しています。音を鳴らしている時間の調整は52行目で行なっています。
実行してみると、綺麗な音ではありませんが、一応メロディーに聞こえます。
諸元
| 名称 | Buzzer ブザー |
| バージョン | v1.2 |
| Seeed社 Wiki | http://wiki.seeedstudio.com/Grove-Buzzer/ |
| スイッチサイエンス商品ページ | https://www.switch-science.com/catalog/804/ |
| Nefry BT無印 動作ソケット | D2, A0, A2 |
| Nefry BT R2 動作ソケット | D0, D2, D5, A0(プログラム上はA1) |
「Nefry BTとGroveモジュール接続実験」 トップページ
