3
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 1 year has passed since last update.

雪だるま作ろう〜obnizで〜【ちかちかしながらジングルベルが流れる】初心者向け

Posted at

はじめに

クリスマスツリーとあわせて飾る雪だるまが作りたくて、どうせなら光る方が、どうせなら音楽流れる方が、と思ってobnizで作りました。
https://twitter.com/ped_yi/status/1601755032390889472

準備する物

obnizスターターキット(obniz Board、USB micro B cable、LED Keyestudio_TrafficLight
ブザー Keyestudio_Buzzer、ボタンKeyestudio_Button)
発泡スチロール(雪だるま胴体)、装飾品(私は鼻用に人参の先をつけ、毛糸の帽子をかぶせました。)

実装

obnizの開発者コンソールはとても便利ですがブラウザを開いておかないといけないため、今回はNode.jsを使って動かしました。

↓参考になります。
obniz.comに掲載されているnode.jsの記事

パーツをつないで、後は好きな音楽の楽譜と周波数表を見ながらひたすら音を打ち込むのみです!

speaker.play(195.998);
await obniz.wait(400);

↑この部分の数字をそれぞれ変更します。
同じ音がずっと続くと長い1音みたいになっちゃうので適宜途切れ目を入れました。

全体像

"use strict";

const Obniz = require("obniz");

const obniz = new Obniz("obnizの番号");
obniz.onconnect = async function () {
  let light = obniz.wired("Keyestudio_TrafficLight", {
    gnd: 0,
    green: 1,
    yellow: 2,
    red: 3,
  });
    let button = obniz.wired("Keyestudio_Button", {
    signal: 4,
    vcc: 5,
    gnd: 6,
  });
  let speaker = obniz.wired("Keyestudio_Buzzer", {
    signal: 9,
    vcc: 10,
    gnd: 11,
  });
  
  obniz.repeat(async () => {
    await button.stateWait(false);
    console.log("pressed");
    light.green.on();
    light.yellow.on();
    light.red.on();
    await obniz.wait(1000);
    light.green.off();
    light.yellow.off();
    light.red.off();
    await obniz.wait(100);
        // 点滅させる
    light.yellow.blink();
    light.green.blink();
    light.red.blink();


    // そみれどそ
    speaker.play(195.998);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(293.665);
    await obniz.wait(400);
    speaker.play(261.626);
    await obniz.wait(400);
    speaker.play(195.998);
    await obniz.wait(1000);

    // そそみれどら
    speaker.play(195.998);
    await obniz.wait(400);
    speaker.play(195.998);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(293.665);
    await obniz.wait(400);
    speaker.play(261.626);
    await obniz.wait(400);
    speaker.play(220.0);
    await obniz.wait(1000);

    // ららふぁみれし
    speaker.play(220.0);
    await obniz.wait(400);
    speaker.play(220.0);
    await obniz.wait(400);
    speaker.play(349.228);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(293.665);
    await obniz.wait(400);
    speaker.play(246.942);
    await obniz.wait(1000);

    // ふぁそそふぁれみ
    speaker.play(349.228);
    await obniz.wait(400);
    speaker.play(391.995);
    await obniz.wait(400);
    speaker.play(391.995);
    await obniz.wait(400);
    speaker.play(349.228);
    await obniz.wait(400);
    speaker.play(293.665);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(1000);

    // そそみれどそ
    speaker.play(195.998);
    await obniz.wait(400);
    speaker.play(195.998);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(293.665);
    await obniz.wait(400);
    speaker.play(261.626);
    await obniz.wait(400);
    speaker.play(195.998);
    await obniz.wait(1000);

    // そそみれどら
    speaker.play(195.998);
    await obniz.wait(400);
    speaker.play(195.998);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(293.665);
    await obniz.wait(400);
    speaker.play(261.626);
    await obniz.wait(400);
    speaker.play(220.0);
    await obniz.wait(1000);

    // ららふぁみれそそそそらそふぁれど
    speaker.play(220.0);
    await obniz.wait(400);
    speaker.play(220.0);
    await obniz.wait(400);
    speaker.play(349.228);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(293.665);
    await obniz.wait(400);
    speaker.play(391.995);
    await obniz.wait(400);
    speaker.play(391.995);
    await obniz.wait(400);
    speaker.play(391.995);
    await obniz.wait(400);
    speaker.play(391.995);
    await obniz.wait(400);
    speaker.play(440.0);
    await obniz.wait(400);
    speaker.play(391.995);
    await obniz.wait(400);
    speaker.play(349.228);
    await obniz.wait(400);
    speaker.play(293.665);
    await obniz.wait(400);
    speaker.play(261.626);
    await obniz.wait(800);

    speaker.play(391.995);
    await obniz.wait(800);

    // みみみーみみみーみそどれみ
    speaker.play(329.628);
    await obniz.wait(390);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(790);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(329.628);
    await obniz.wait(390);
    speaker.stop();
    speaker.play(329.628);
    await obniz.wait(390);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(329.628);
    await obniz.wait(790);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(391.995);
    await obniz.wait(400);
    speaker.play(261.626);
    await obniz.wait(600);
    speaker.play(293.665);
    await obniz.wait(200);
    speaker.play(329.628);
    await obniz.wait(1150);
    speaker.stop();
    await obniz.wait(50);

    // ふぁふぁふぁーふぁふぁみみみみれれみれ、そー
    speaker.play(349.228);
    await obniz.wait(390);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(349.228);
    await obniz.wait(390);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(349.228);
    await obniz.wait(590);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(349.228);
    await obniz.wait(200);
    speaker.play(349.228);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(390);
    speaker.stop();
    speaker.play(329.628);
    await obniz.wait(390);
    speaker.stop();
    speaker.play(329.628);
    await obniz.wait(390);
    speaker.stop();
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(293.665);
    await obniz.wait(400);
    speaker.play(293.665);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(293.665);
    await obniz.wait(800);
    speaker.play(391.995);
    await obniz.wait(800);

    // みみみーみみみーみそどれみ
    speaker.play(329.628);
    await obniz.wait(390);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(329.628);
    await obniz.wait(390);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(329.628);
    await obniz.wait(790);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(329.628);
    speaker.play(329.628);
    await obniz.wait(390);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(790);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(391.995);
    await obniz.wait(400);
    speaker.play(261.626);
    await obniz.wait(600);
    speaker.play(293.665);
    await obniz.wait(200);
    speaker.play(329.628);
    await obniz.wait(1150);
    speaker.stop();
    await obniz.wait(50);

    // ふぁふぁふぁーふぁふぁみみみそそふぁれど
    speaker.play(349.228);
    await obniz.wait(390);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(349.228);
    await obniz.wait(390);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(349.228);
    await obniz.wait(590);
    speaker.stop();
    await obniz.wait(10);
    speaker.play(349.228);
    await obniz.wait(200);
    speaker.play(349.228);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(329.628);
    await obniz.wait(400);
    speaker.play(391.995);
    await obniz.wait(400);
    speaker.play(391.995);
    await obniz.wait(400);
    speaker.play(349.228);
    await obniz.wait(400);
    speaker.play(293.665);
    await obniz.wait(400);
    speaker.play(261.626);
    await obniz.wait(800);

    speaker.stop();
    light.green.off();
    light.yellow.off();
    light.red.off();
  }, 0);
};

おわりに

是非みなさんも作ってクリスマスを楽しんでください♪
Happy Birthday to youのコードはこちら

3
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
3
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?