2
0

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で、近づくとHappy Birthdayの歌が流れる+LEDがちかちか【初心者】

Posted at

はじめに

obnizスターターキットに含まれているものだけで何か作りたいなと思って、ハッピーな作品を作りました。
相手がある一定距離以上近づいてきたらHappy Birthdayの歌が流れ、同時にLEDがちかちかと順番に光ります。
obnizのオンラインのHTMLエディタで作成したため、非常に簡単にできます。

使用したもの

(全てobnizスターターキットに含まれています。)
obniz Board
USB micro B cable
LED Keyestudio_TrafficLight
ブザー Keyestudio_Buzzer
距離センサー HC-SR04

###準備
obnizの準備は公式HPを参考にします。
https://obniz.com/ja/doc/reference/board-1y/quick-start/
obniz boardにセンサー類を接続します。
LEDを0,1,2,3(GNDが0)
距離センサーを5,6,7,8(GNDが5)
ブザーを9,10,11(Sが9)
となるように接続します。
(※どこに接続しても問題ないです。上記と違うところに接続した場合は、var sp, var hcsr, var ledの数字を変更してください。)

<html>
 <head>
   <script src="https://unpkg.com/obniz@3.x/obniz.js"></script>
 </head>
 <body>
   <script>
     var obniz = new Obniz("obniz ID"); // ここに自身のobniz IDを入力する
     var sp = 9
     var hcsr = 5
     var led = 0
 
     obniz.onconnect = async function () {
       for (let i = 0; i < 10; i++) {
       var speaker = obniz.wired("Keyestudio_Buzzer", {signal: sp, vcc:sp+1, gnd:sp+2});
       var hcsr04 = obniz.wired("HC-SR04", {gnd:hcsr, echo:hcsr+1, trigger:hcsr+2, vcc:hcsr+3});
       var light = obniz.wired("Keyestudio_TrafficLight", {gnd:led, green:led+1, yellow:led+2, red:led+3});
      
       const distance = await hcsr04.measureWait();
       console.log("distance " + distance + " mm")
         if (distance < 100) {
           light.next();
           speaker.play(130.815)
           await obniz.wait(200)
           light.next();
           speaker.play(130.815)
           await obniz.wait(200)
           light.next();
           speaker.play(146.835)
           await obniz.wait(400)
           light.next()
           speaker.play(130.815)
           await obniz.wait(400)
           light.next()
           speaker.play(174.62)
           await obniz.wait(400)
           light.next()
           speaker.play(164.82)
           await obniz.wait(800)
           light.next()
           speaker.play(130.815)
           await obniz.wait(200)
           light.next()
           speaker.play(130.815)
           await obniz.wait(200)
           light.next()
           speaker.play(146.835)
           await obniz.wait(400)
           light.next()
           speaker.play(130.815)
           await obniz.wait(400)
           light.next()
           speaker.play(196.0)
           await obniz.wait(400)
           light.next()
           speaker.play(174.62)
           await obniz.wait(800)
           light.next()
           speaker.play(130.815)
           await obniz.wait(200)
           light.next()
           speaker.play(130.815)
           await obniz.wait(200)
           light.next()
           speaker.play(261.63)
           await obniz.wait(400)
           light.next()
           speaker.play(220.93)
           await obniz.wait(400)
           light.next()
           speaker.play(174.62)
           await obniz.wait(400)
           light.next()
           speaker.play(164.82)
           await obniz.wait(400)
           light.next()
           speaker.play(146.835)
           await obniz.wait(800)
           light.next()
           speaker.play(233.082)
           await obniz.wait(200)
           light.next()
           speaker.play(233.082)
           await obniz.wait(200)
           light.next()
           speaker.play(220.00)
           await obniz.wait(400)
           light.next()
           speaker.play(174.62)
           await obniz.wait(400)
           light.next()
           speaker.play(196.0)
           await obniz.wait(400)
           light.next()
           speaker.play(174.62)
           await obniz.wait(800)
           light.next()
         }
 
         await obniz.wait(1000); // 1秒待つ
         speaker.stop();
     }
     }
   </script>
 </body>
</html>

参考文献

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?