LoginSignup
1
3

More than 5 years have passed since last update.

obnizのサンプルプログラムを試してみた3(DCモーター)

Last updated at Posted at 2018-08-22

概要

  • obnizのサンプルプログラムを試してみたのDCモーター編.
  • 「Parts Library for obniz」に掲載されているものを組み合わせてやってみただけ.
  • 「forward」ボタンを押したらモータを回転する.「reverse」ボタンを押したらモーターを逆回転する.
  • モータードライバ不要.モーターそのまま挿すだけ.(ここ大事!)

試したパーツ

  • DCモーター

DCモーター

みのむしクリップで挟んだだけ.
ArduinoやRaspberryPiでDCモーターを使おうとすると「モータードライバ」なる謎のパーツが出てきて一気に難易度が上がり電子工作素人の私にはギブアップ気味だが、これならすんなり受け入れられる.
obnizでDCモーター

画面にはボタンを2つ配置.
「forward」ボタンを押したら1秒回転する.
「reverse」ボタンを押したら1秒逆回転する.
obnizでDCモーター

private_DCMotor.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.4/obniz.js" crossorigin="anonymous"></script>
</head>
<body>

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

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

var motor = obniz.wired("DCMotor", {forward:1, back:0});

//モーターを回転する
$('#forward').click(function () {
  motor.forward();
  setTimeout(function(){
    motor.stop();
  }, 1000);
});

//モーターを逆回転する
$('#reverse').click(function () {
  motor.reverse();
  setTimeout(function(){
    motor.stop();
  }, 1000);
});

}

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

動画
画面を映してないので分かりづらいが、最初に「forward」ボタンを押す.
するとモーターが1秒回転して止まる.
続いて、「reverse」ボタンを押す.
今度はモーターが1秒逆回転して止まる.
モーターの先にピニオンギアしか付けていないので回っているのかどうか分かりづらいが、回転する際にモーター自体が横倒しになるのでなんとか見て取れるかと.
「forward」ボタンを押したときと「reverse」ボタンを押したときとでは、のたうち回り方が逆方向なのも見て取れる.

参考

obniz
https://obniz.io/

ArduinoでDCモーターを使う場合
https://trac.switch-science.com/wiki/DCMotorDriver

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