1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

obnizのサンプルプログラムを試してみた2(圧電スピーカー)

Last updated at Posted at 2018-08-06

#概要

#試したパーツ

  • 圧電スピーカー

#圧電スピーカー
周波数を1000hzから100ずつ下げながら、0.5秒ずつ鳴らす.
obniz圧電スピーカー

private_speaker.html
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
  <script src="https://unpkg.com/obniz@1.9.2/obniz.js" crossorigin="anonymous"></script>
</head>
<body>

<div id="obniz-debug"></div>
<h1>obniz instant html</h1>
<div id="print"></div>

<script>
var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {

// Javascript Example
var speaker = obniz.wired("Speaker", {signal:0, gnd:1});

speaker.play(1000); //1000hz
await obniz.wait(500);
speaker.play(900); //900hz
await obniz.wait(500);
speaker.play(800); //800hz
await obniz.wait(500);
speaker.play(700); //700hz
await obniz.wait(500);
speaker.play(600); //600hz
await obniz.wait(500);
speaker.stop();
  
}
  
</script>
</body>
</html>

#追記 ドレミファソラシド
周波数を変えていくと音が変わることが分かったので、どうすればドレミファソラシドになるのか調べてみた.

ド 262Hz レ 294Hz ミ 330Hz ファ 349Hz ソ 392Hz ラ 440Hz シ 494Hz ド 523Hz

だそうです.
https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1053868595

やってみた.

private_speaker_doremi.html
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
  <script src="https://unpkg.com/obniz@1.9.2/obniz.js" crossorigin="anonymous"></script>
</head>
<body>

<div id="obniz-debug"></div>
<h1>obniz instant html</h1>
<div id="print"></div>

<script>
var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {

  var speaker = obniz.wired("Speaker", {signal:0, gnd:1});

  // tone()関数
  // frequency : 周波数(Hz)
  // duration  : 音を鳴らす時間(ミリ秒)
  async function tone(frequency, duration){
    speaker.play(frequency);
    await obniz.wait(duration);
    speaker.stop();
  }
  
  // ドレミファソラシドの周波数データを配列化
  var sounds = ['262', '294', '330', '349', '392', '440', '494', '523'];

  // tone()関数を使って演奏する
  for(var i=0; i<sounds.length; i++){
    tone(sounds[i], 500);
  }

}
  
</script>
</body>
</html>

speaker.play()をズラズラ書いていくのも何なので、演奏データは配列化し、演奏処理は関数にした.
「演奏」と書くとなんだか大げさな気がしますが。。。

以下の投稿が参考になった.
[WIP] Arduinoで音を出したい(メモ)

#参考
obniz
https://obniz.io/

[WIP] Arduinoで音を出したい(メモ)
https://qiita.com/tadfmac/items/7fb26596fe6240959c48

Arduino Reference
tone()
https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?