7
2

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 3 years have passed since last update.

Obnizと圧電スピーカーで日が暮れたら「ゆうやけこやけ」を流して切なくなってみた

Posted at

#まずは完成形
CdSセル(照度センサ)で暗くなると、ゆうやけこやけが流れます。
この曲聞くと切なくなるな。。

#使った部品
・Obniz Board 1Y
・圧電スピーカー(PKM13EPYH4000-A0)
・CdSセル(MI527/MI5527)
・カーボン抵抗 1/2W330Ω(CFS50J330RB)
・ミニブレッドボード(BB-601)
・ブレッドボード・ジャンパーワイヤ(オス-オス)(BBJ-20)

#接続図
image.png

#コード

.js
const Obniz = require('obniz');
const obniz = new Obniz('0000-0000'); // Obniz_ID

// 任意の秒数待つことができる関数
// 参考: https://qiita.com/suin/items/99aa8641d06b5f819656
const sleep = (msec) => new Promise(res => setTimeout(res, msec));

// 音階
const Key = {
    "" : 261.626,
    "" : 293.665,
    "" : 329.628,
    "ファ" : 349.228,
    "" : 391.995,
    "" : 440.000,
    "" : 493.883,
    "ド2" : 523.251,
    "レ2" : 587.330
}

obniz.onconnect = async function () {

    // ディスプレイ表示
    obniz.display.clear();
    obniz.display.print('TEST');

    // スピーカー
    const speaker = obniz.wired('Speaker', { signal: 0, gnd: 1 });

    // 照度センサ
    obniz.io9.output(true);  // io9電圧を5Vに(電源+)
    obniz.io11.output(false); // io11電圧を0Vに(電源−)

    // setIntervalで一定間隔で処理
    setInterval(async function () {
        // io10をアナログピンに(照度センサーの値を取得)
        var voltage = await obniz.ad10.getWait();
        console.log(`changed to ${voltage} v`);

        if (voltage < 0.3) {
            // 暗くなったら「ゆうやけこやけ」を流す
            speaker.play(Key[""]); await sleep(1000); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);

            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);

            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);

            speaker.play(Key[""]); await sleep(1500); speaker.stop(); await sleep(500);

            speaker.play(Key[""]); await sleep(1000); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);

            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key["ド2"]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key["ド2"]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);

            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);
            speaker.play(Key[""]); await sleep(500); speaker.stop(); await sleep(100);

            speaker.play(Key["ド2"]); await sleep(1500); speaker.stop(); await sleep(500);
        }
    }, 20000);  // 20秒(約1曲分は待つ)
}

#やってみた感想
圧電スピーカーは周波数で音階を決められるので、曲を流すのは割と簡単でした。
参考:音階周波数

今回、圧電スピーカーで曲を流してみたかっただけですが、なかなか面白かった。ハマりそうです。。スピーカーを複数繋げれば、和音も表現できそうで曲の幅が広がりそうで、時間があったらやってみたい。

7
2
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
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?