LoginSignup
5
1

More than 5 years have passed since last update.

続編)ダイソープラレール*2+Obnizで作るIoTリモコンカー(戦車版:ゴム鉄砲発射機能を追加)

Last updated at Posted at 2018-09-24

概要

前編の
https://qiita.com/keicafeblack/items/de2e4f408e7103049929
にゴム鉄砲機能を追加して戦車にしました。

この機能が付いただけで、うちの息子達は大興奮でした。
男の子はこういうのが大好きですよね。
videotogif_2018.09.24_15.57.31.gif

割りばしとサーボモーターをテープで止めるだけなので、10分ほどで完成です。
KIMG3752.jpg

追加材料

・割りばし
・サーボモーター

コード

obniz_tank.html
<!-- HTML Example -->
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
  <script src="https://unpkg.com/obniz@1.9.3/obniz.js" crossorigin="anonymous"></script>
</head>
<body>

<div id="obniz-debug"></div>
  <br>
<button id="lf" class="btn btn-warning" style="width:45%;height:100px;font-size:50px;"></button>
<button id="rf" class="btn btn-warning" style="width:45%;height:100px;font-size:50px;"></button>
  <br>
  <br>
<button id="lb" class="btn btn-primary" style="width:45%;height:100px;font-size:50px;"></button>
<button id="rb" class="btn btn-primary" style="width:45%;height:100px;font-size:50px;"></button>
    <br>
  <br>
  <button id="on">ON</button>
  <button id="off">OFF</button>

<script>
var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  var motorA = obniz.wired("DCMotor",  {forward:3, back:2});
  motorA.power(40);
  var motorB = obniz.wired("DCMotor",  {forward:0, back:1});
  motorB.power(40);

    var servo = obniz.wired("ServoMotor", {gnd:4, vcc:5, signal:6});


  $("#lf").on('touchstart mousedown', ()=>{
    motorA.move(true);
  })
  $("#lf").on('touchend mouseup',()=>{
    motorA.stop();
  })
  $("#lb").on('touchstart mousedown',()=>{
    motorA.move(false);
  })
  $("#lb").on('touchend mouseup',()=>{
    motorA.stop();
  })

  $("#rf").on('touchstart mousedown',()=>{
    motorB.move(true);
  })
  $("#rf").on('touchend mouseup',()=>{
    motorB.stop();
  })
  $("#rb").on('touchstart mousedown',()=>{
    motorB.move(false);
  })
  $("#rb").on('touchend mouseup',()=>{
    motorB.stop();
  })

        $("#on").on("click",function(){
      servo.angle(100.0); // half position

      });
      $("#off").on("click",function(){
      servo.angle(180.0); // 180 position
    });
}

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

参考にしたサイト

Qiitaに動画を埋め込む方法

5
1
2

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
5
1